In my post on Y-Hack I touched upon a Python based technology called BreakfastSerial. Some of you Arduino fans out there may already be familiar with it, but for those who are not I have provided short guide below.
BreakfastSerial is a Python based library that allows you to write Python code that can be run on the Ardunio.
The library runs on top of a technology called Firmata.
Firmata is a generic protocol for communicating with Microcontrollers from software running on a host computer.
You can read more about the Arduino implementation of Firmata at the Arduino.cc website:
BreakfastSerial works by allowing you to interact with Firmata loaded onto your Arduino by writing Python code.
You simply upload Firmata to your Arduino board, and then can start writing Python applications that use it.
We will look at an example next.
Setting up Firmata
In this post I will only be covering the steps for the older boards such as the Uno. There is a separate post in the works for the YUN. I will also be assuming you are using a Linux like environment (Ubuntu, Mac terminal window, Cygwin etc.)
To start with you will need to open up the Arduino IDE sketch pad, if you haven’t download it already you can find it at:
Note, that depending on your Arduino model (e.g. Uno versus YUN) there are different versions of the IDE.
Once you have the IDE installed, you can find the Firmata software under:
File -> Examples -> Firmata -> StandardFirmata
Next connect your board up to your computers USB port and upload the sketch.
Once uploaded you can then move onto the testing out BreakfastSerial.
Open a terminal window on your machine and using pip grab the latest version of BreakfastSerial from PyPi
pip install BreakfastSerial
I tend to work in a virtual environment when using Python. If you wish to do the same, you can create a virtual environment as follows
virtualenv --system-site-packages Ardunio
This will create a new directory called Arduino with a Python virtual environment in. From within this, you can activate it using
If you don’t have virtualenv on your machine you can grab it using pip:
pip install virtualenv
Next we can create a test project using Python. Before we do this though, take an LED and attach it to digital Pin 13 and Ground on your Arduino – yes you guessed it we are going to make the LED blink.
Once you have your hardware hooked up, create a new Python file called blink.py for example:
Inside this file we are going to add the following code:
from BreakfastSerial import Arduino, Led
Here we are importing Arduino and Led from the BreakfastSerial library.
Next let’s add the following code:
board = Arduino()
pin = 13
led = Led(board, pin)
This sets up a variable
board with an instantiation of
Arduino and a
pin variable set to 13 (where you attached your LED). Finally the
led variable is declared with a copy of
Led(), which we pass
pin to as arguments.
Once you have added the above, we can include the final piece of code that creates the blinking action:
the above is pretty self explanatory. The led is switched on, we wait two seconds and then it is switch off.
Save the file and exit.
We can now test our application. From the command line run:
You should now see your LED blink.
And that’s all there is too it really.
There are a host of examples you can try located on the BreakfastSerial site: