At Europython 2012, I attended a talk about Python and the Arduino which motivated me to make my computer talk to my Arduino. Actually, using Python, it is pretty straightforward. This would bring some opportunities to have a real interaction between the computer and the physical world.


The Arduino is plugged to the computer trough a USB wire. So it is a serial communication between both of them. It is possible to send and receive messages to and from the Arduino, with the right program. To test this, we are going to set up a our board.

Setting all up

We are going to keep it simple at this stage. All we’ll need is an Arduino, a breadboard, a LED a light sensor and of course, some wires.

You can get an Arduino Uno from Amazon and a starter kit like the Arduino Sidekick Basic Kitin which you will find everything you need for the current tutorial.

Lets then build our mockup.

Our components assembled.

You will also need the Arduino software freely available. Even if Python is installed on your computer, you may need to add the pySerial library. There is a good documentation on http://pyserial.sourceforge.net/ explaining how to install it. On Ubuntu, you’ll have just to install the package the usual way with a

sudo apt-get install python-serial

Otherwise, you can use the classical Pythonistic ways:

pip pyserial

or

easy-install -U pyserial

We will need to upload a program on the Arduino allowing it to work properly. Here is the code we’ll use to send and receive messages with the Arduino board.:

int val = 0;
int sensorPin = 0;
int ledPin = 13;
byte incomingByte;

void setup() {
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);
}

void loop() {
    val = analogRead(sensorPin);
    Serial.println(val);

    if (Serial.available() > 0) {
        incomingByte = Serial.read();
        digitalWrite(ledPin, HIGH);
    }

    delay(1000);
    digitalWrite(ledPin, LOW);}

In each loop, the Arduino will read the value of the light sensor and write it on the serial allowing us to read it on the computer from the serial. On the Arduino, there is a serial buffer from which the Arduino can read. If a value is present, the LED will turn on one second and then turn off after one second.

Reading from the arduino

Once the previous code is uploaded on the Arduino, the board sends the value of the light sensor trough the serial wire. The following code reads that value and displays it. Before executing it on your computer, you should check and maybe change the path to your serial (/dev/tty.usbserial) since the one used here is an alias.

import serial

ser = serial.Serial('/dev/tty.usbserial', 9600)
while 1:
    line = ser.readline()
    print 'Value: %s' % line

So the value is printed continually on the terminal. Nice, but we can do something better by processing the received data. This will be pretty simple since it is raw Python:

import serial

ser = serial.Serial('/dev/tty.usbserial', 9600)

was_dark = False

while 1:
    line = ser.readline()
    if int(line) < 200 and not was_dark: # Adapt the value to your component
        print 'Its dark here, would you turn on the light ?'
        was_dark = True
    elif int(line) > 200 and was_dark:
        print 'Ok, you can turn off the light.'
        was_dark = False

You can of course add any process you want. As an homework, change the program to send a notification trough the notification framework of your system rather to print the status on screen.

Sending data to the Arduino

Sending data to the Arduino is just as easy using Python as reading it. The board is ready to receive any data from the serial. For that purpose, we will not write a program but use the Python shell. Simply write the following

>>> import serial
>>> ser = serial.Serial('/dev/tty.usbserial', 9600)
>>> ser.write('0')

In our code, the Arduino does not process the incoming data. If there is at least one byte in the buffer, the conditional code is executed. It reads the byte (removes it from the buffer) and turns on the LED.

And yes, if you execute the following instruction

>>> ser.write('foobar')

the Arduino stores 6 bytes in the buffer so it will need 6 iterations to empty it as one byte is read in each iteration. So the LED will be turned on for 1 second and turned (briefly) off 6 times.

Here again you can improve the code. As an homework, you can change the Arduino code so the LED is turned on with an “1”, off with a “0” and does nothing on any other byte.

With this the basic ways to communication between a computer and an Arduino board. You have everything you need to create your own mockups now. What will you build ?

About Darko Stankovski

Darko Stankovski is the founder and editor of Dad 3.0. You can find more about him trough the following links.