Question 14.3: Setting Up PyFirmata to Control an Arduino from a Raspberry ......

Setting Up PyFirmata to Control an Arduino from a Raspberry Pi

You want to use an Arduino as an interface board for your Raspberry Pi.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

Connect the Arduino to a USB socket of the Raspberry Pi so that the computer can communicate and send power to the Arduino.

Next, install the Firmata sketch onto the Arduino and the PyFirmata onto your Raspberry Pi. This entails installing the Arduino IDE, so if you haven’t already done so, follow Recipe 14.1.

The Arduino IDE includes Firmata, so all you have to do to install Firmata onto your Arduino board is to upload a sketch. You will find the sketch at File→Examples→Firmata→StandardFirmata.

Once Firmata is installed, the Arduino waits for communication from the Raspberry Pi.

Now you need to install PyFirmata, the other half of the link. This requires the use of the PySerial library, so follow Recipe 8.8 to install this. The PyFirmata library is maintained on GitHub, so you will also need to install Git by using the command sudo aptget install git (Recipe 3.19).

You can now download and install PyFirmata using these commands:

$ git clone https://github.com/tino/pyFirmata.git
$ cd pyFirmata
$ sudo python setup.py install

You can try out the PyFirmata library from the Python console. Enter the following commands to turn on the built-in LED on Arduino pin 13 (marked with an L) and then turn it off again.

$ sudo python
Python 2.7.3 (default, Jan 13 2013, 11:20:46)
[GCC 4.6.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import pyfirmata
>>> board = pyfirmata.Arduino(‘/dev/ttyACM0’)
>>> pin13 = board.get_pin(‘d:13:o’)
>>> pin13.write(1)
>>> pin13.write(0)
>>> board.exit()

Discussion

The preceding code first imports the PyFirmata library and then makes an instance of Arduino called board, using the USB interface (/dev/ttyACM0) as its parameter. We can then gain a reference to one of the Arduino pins (in this case, 13) and set it to be a digital output. The d is for digital, 13 is the pin number, and o is for output.

To set the output pin high, you just use write(1) and to set it low write(0). You can also use True and False in place of 1 and 0.

Figure 14-4 shows an Arduino with the rows of connections down both sides of the board.

The pins at the top of Figure 14-4 marked 0 to 13 can be used as digital inputs or outputs. Some of these pins are also used for other things. Pins 0 and 1 are used as a serial interface, and are in use when the USB port is being used, and pin 13 is attached to the on-board LED marked with an L. The digital I/O pins 3, 5, 6, 9, 10, and 11 have a ~ symbol next to them that indicates that they can be used for PWM output (see Figure 14-4).

On the other side of the board, there is one set of connectors that supply power at 5V and 3.3V and six analog inputs marked A0 to A5.

An Arduino Uno on its own uses about 50 mA, which, given that the Raspberry Pi itself is probably using about 10 times that, makes it perfectly possible to power the Arduino from the USB connection of the Raspberry Pi. However, if you start attaching a lot of external electronics to the Arduino, and the current consumption increases, then you may want to power the Arduino from its own power adapter by using the DC barrel socket. This will accept 7V to 12V DC.

The only real downside of using Firmata is that because all instructions have to come from the Raspberry Pi, it doesn’t make much use of Arduino’s ability to run independently. For advanced projects, you will probably end up writing your own Arduino code that receives instructions from the Raspberry Pi and/or sends messages to the Raspberry Pi, while it gets on with other tasks.

See Also

The following recipes look at using the full range of Arduino pin features with PyFirmata: 14.4, 14.6, 14.7, 14.8, and 14.9.

You can find the official pyFirmata documentation at pyFirmata’s GitHub.

14.4

Related Answered Questions

Question: 14.11

Verified Answer:

The I2C bus has the concept of master and slave de...
Question: 14.10

Verified Answer:

Use a level converter or pair of resistors to conn...
Question: 14.9

Verified Answer:

Use PyFirmata to send commands to an Arduino to ge...
Question: 14.8

Verified Answer:

Use PyFirmata to send commands to an Arduino to ge...
Question: 14.7

Verified Answer:

Use PyFirmata to read an analog input on the Ardui...
Question: 14.6

Verified Answer:

Use PyFirmata to read a digital input on the Ardui...
Question: 14.5

Verified Answer:

Use a level converter to connect the RXD pin of th...
Question: 14.14

Verified Answer:

Use an aLaMode interface board. You may find that ...