Introduction: PYTHON WITH ARDUINO

Picture of PYTHON WITH ARDUINO

Today we are going to start a new series of lessons that will allow you to take your Arduino projects up to the next level. If you are not familiar with the Arduino, you should start with ARDUINO lessons. If you are familiar with Arduino, you can just right into this series of Lessons.
One of the major limitations of the Arduino IDE is its limited ability to interact with the user. You can print text to a simple serial monitor text box, or get text input from the user. You can open up a new world of possibilities by using the programming language python to interact with the Arduino. Python combined with Arduino is a powerful combination what will drastically increase the WOW factor of your projects.

Step 1: PYTHON WITH ARDUINO : INSTALLING THE SOFTWARE

Picture of PYTHON WITH ARDUINO : INSTALLING THE SOFTWARE

There are some really incredible things we can do when we get our little Arduino to talk to the big bad Python programming language. To do that, we have to start by downloading some software. Never fear . . . it is all free

You can download these three software packages:
1) Download and Install Python 2.7.8. Please note all my tutorials use this flavor of python. If you want to follow my tutorials, do notdownload a Python 3.x. Also download 32 bit version of the software, even if you have a 64 bit windows machine.

2) Download and Install Pyserial version 2.7. Again, download the 32 bit version of the software.

3) Download and install the Vpython library for 32 bit windows.

Step 2: CODING(ARDUINO)

Picture of CODING(ARDUINO)

Now, lets get python and arduino talking together. First up, we need a simple program to get the Arduino sending data over the serial port. The following program is a simple program that will have the Arduino count and send the data to the serial port.

int cnt=0;
void setup()

{

Serial.begin(9600);

}

void loop()

{

Serial.print("I am Counting to: ");

Serial.print(cnt);

Serial.println(" Mississippi.");

cnt=cnt+1;

delay(1000);

}

Step 3: CODING(PYTHON)

Picture of CODING(PYTHON)

Now, open the VIDLE environment which you downloaded with the Vpython library. Once you have done that, you are ready to write a Python Program that will go out and read the data over the serial port.Do note, however, that the line:

arduinoSerialData = serial.Serial('com11',9600)

This is for a windows machine. Also, the ‘com11’ has to be adjusted and set to whatever com port your arduino is talking on. You can figure that out by looking at Tools – Port on the Arduino. Set this parameter to whatever port your Arduino is talking on. Then, the baud rate needs to match as well. You can use whatever you want by Arduino and Python need to be on the same baud rate, which you set on this line of code. OK, the complete Python code to read the Arduino data from the serial port is here:

import serial #Import Serial Library
arduinoSerialData = serial.Serial('com11',9600) #Create Serial port object called arduinoSerialData

while (1==1):

if (arduinoSerialData.inWaiting()>0):

myData = arduinoSerialData.readline()

print myData

Simple as that! Welcome to the world of having the Power of Python now at your fingertips.

Comments

YassineU (author)2017-05-04

hi & thanks for the amazing tuto Im actually working on a project 'reading the state of button on arduino & send it to be written in txt file using python code "so the problem is everytime i try to read the value from arduino port & opening up also port from python it tells me that port is bussy on arduino ......what should i do thanks in advance

russ_hensel (author)2016-10-23

A bit brief, but your instructable is now featured in the collection: Arduino and Python and Perhaps a Rasberry Pi: https://www.instructables.com/editCollection/OFR9W...

alok014 (author)russ_hensel2016-10-23

wait for my second lessons(Instructable) tomorrow i will show u how to connect real world with virtual

russ_hensel (author)alok0142016-10-23

If you are doing more look at: https://www.instructables.com/id/Put-Your-Code-in-a... And also consider git hub.

alok014 (author)russ_hensel2016-10-23

ok bro :)

About This Instructable

2,407views

39favorites

Bio: can't say about my self it's totally depends on ur behavior
More by alok014:Diwali SpecialGesture Controlled MouseGetting Started With ARDUINO
Add instructable to: