Introduction: 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
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)
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.
Serial.print("I am Counting to: ");
Step 3: 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
myData = arduinoSerialData.readline()
Simple as that! Welcome to the world of having the Power of Python now at your fingertips.
We have a be nice policy.
Please be positive and constructive.