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.