Arduino serial communication with Python

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.

4 Responses to “Arduino serial communication with Python”


  1. 1 Dave Jul 3rd, 2007 at 1:16 am

    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

  2. 2 Jason Brower Oct 20th, 2009 at 10:52 am

    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

  1. 1 serial to ethernet Trackback on Jul 1st, 2007 at 10:23 am
  2. 2 4ceac4b1bf7b Trackback on May 10th, 2008 at 10:47 pm

Leave a Reply