Tested on:Raspberry Pi 2 and B+; Arduino Nano v3.1; Raspbian Jessie
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)
Hardware:Arduino (nano)
Cable to connect the Arduino to the RPi USB port
Breadboard and LED with fitting resistor(Vin ~= 3.0V)
Screen, mouse and keyboard

Raspi + Ardi = Big Love: PyFirmata Introduction

PyFirmata by Tino de Bruijn is an amazing tool for combining the powers of the Raspberry Pi(full operating system, python, …) and the Arduino (high time precision, …). Accessing and controlling an Arduino from a Raspberry Pi is quite easy with PyFirmata. In this tutorial, a python script on a Raspberry Pi controls an LEDs brightness using an hardware-pulse-width-modulation(pwm) pin of the Arduino.

Setup PyFirmata
First, the Arduino software and PyFirmata need to be installed. Connect the Raspberry Pi to the internet and the peripherals (or use a VPN), then enter

Now, connect the Arduino to the running Raspberry Pi, start the Arduino IDE by typing arduino in the terminal and select the serial port and board type like in the Arduino introduction (note the USB port found!). Now, upload the default PyFirmata firmware on the Arduino by clicking File, then Examples, then Firmata, then Standard Firmata (see screenshot below).



Then, click the upload button.

PyFirmata Hello World
Download the script from GitHub with

Make sure the USB port in the following line from the script matches the value found in the last paragraph:

Then, connect the pin D3 of the Arduino to a LED and a fitting resistor on the breadboard like this:

PyFirmata test circuit

Arduino nano with LED

Now, start the script py typing
The glow of the LED will increase slowly until the full capacity is reached, like in this clip:

Code explained
After setting up the board, starting an iterator thread on the Arduino is required for pwm:

The output pin is selected with

which means digital pin 3 with pwm.
Then, it iterates over 10000 STEPS, from 0.0 to 1.0, the maximum value for a pwm write:

Click on Full Script to show all of the code.

Closing PyFirmata
As of now, PyFirmata has a bug and sometimes doesn’t quit program execution normally. To close PyFirmata anyways, press Ctrl+z to move it in the background. Then, enter

to display the process ID:

Kill PyFirmata

Kill PyFirmata

With this process ID, simply enter
sudo kill -9 ID
to quit PyFirmata.

Easy Arduino board selection
Tino de Bruijn hinted me at using util.get_the_board for accessing the Arduino without manually passing the USB port. Replace the line

in the script with

This does not require the USB identifier ttyUSBX and is stable for removing and reattaching an Arduino.

Checkout the PyFirmata documentation for more info!

PyFirmata on GitHub: https://github.com/tino/pyFirmata
Arduino Introduction: http://www.knight-of-pi.org/arduino-mini-introduction-linux/
PyFirmata Documentation WIP (PDF): https://media.readthedocs.org/pdf/pyfirmata/latest/pyfirmata.pdf

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.


  1. Hello
    How can I generate 3 PWM Signals with fixed ratio (4 %, 25 %, 46%

    • Not sure i know what you want to do, but how about
      d3 = board.get_pin(‘d:3:p’)
      d4 = board.get_pin(‘d:4:p’)
      d5 = board.get_pin(‘d:5:p’)

Leave a Reply to Salim Cancel reply

Time limit is exhausted. Please reload CAPTCHA.