Blog

Python: interfacing with an arduino

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).

19 Comments

  1. […] Python: interfacing with an arduino By d.g. Nice simple example in interfacing Arduino with Python. Via [Stealthcopter] […]

  2. Anon

    Die-ci-mi-la. 🙂

  3. David Boddie

    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.

  4. wrybread

    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?

  5. […] The code for all this stuff is on Github. You’ll need to load up your Arduino with the serial.c file, which contains a simple serial protocol that brings a pin high on receiving a ‘Y‘ and low on receiving a ‘N‘. More information on talking to Arduinos with Python is here. […]

  6. Simon

    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.

  7. […] (c code). I did a bit of research about communicating between Arduino and python. Thanks to this page it wasn’t hard to get started. With alot of trial and error (probably should have payed more […]

  8. Abhi

    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

  9. tonnytg

    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?

  10. Ricardo Grosman

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

  11. […] http://www.stealthcopter.com/blog/2010/02/python-interfacing-with-an-arduino/ Share this:TwitterFacebookLike this:LikeBe the first to like this post. Leave a comment […]

  12. steven kavuma

    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.

  13. Maya

    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!

  14. […] gibt weiter noch folgendes Tutorial für Python und die Ardunios. This entry was posted in Arduino. Bookmark the permalink. ← […]

  15. stanley

    great tutorial.

    but am having difficulties. my arduino is on port /dev/ttyACM0 as shown in the IDE. but when I run the python script it says:

    Failed to connect on /dev/ttyACM0

    am using python 2.7 in ubuntu 12.04

    please help.

  16. Cindy

    Hello Flaviu!I might be able to help you, but I need to know what kind of sensors you have. Is it a cieobnmd temperature, humidity and pressure sensor or are they separate? You need to find the part/model numbers. My temperature sensor is a DS18S20 digital temperature sensor.

  17. Erik Marsja

    Hey,

    thanks for an awesome tutorial!

    Really helpful,

    Erik

  18. Kris

    One of the most important parts of your home is your roof, so it is important that you keep
    it in top shape. Interestingly the roof structure appears to be the main repellent of the unwanted effects
    of the environment. In the event of cold weather
    the small heat in the conditions is radiated.

  19. Jurgen

    If you are interested in topic: earn online by clicking pen meme
    – you should read about Bucksflooder first

Leave a Comment

Your email address will not be published. Required fields are marked *