Ceci est une ancienne révision du document !
This time, we will start controlling the Arduino with the Raspberry Pi. To do this, we will be using the Firmata library and protocol.
What the heck is Firmata?
Firmata is a serial communications protocol long used for communications between microcontrollers and programs on another computer. Firmata gives access directly to the Arduino board. You can communicate through the serial port of the “host” computer using just about any serial language, including Python. It is based on the MIDI spec.
By using the “Standard Firmata” script included in the Arduino examples, you can access and control any of the digital and analogue pins on the Arduino board, without having to write any custom code. If you want, you can write specialized code incorporating the Firmata library for the Arduino that does specialized functions.
Getting Started
Load the “StandardFirmata” script from the Arduino examples (File|Examples|FIrmata|StandardFirmata), compile and send it to the Arduino.
If you are going to test Firmata/Arduino on the Raspberry Pi, you should download a version of Firmata_Test program that is compiled for the RPi from: https://github.com/freetronics/PiLeven/wiki/Direct-Control-with-Firmata, otherwise, you can download the Firmata_Test from firmata.org/wiki/Main_Page#Firmata_Test_Program. I saved it to my RPi desktop, but you can save it anywhere you want.
Be sure to set the permissions to allow execution, since it’s a .bin file. Then run it
./firmata_test
Set the port to the same port your Arduino is. In my case, it’s on /dev/ttyACM0. Then after a few seconds, the program will show the 13 digital ports and the 5 analogue ports.
Click on the button marked “Low” for pin 13. This should change to “High” and the LED on the arduino board should light up. Click it again and it should turn back to “Low” and the LED should go out.
Going Further
For now, most anything you want to do in Python will work with the “Standard” Firmata code uploaded to the Arduino by using the pyFirmata Python library. However, you can write your own Firmata code on the Arduino.
For the Python side of things, we will need to install two libraries (if they aren't already installed). The first is pySerial and the second is pyFirmata. You can use pip to install both.
sudo pip install pyserial
sudo pip install pyfirmata
Using pySerial
Our first Python example will require a momentary push-button connected to pin 2 of the Arduino. Below is the Fritzing breadboard layout.
Basically, we simply connect +5VDC from the Arduino through the pushbutton to Digital pin #2. Alternately, if you don’t have a small pushbutton available, you can momentarily connect a jumper between the +5VDC pin and Digital pin #2.
On the Arduino, compile DigitalRealSerial example file from the File|Examples|01 Basics menu.
On your computer, you can either simply use the Python Terminal Interface or enter the following code into an IDE like Geany and run it in a terminal.
import serial
s = serial.Serial('/dev/ttyACM0',9600) # You might need to change this to ACM1 or whatever your Arduino is connected to
while True:
print s.readline()
As you press the button and release it, you should see the output in the terminal change from 0 to 1 and 1 to 0.
To quit the program, use <Ctrl> + C.
Now you have written your first program to control the Arduino from Python.
Now we’ll do something a bit more complicated. We’ll “monitor” an analogue voltage (controlled by a potentiometer) on analogue input A0 and if the voltage is over a certain value (.50) we’ll turn on a LED connected to digital pin #2. Below is the Fritzing breadboard.
Using the StandardFirmata Arduino code again, start up the Arduino board.
Below is the Python code. I’ve named it “analogue_test1.py”.
import pyfirmata
PORT = “/dev/ttyACM0” # Change this to suit your setup
board = pyfirmata.Arduino(PORT)
In the first three lines, we import the pyfirmata library, assign the serial port and create an instance of the “board”.
PINS = (0, 1, 2, 3)
Here we are setting up for 4 analogue pins to be handled, even though we are only going to use pin 0 for this example.
The next two lines create an iterator to handle the serial communications for the analogue ports. This is recommended in the pyfirmata documentation.
it = pyfirmata.util.Iterator(board)
it.start()
Now we enable the 4 analogue pins for reporting so we can read the value. We also set up digital pin 0 as an output pin (to be able to drive the LED).
for pin in PINS:
board.analog[pin].enable_reporting()
pin2 = board.get_pin('d:2:o')
The next two lines will read analogue pin 0 and then wait one second. This allows for the board to settle before we start the loop.
val = board.analog[0].read()
board.pass_time(1)
Now we start a forever loop…
while True:
val = board.analog[0].read()
print val
if val >= .50: pin2.write(1) else: pin2.write(0)
Within the loop, we read the analogue pin (remember the value will be between 0.0 and 0.9) and if it is at or greater than 0.5, then we write a 1 to the digital output pin #2, turning on the LED. Once it is under .50, then we write a 0 to the pin, turning off the LED again.
board.pass_time(1)
The board.pass_time command takes an integer of t number of whole seconds. It is a non-blocking call, so it does not block other processing.
The Python source code is available on pastebin at http://pastebin.com/xG9VJ34i
Now that you have the basic idea, we can move forward.
You might have realized that this solution will work only as long as we have a direct serial connection to the Arduino board. What if, however, we need to have a wireless connection to the Arduino? We’ll save that discussion for another time.
Enjoy.