COMMUNICATION BETWEEN ARDUINO & PYTHON !

Introduction: COMMUNICATION BETWEEN ARDUINO & PYTHON !

About: I'm a guy who loves to play with technology .....

So recently I started learning python and was amazed by the simplicity and capabilities. So I thought lets connect it to arduino and see what I can do with it. Communication between Arduino and Python isn't a new concept, There are many modules available for Python that help in the process. In this instructable I'm gonna be using 'pyserial' package for the communication. For this you will need a basic understanding of Python and Arduino. I'm using 'Python 2.7' here.

So lets get started...

Step 1: GATHERING COMPONENTS

1. Arduino. (I've used UNO here)

2. LED. (We will be using pin 13 so this is optional )

3. Python 2.7

4. Arduino IDE.

Step 2: SETUP PROCESS

Before Writing any code first you need to install Arduino IDE & Python 2.7.

1. Install the IDE. visit Arduino's website at www.arduino.cc and download the latest IDE compatible to your OS. Then follow the installation process and you are done.

2. Download and install Python from www.python.org . The installation is simple enough if you have doubts you can ask in comments or there are many good tutorials online.

3. Once the Python is up and running. You have to install the 'pyserial' module. To do this open your command prompt(CMD) and type in 'pip install pyserial' (without the single quotes) hit enter and the installation process should start.

NOTE: You will need active internet connection to use pip.

Once pyserial is installed the setup is completed and we can move on to coding.

Step 3: WRITING ARDUINO SKETCH

First open up your Arduino IDE and write the code given below :-

//declaring variables

int LED = 13; // pin 13 is given variable LED int data; // Variable to store data void setup() { Serial.begin(9600); //setting baud rate for communication pinMode(LED,OUTPUT); //Pin 13 set as output digitalWrite(LED,LOW); //LED is off by default } void loop() { while(Serial.available()) //check if data is available { data = Serial.read(); //while data is available read the data } if(data == '1') //if data is value '1' { digitalWrite(LED,HIGH); //turn LED on Serial.println("LED turned on"); //print output on serial monitor } else if(data == '0') //if data is value '0' { digitalWrite(LED,LOW); //turn LED off Serial.println("LED turned off"); //print output on serial monitor } }

Compile the code and upload it to the Arduino. Now open Serial monitor goto>>tools>>serial monitor. Enter '1' to turn LED 'on' and '0' to turn it 'off'. Also check the PORT where arduino is connected , You can find the port name by going to tools and port. Here my port is 'COM3' this is required in Python code so it knows where the Arduino board is.

Step 4: WRITING PYTHON SCRIPT

To write Python script open python IDLE Goto>> Files >> New file.

Write the code given bellow:

import serial #imports the pyserial module
import time #imports time module required for delays (sleep functions) arduino = serial.Serial('COM3', 9600) #Creates arduino object and establishes connection to port (Enter your port) time.sleep(2) #waits for connection to establish print arduino.readline() print ("Enter '1' to turn 'on' the LED and '0' to turn LED 'off'") #asks for input while 1: #while data is available var = raw_input() #accepts input puts it in variable 'var' print "You Entered :", var #prints input if(var == '1'): #if input is 1 arduino.write('1') #sends '1' to arduino print("LED turned on") time.sleep(1) if(var == '0'): #if var is 0 arduino.write('0') #sends '0' to arduino print("LED turned off")

Once you have the code. Save it and goto Run / Hit 'F5' key to execute the code.

You will see a screen asking for input. just Enter 1/0 to turn LED on/off.

If the LED does not respond for the first time just close the IDLE and unplug Arduino and plug it again.

Step 5: FILES

Here I have provided the code files so you can just download it and use.

Hope this Instructable is useful to you. You can try many things with it.

In future project I will be writing python code to recognize colour and send it to arduino so arduino can process it and turn on appropriate LED.

If you have any questions you can leave a comment.

Share

    Recommendations

    • Tiny Home Contest

      Tiny Home Contest
    • Creative Misuse Contest

      Creative Misuse Contest
    • Water Contest

      Water Contest

    Discussions