Using Raspberry Pi to Control Arduino With Firmata
Raspberry Pi and Arduino
The “Should I use a Raspberry Pi, or should I use an Arduino?” question always comes up in the first stages of product development. Honestly, who wouldn’t be confused? Both boards are insanely popular in the craft. Both somehow look the same. And most importantly, both are single-board devices that can be used to create anything under the sun.
The Arduino, born in 2005, is a microcontroller development board based on an ATMega328P. It is a platform that makes it easier to interact with electronic components such as sensors and displays—no need to use shift registers or assembly language. The whole Arduino environment makes electronic interfacing simple enough that you can even trick kids by disguising them as toys to make them learn electronics and programming.
On the other hand, the Raspberry Pi, which was introduced in early 2012, is a full-blown credit-card-sized computer. With its size, you may think it’s not powerful enough to do anything. You may think it only works with a command line because it can’t support a desktop environment. Well, yes, that’s probably right way back in 2012.
But now, the Raspberry Pi 4 has a quad-core processor running at 1.5GHz, max 8GB of RAM (higher than the laptop I’m using to write this article), and can run 2 displays via micro HDMI, in 4K! But the best part is the pre-installed Minecraft.
Now you might be thinking, why would I use the Arduino? If the Raspberry Pi is that powerful, wouldn’t it be more logical to stick with Raspberry Pi? The Raspberry Pi is powerful, yes, but since it is a computer, it needs the software to run as a computer. It needs an operating system to be exact. This creates many layers of abstraction that widen the gap between your program and your sensor, making real-time processing and timed responses unreliable. On the other hand, the Arduino has nothing to widen the gap. If you load the program to its flash memory, it simply runs it every time it’s powered on.
But you can use them together! This is what we’re going to do in this tutorial. Using a protocol called Firmata, we are going to have the best of both worlds.
Firmata
Firmata is a communication protocol that connects a microcontroller from software on a host computer. Think of it as a unique language that both Arduino and Raspberry Pi understands. With Firmata, you can directly create a program in the Raspberry Pi to control the Arduino. You’re programming the Arduino with Python now. Isn’t that awesome?
Setting up on the Arduino
The easiest way to setup Firmata on the Arduino is by using the Raspberry Pi. First, we need to install the required software for the job.
1. Connect the Raspberry Pi to the internet. Open command terminal and enter the following command.
sudo apt-get -y install arduino python-serial mercurial
2. Connect your Arduino to the Pi via USB. Open the Arduino IDE, and select the correct port for the device.
3. Next, upload the PyFirmata firmware on the Arduino by opening the sketch from File > Examples > Firmata > Standard Firmata. Finally, click the upload button.
Setting Up on the Raspberry Pi
To prepare the Pi:
1. First, install pyFirmata files using the given github repo:
git clone https://github.com/tino/pyFirmata
2. Then, run the following command:
cd pyFirmata
sudo python setup.py install
Preparing the Hardware
To demonstrate how Firmata works, we will control the Arduino UNO’s GPIO pins with the Raspberry Pi using Firmata. We will interface a button, and an LED so that the LED lights up when the button is pressed. To do that, we need the following components:
- Raspberry Pi
- Arduino UNO
- Breadboard
- Button
- LED
- 220 Ohm Resistor
- Jumper Wires
Connect the components as shown below.
Code
import pyfirmata led_pin = 7
button_pin = 8 board = pyfirmata.Arduino("/dev/ttyACM0") while True: if board.digital[button_pin].read(1): board.digital[led_pin].write(1) else: board.digital[led_pin].write(0)
Code Explanation
First, we import the pyFirmata library using import pyFirmata library
. Then we label the LED and button pins by storing them into variables: led_pin
and button_pin
. To make your program distinguish the Arduino, you must tell it the port wherein it is connected. Use pyfirmata.Arduino()
and then create the instance by assigning the port in the board variable.
The while loop basically acts like void setup()
. We use an if statement to monitor the status of the button. If the button sends a HIGH signal, meaning it was pressed, the LED lights up. Both use the digital[].read
and digital[].write
methods to control the GPIO in the Arduino.
Now save the file with a .py extension. Run the command by typing python plus the filename of your sketch. Now, you can press the button and see your LED light up. Press CTRL + C to stop the program.