Communicate with Arduino using python
Posted: | More posts about python arduino
Let's see how easy it is to communicate with an Arduino 2009 board and the pySerial python's module. What we will do is to use python to send characters serially to an Arduino 2009 which will send them back.
Everything here has a demonstration purposes only, since the code proposed here has no specific function but you can easily modify it to get something usable for your projects (take a look at Comunikino for example). The methods has been tested on Ubuntu 10.04 but should work on other distributions as on various Windows and Mac OSX with minimal modification.
Use your favorite packet manager to install python-serial or from command line type the famous command (on Ubuntu &co.):
# apt-get install python-serial
Download ed install pySerial (the file should be pyserial-2.5.win32.exe)
Download test code
Program Arduino with this sketch Serial echo test program:
download the python code above in a file called communication.py
Open the python script with a text editor and see if at line 16 in place of '/dev/ttyUSB0' what you find the editor of the Arduino sketch below themenu Tools -> Serial Port -> xxxxxxx.
conn = serial.Serial('/dev/ttyUSB0', timeout=1)
Let's try it
With the sketch above loaded into Arduino and connected to the computer run:
$ python communication.py
If it works the board will send back all the ASCII characters that you type on the keyboard. You can verify if the communication is actually taking place looking at the flashing LED TX and RX on the board. Now you can to modify these examples to suit your needs.
If you have problems let me know!
Ah! The python's way…