Busy making things: tinycast, github, links, photos, @mc.

Arduino serial communication with Python

Posted: January 18th, 2007 | Author: | Filed under: Projects, Python | 8 Comments »

The Arduino is here!

I got my shiny Arduino yesterday. The first order of business (after the obligatory “Hello World” led_blink sketch) was interfacing Arduino with my language of choice, Python.

Googling around for python serial led me to pySerial, a cross-platform serial library. I was actually quite suprised that such a wrapper didn’t exist in the Python Standard Library. Nevertheless, I plodded on.

The first order of business was symlinking the default device for the Arduino serial drivers on my mac (for sanity):
sudo ln -s /dev/tty.usbserial-LOTSOFCHARSANDNUMBERS /dev/tty.usbserial. From there I fired up the Python shell and ran the serial hello world sketch on my Arduino:

>>> import serial
>>> ser = serial.Serial('/dev/tty.usbserial', 9600)
>>> while 1:
...     ser.readline()
'1 Hello world!\r\n'
'2 Hello world!\r\n'
'3 Hello world!\r\n'

Writing from Python to Arduino is simple too. Load serial_read_blink and do the following from Python:

>>> import serial
>>> ser = serial.Serial('/dev/tty.usbserial', 9600)  
>>> ser.write('5')

Hooray, it worked! Communicating with the Arduino over serial with Python (just like every other language) is a pretty trivial process.


8 Comments on “Arduino serial communication with Python”

  1. 1 serial to ethernet said at 10:23 am on July 1st, 2007:

    serial to ethernet…

    Hi. Very nice blog. I\’ve been reading your other entries all day long..lol….

  2. 2 Dave said at 1:16 am on July 3rd, 2007:

    I just got a nokia 770 and I’d like to get the flash player in it to talk to arduino. Do you know how I could go about doing this? It seems like you have experience with both of these devices and would be a good person to ask.

    Thanks,
    Dave

  3. 3 4ceac4b1bf7b said at 10:47 pm on May 10th, 2008:

    4ceac4b1bf7b…

    4ceac4b1bf7b17a7369d…

  4. 4 zoomy dot net » Archive » Basic Arduino to Maya Communication said at 4:21 pm on July 26th, 2009:

    [...] I found this simple Arduino serial test script by Tod Kurt linked to by Matt Croydon (original file here), and uploaded it to my Arduino. [...]

  5. 5 Jason Brower said at 10:52 am on October 20th, 2009:

    Great work. These instructons worked for me as well. You wouldn’t happen to know how to send binary data would you?
    Best Regards,
    Jason

  6. 6 Nate said at 5:45 pm on April 11th, 2010:

    Curious. When I try to pull the same thing off in IDLE (2.6) it causes the interpreter to go unresponsive.

    Let’s give it a try running the interpreter in bash… same thing. Any ideas?

  7. 7 propecia said at 5:11 am on January 24th, 2011:

    The authoritative point of view, funny…

  8. 8 dfehgiuoe said at 12:12 pm on February 10th, 2011:

    Hi! I found your site accidentally this morning, but am really pleased that I did! It’s not only entertaining, but additionally straightforward to use compared with lots that I’ve viewed!