Python Meets Arduino

Posted: May 25, 2011 in Uncategorized
Tags: , , , , , ,

I’m not going to be providing any ground breaking knowledge in this post as I’m not the first or even the 10,000th person to use a Python script to control an Arduino. My aim is simply to save you a little time if you are trying to do this for the first time.

The first order of business is to set up your Arduino board and take a few notes.

Here’s a cheesy sketch that toggles a few outputs depending on the character it sees on the Arduino’s RX pin. After you get this to work check out the “Firmata” library.

int firstOutput = 9;
int secondOutput = 10;
int inByte = 0;
void setup()
pinMode(firstOutput, OUTPUT);
pinMode(secondOutput, OUTPUT);
digitalWrite (firstOutput, LOW);
digitalWrite (secondOutput, LOW);
void loop()
if (Serial.available() > 0) {
inByte =;
if (inByte = 1) {
digitalWrite(firstOutput, HIGH);
digitalWrite(secondOutput, LOW);}
if (inByte = 2) {
digitalWrite(secondOutput, HIGH);
digitalWrite(firstOutput, LOW);}

After you download the sketch connect an LED and current limiting resistor in series from pin 9 to GND and from pin 10 to GND.

Click on the “Serial Monitor” button in your Arduino IDE. Set the baud to 9600. Make note of the serial port that’s being used (top of screen). Mine says “/dev/ttyUSB0”. You’ll need this info later.

Type “1” in the input line if the serial terminal (no quotes) and hit the “Send” button. One of your LEDs should turn on. Sending a “2” should toggle the LEDs. If this works move on. If not regroup and check your wiring.

Once you are able to toggle the LEDs it’s time to move on to the Python side of the equation. I’m not going to cover the install of Python or the pySerial plug in. This is covered elsewhere on line. Here’s a good place to start for the pySerial install:

Once you have Python and pySerial installed talking to Arduino is fairly straight forward. Open Python in your terminal. Just type “python” and hit Enter.

>>> import serial
>>> ser = serial.Serial(‘/dev/ttyUSB0’, 9600, timeout=1)
>>> ser.write(‘1’)
Hit Enter
>>> ser.write(‘2’)
Hit Enter

This should toggle the LEDs just like the Arduino Serial Monitor connection did.

What’s the next step? Let’s turn this into a simple script that toggles the outputs. Put the following in a text file and save it as “”. Note* your path to Python may be different…


import serial
import time
ser = serial.Serial(‘/dev/ttyUSB0’, 9600, timeout=1)


That should be it. In your terminal move into the directory that you just save the script in. Now just type “python” and something magical should happen to your LEDs. I’m crossing my fingers!

Here’s a little more info from


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s