Skip to content

Python: interfacing with an arduino

by mat on February 3rd, 2010

So what is an arduino?
An arduino is an open source open hardware programmable controller with several inputs and outputs. The image below shows an Ardunio Dicemella.

Ardunio Dicemella Annotated Photo

Ardunio Dicemella Annotated Photo

It (Arduino Dicemella) has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started.

They are very useful for people who know how to program but have little experience with hardware interaction.

Programming the arduino
This post will not contain in-depth detail on how to program the arduino, instead focussing briefly on setting up serial (over serial or usb cable) communications in order to talk to a python script. The arduino can be programmed via a IDE provided by the creators in a C-style hardware language.

Code example

int ledPin = 13;            // choose the pin for the LED
int inputPin = 2;          // choose the input pin (for a pushbutton)
int val = 0;                // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare pushbutton as input
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
  }
}

Arduino LED switch circuit off

Arduino LED switch circuit off


Arduino LED switch circuit on

Arduino LED switch circuit on

Now we add a few lines to enable the writing of information from our arduino over the serial connection. We first need to set up the transfer speed in our setup (Serial.begin(9600);). Then we can simply send messages over serial using Serial.print(“message\n”);. You can choose between print and println with the difference been that the latter automatically appends the newline char, so we would use the former to write multiple things to the same line. Below is our modified code:

Serial write example

int ledPin = 13;           // choose the pin for the LED
int inputPin = 2;         // choose the input pin (for a pushbutton)
int val = 0;               // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare pushbutton as input
  Serial.begin(9600);
  Serial.print("Program Initiated\n");  
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    Serial.print("LED Activated\n");
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
  }
}

We now add into this code the ability to receive information via serial. Below is the modified example which removes the action of the button and replaces it by activating the LED when ‘Y’ is sent via serial.

Serial read example

int ledPin = 13;  // choose the pin for the LED
int val = 0;      // variable for reading the pin status
char msg = '  ';   // variable to hold data from serial


void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  Serial.begin(9600);
  Serial.print("Program Initiated\n");  
}

void loop(){
        // While data is sent over serial assign it to the msg
	while (Serial.available()>0){ 
		msg=Serial.read();
	}

  // Turn LED on/off if we recieve 'Y'/'N' over serial 
  if (msg=='Y') {            
    digitalWrite(ledPin, HIGH);  // turn LED ON
    Serial.print("LED Activated\n");
    msg=' ';
  } else if (msg=='N') {
    digitalWrite(ledPin, LOW); // turn LED OFF
  }
}

Interaction with python

First we import the serial library to python in order to communicate with the arduino (this includes talking over usb).

import serial

We then attempt to connect to our arduino on /dev/ttyUSB0, using try and except to catch an exception if we are unable to find the arduino on USB0. The 9600 corresponds to the baud rate (speed of communication) that we are using with the arduino and should be the same as set in the program on the arduino otherwise your communication may appear garbled.

try:
	arduino = serial.Serial('/dev/ttyUSB0', 9600)
except:
	print "Failed to connect on /dev/ttyUSB0"

The address will be /dev/ttyUSB# where # is replaced by a number for arduinos connected via usb and /dev/ttyS# where # is replaced by a number for arduinos connected via serial. If you are not sure of the location of your arduino, it can be found in the arduino IDE or you can write some python to scroll through possible locations until a response is found

locations=['/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3',
'/dev/ttyS0','/dev/ttyS1','/dev/ttyS2','/dev/ttyS3']

for device in locations:
	try:
		arduino = serial.Serial(device, 9600)
	except:
		print "Failed to connect on",device

You may need to be careful as other devices can be connected. For example if I try to connect to /dev/ttyS0 I will connect to the wacom tablet on my laptop.

Once you have connected to your arduino successfully you can write information to it using write and read information sent from it using read (you will need to import time to use the sleep function). If your arduino does not send any messages via serial then attempting to readline will result in your program hanging until it receives a message.

try:
	arduino.write('Y')
	time.sleep(1)
	print arduino.readline()
except:
	print "Failed to send!"

So the python code should now look like the following and we should be able to control the LED over serial.

import serial
import time

locations=['/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3',
'/dev/ttyS0','/dev/ttyS1','/dev/ttyS2','/dev/ttyS3']  
  
for device in locations:  
	try:  
		print "Trying...",device
		arduino = serial.Serial(device, 9600) 
		break
	except:  
		print "Failed to connect on",device   

try:  
    arduino.write('Y')  
    time.sleep(1)
    print arduino.readline()
