Last mod: 2024.12.30

Firmata Library - Raspberry Pi and Arduino cooperation

Looking to combine the flexibility of Arduino boards with the power of a Raspberry Pi? The Firmata library provides an easy-to-use protocol for communicating between the two, allowing you to harness the best of both worlds. By leveraging Firmata, you can remotely control Arduino's I/O pins directly from the Raspberry Pi, simplifying complex projects and reducing development time. This approach is ideal for IoT, robotics, and sensor-driven applications, offering improved performance, versatility, and streamlined workflows.

Arduino

We can use almost any Arduino. However, this may require small modifications to the Python script. In the example, I use the original Arduino Uno.

Install Arduino IDE

Install the Arduino IDE from https://www.arduino.cc/en/software

Install Firmata on Arduino

Launch the Arduino IDE and:

  • Go to Sketch > Include Library > Manage Libraries.
  • In the Library Manager, search for Firmata.
  • Click Install next to the latest version of the "Firmata" library.
  • Repeat instalation for Servo library.

creenshot-Firmata

creenshot-Servo

Connect Arduino to PC with Arduino IDE. Open the StandardFirmata example:

  • Go to File > Examples > Firmata > StandardFirmata. This sketch implements the full Firmata protocol, making your Arduino ready to communicate.
  • Select valid Arduino model Tools > Board, in examle is "Arduino Uno"
  • Select valid communication port Tools > Port, in my example (Ubuntu) is /dev/ttyACM0.
  • Press the “Upload” button, this will compile and upload the code to the Arduino.
  • We can disconnect the Arduino.

Raspberry Pi

Connect the Arduino with a USB cable to the Raspberry.

Raspberry Pi and Arduino Uno connected

Install Python 3.7

Important! Firmata does not work properly with all versions of Python and therefore we need to install exactly version 3.7. The process of compiling and installing Python is described at https://installvirtual.com/install-python-3-7-on-raspberry-pi/

In simple terms, use bash commands:

sudo apt-get update
sudo apt-get install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
sudo tar zxf Python-3.7.0.tgz
cd Python-3.7.0
sudo ./configure
sudo make -j 4
sudo make altinstall

In file:

vi ~/.bashrc

add Python path:

alias python='/usr/local/bin/python3.7'

Reload configuration:

source ~/.bashrc

Let's verify the Python version:

python3 --version

creenshot-Python

Install Firmata on Raspberry Pi

If everything is ok then we proceed to install the environment and the needed packages:

sudo apt install python3-virtualenv
virtualenv firmata_env
cd firmata_env/
source bin/activate
sudo python -m pip install pyserial pyfirmata

We need to check under what name the connection to the Arduino is available:

ls /dev/tty*

creenshot-tty

In our example, it is /dev/ttyACM0. We create a simple script that flashes the LED soldered into the Arduino:

blinking_led13.py

We add the code to the file:

from pyfirmata import Arduino, time
board = Arduino('/dev/ttyACM0')

while True:
   board.digital[13].write(1)
   time.sleep(1)
   board.digital[13].write(0)
   time.sleep(1)

And we are launching:

python blinking_led13.py

After a few seconds, the LED should start flashing.

Links

https://www.arduino.cc/en/software
https://docs.arduino.cc/retired/hacking/software/FirmataLibrary/
https://installvirtual.com/install-python-3-7-on-raspberry-pi/
https://stackoverflow.com/questions/74585622/pyfirmata-gives-error-module-inspect-has-no-attribute-getargspec