Arduino serial communication with Python


Posted

in

,

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.

Comments

8 responses to “Arduino serial communication with Python”

  1. serial to ethernet…

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

  2. Dave Avatar
    Dave

    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. 4ceac4b1bf7b…

    4ceac4b1bf7b17a7369d…

  4. […] 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. Jason Brower Avatar

    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. Nate Avatar
    Nate

    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. propecia Avatar

    The authoritative point of view, funny…

  8. dfehgiuoe Avatar

    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!