except:  
    print "Failed to send!" 

The above will send the character ‘Y’ (Y for Yes please turn on the LED) to the arduino wait for 1 second and then read from the arduino which will have hopefully posted a response to our ‘Y’. Using the program on this should turn the LED on, and report LED Activated back via serial to our python program. This should be enough for people to get started with ardunios and communicating with them in python.

References

  • Arduino – The arduino website with everything you are likely to need (programming examples and reference guide, and hardware information)
  • Arduino tutorial – a basic and easy to understand tutorial on programming the arduino
  • Python port of arduino-serial.c – By John Wiseman from which I based my program.
  • original arduino-serial.c – by Tod E. Kurt.
  • Sparkfun – Here is a good place to purchase ardunio and other electronics parts. Try coolcomponents if your from the uk like me
  • Dealextreme – Hong Kong based retailer that sells a lot of cheap DIY electronics and also has worldwide free delivery with no min spend (crazy). Does take about two weeks to arrive though (uk).
15 Comments
  1. Anon permalink

    Die-ci-mi-la. :)

  2. David Boddie permalink

    Arduino programming is interesting, even if the scope for Python programming is restricted to the host computer. One related project I found recently is this Python-based IDE:

    http://code.google.com/p/arduino-pyqt/

    Of course, the language is still the Arduino variant of C++, but it’s interesting to see work being done on tools for the Arduino.

  3. wrybread permalink

    Fantabulous tutorial. Its funny how rare it is to read a tutorial that’s not obfuscated by overly complicated details. This is just super clear and demonstrates everything needed to get the job done. Why aren’t more tutorials on the intershnitzel like this?

  4. Simon permalink

    I am having problems. I have used your code for both Arduino and Python. When I use the Arudino Serial monitor to send a ‘Y’ The LED comes on. When python sends it, the LED comes on for a second and then goes off again.

    I have changed to pin 8 to ensure that it was not just flashing when it received data. But Pin 8 still works with the Serial Monitor, but again flashes on and off when I use Python.

    I have altered the code on Arduino to make the LED flash a number of times when it recievies the ‘Y’ and this happens, so I know the all code is working to the point where the LED just turns off.

    I am using Windows Vista 64 Bit if that makes any difference.

  5. Abhi permalink

    I am new to interfacing Arduino to Python. I tried the example you have provided on this site.
    When i run the code the output on the Python IDLE window is as follows :

    Trying… COM4
    Failed to connect to COM4
    Failed to send!!

    I am using Win7. According to the Arduino program the board is connected to the COM4 (that is also suggested by the device manager).
    What can i do in this case ?

    There was an other question i had in mind:
    How does the python code identify what arduino code has to be uploaded and run. Or does it run the code that was previously uploaded?

    Thank you in advance,
    Cheers

  6. tonnytg permalink

    Problem with python 3.2:

    arduino.write(‘Y’) don’t work, i find people use raw_input() for this problem but python 3.2 don’t have raw_input.

    Erro :
    File “/usr/lib/python3.2/site-packages/serial/serialposix.py”, line 467, in write
    t = len(data)

    Do you know how?

  7. I dugg some of you post as I thought they were very helpful extremely helpful Visit my website . thx.!

  8. steven kavuma permalink

    Simon the serial port can only hold one message at a time. Therefore after sending the “Y” value to the arduino. It turns on the led. Then when you send the message “led activated” the “Y” value is lost and the led goes off.If you want the led to remain on comment the “led activated” line in arduino.

  9. Maya permalink

    Can someone help me? Python doesn’t know what to do with the import serial command.

    I get this error:

    Traceback (most recent call last):
    File “C:/Users/Mayan/Desktop/Programmer’s Module/Python Arduino/Python/Tutorial2.py”, line 5, in
    import serial
    ImportError: No module named serial

    Please help!

  10. But Vick broke his fibula one day after Madden was released
    and missed the majority of a terrible 5-11 season. So many young men become the
    kind of moody, depressed, and angry boy that media often creates and then society wants to fix.

    Well, I don’t sit right down, first I go through my
    ritual.

Trackbacks & Pingbacks

  1. Python: interfacing with an arduino « Arduinian Tales
  2. Tracking Signups with Woot Lights | Loggly
  3. Syncing a DS1307 automatically using python | My Arduino Project
  4. Serial to Arduino Code « Freakin Blinky Lights!
  5. Arduino und Python | bits and pieces

Leave a Reply

Note: I am currently writing my thesis so probably wont have time to reply to your comment
Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS