Friday, September 10, 2010

When Python talk to Arduino

Yesterday I show a code where when we send a 't' via serial connection, the LED will blink while the arduino board, send back a series of string via serial, the code below is what I used to run on arduino, to test with the python code.
link


In the arduino development environment, there is a serial monitor that do the the testing.


Since it is just a standard serial interface, so one can just do it anyway they want. Here I show how to do it via python. First what we need, is the pyserial library, you can install it using pip or easy_install or grab it from pyserial site

To install, just use to following command. if you need more detail, read the docs, because I use python 2.7 it just works for me,
pip pyserial
or
easy_install pyserial
To use it, with the arduino code from the top of the page just, do the following
link


The line s=serial.Serial(2) points to com3 on windows, on linux it should be s=serial.Serial('/dev/ttyUSB0'), or other port that shown by the arduino serial monitor. Pyserial actually have a few options on the Serial object, but I found that for arduino, the default is adequate.

And this is how to use arduino to interface to your computer using python. enjiy

No comments:

Post a Comment