Arduino Standard Firmata Tutorial

This tutorial is an introduction to Arduino Firmata, with a focus on Standard Firmata.

First I’ll explain to you when and why to use Firmata for Arduino. After that, you’ll see how to quickly setup Standard Firmata on your Arduino. And then you’ll create a client on your computer to actually communicate and give commands to your Arduino.

For this tutorial I’ll use an Arduino Uno board.

Why using Arduino (Standard) Firmata?

The need for communication

First of all, if your Arduino application is a program that doesn’t need to communicate with any other device, then you won’t need to use Firmata.

Now, let’s say you want to control your Arduino from another machine – here we’ll simply use your computer. You want all the logic of your program to be on your computer, while the Arduino board only executes orders (actuates hardware pins), and gives back some information (reads hardware pins). Basically you want to make your Arduino more “dynamic”, and export the “brain” of the Arduino into your computer. Put in other words, you want to write the firmware logic on your computer, not on the Arduino.

In this case you need to implement some sort of communication. The most straightforward communication is Serial via USB. That’s probably what you’ve used when learning some Arduino basics. And it works fine. You can send messages to the Arduino (bytes, int, string, etc) and also receive messages.


You are learning how to use Arduino to build your own projects?

Check out Arduino For Beginners and learn step by step.


Now, Arduino Firmata is a layer added on top of Serial communication. The library uses the Firmata protocol which communicates over Serial.

Arduino Firmata is kind of a high level interface to simplify things, and makes writing code easier and quicker for you. You could write your own library based on Serial, or you could use Firmata.

What is StandardFirmata for Arduino?

In this tutorial we’ll focus on Standard Firmata. This is a sketch based on the Firmata library (based on the Firmata protocol) which will allow you to control all Arduino pins from your computer. You’ll be able to change a pin’s mode, state, read the pin, etc.

And all that without writing any code on the Arduino side. It’s already done for you.

The Arduino will act like a server (receives requests and sends data), and your computer will act like a client (sends requests and receives data).

Arduino Standard Firmata Schema

For example, if you want to set pin 13 to OUTPUT, and set its state to HIGH, then that’s something you’ll do in your computer using a Firmata client. The command will be sent to Arduino which will actually execute it.

Install StandardFirmata on Arduino

Let’s upload the StandardFirmata library on your Arduino board.

First, make sure you’ve installed the Arduino IDE on your computer. You can download the Arduino IDE from the official Arduino download page. Choose the system (Windows, Mac, Linux) you’re currently using, download, and execute the installer. If you already have Arduino IDE installed on your computer, make sure the version is recent (update if needed).

Now, open your Arduino IDE, and connect your Arduino board to your computer. Select the proper board under “Tools” > “Boards” (here Arduino Uno), and the port where the board is connected. On Windows, you’ll often see something as COMX. It can vary depending on your system, and what else is plugged to your computer.

Arduino IDE - Select board and port

Now that you have your Arduino board correctly detected by the Arduino IDE, you can open the StandardFirmata sketch, from the available examples.

Arduino StandardFirmata Sketch

If you can’t see “Firmata” under “Examples” it means that the Firmata library is not installed. It also surely means that your Arduino IDE is not up to date, since for recent releases, Firmata is automatically included. So, you can either update your Arduino IDE, or check under “Sketch” > “Include Library” > “Manage Libraries…”.

Arduino IDE Manage Libraries

Write “firmata” in the search bar, and you’ll find the Firmata library (Firmata Built-In by Firmata Developers). If not installed, select the latest version and click on the “Install” button.

Arduino Library Manager - Find Firmata Library

Now that you have Firmata on your Arduino IDE, select the StandardFirmata sketch example. This will open a new window with a pretty long program. Actually you don’t need to read this program. All you have to do it to upload it to your Arduino board.

Click on the “Upload” button.

Now, Firmata is running on your Arduino. That’s all you need to do on the Arduino side!

Test Arduino Firmata without any code

You can download a simple program to test if your Arduino is correctly working with StandardFirmata.

Go to this Firmata wiki page, scroll under “Firmata Test Program”, and download the program accordingly to your computer system.

No need to install it, you can just click on it to launch it. Don’t forget to keep your Arduino board connected to your computer with an USB cable.

StandardFirmata Test Software for Arduino

At first you won’t see any of the pins. Click on “Port”, and select the port you’re using for Arduino (in my case: COM4 on Windows 10). Note: if you’re using Serial communication with another program, first close the communication before you can use it here. Also, if you want to upload something new to your board, make sure you first close this test program, because the upload mechanism also uses Serial.

So, once the communication is setup, you can interact with all available pins on the Arduino!

The most simple test you can do here is to set pin 13 as OUTPUT, and set its state to HIGH. Pin 13 on Arduino Uno is connected to the built in LED. So, by clicking on HIGH and then LOW, you should see the LED blink on your board.

Write a client program with pyfirmata

Setup pyfirmata

Using the GUI test program is nice, but not quite practical if you want to control the Arduino from your own programs.

As for now you’ve only see the server side: A sketch uploaded to the Arduino board, which uses the Firmata protocol.

Now, to talk to your board, you need to write some sort of client code on your computer.

Basically you have to communicate using the Firmata protocol over Serial communication. That can sounds quite difficult to do at first, but don’t worry, there are also client libraries available for you that will handle all the communication out of the box!

For example, pyfirmata is a famous Python library to use Firmata as a client. There are libraries available in many languages (list here), but for this tutorial we’ll only focus on writing a Python client.

To install pyfirmata:

$ pip3 install pyfirmata

Basic code with pyfirmata

Here we’ll simply try to make the Blink example (blink LED 13 every second) using Python and the pyfirmata module.

#!/usr/bin/env python3

from pyfirmata import Arduino
import time

if __name__ == '__main__':
    board = Arduino('YOUR_PORT_HERE')
    print("Communication Successfully started")
    
    while True:
        board.digital[13].write(1)
        time.sleep(1)
        board.digital[13].write(0)
        time.sleep(1)

Let’s break down this code line by line:

#!/usr/bin/env python3

from pyfirmata import Arduino
import time

First we import the Arduino functionalities from pyfirmata.

if __name__ == '__main__':
    board = Arduino('YOUR_PORT_HERE')
    print("Communication Successfully started")

To initiate the communication, simply use the Arduino() function from pyfirmata, to which you give one parameter: the port you’re using for your Arduino board. This is the same port you used to upload the program to your board.

Once the communication is successfully started (it can take some time), we display a log line on the terminal.

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

And here we have the blink LED Python code. The code structure is exactly the same as the blink LED example in Arduino language.

When writing board.digital[13].write(1), for example, pyfirmata will create a request using the Firmata protocol. It will send it to the Arduino via Serial communication. The Arduino board will analyse this request and translate it to digitalWrite(13, HIGH);.

Now, simply run your Python code and see the built-in LED blink on your Arduino board!

You can find all the available functions on the pyfirmata documentation website.

Going further with Arduino Firmata

With this tutorial you should have a simple, but good overview of how to use Firmata with Arduino. It’s quite basic, but with that knowledge you can start developing more complex projects and applications.

Any application that requires a lot of computation power, as well as hardware handling, is a possible candidate for using Firmata. The “brain” board/machine will handle all the logic, and give appropriate commands to the hardware actuator, in this case the Arduino board.

You can go further by:

Also, note that Firmata is not the answer to all problems. In some applications which require communication, you’ll find that a custom Serial communication, or a different protocol (custom or not) might be more appropriate. How will you know that? Practice, and practice, and more practice!

Did you find this tutorial useful?

Do you want to learn Arduino from scratch?

If yes, this course is for you:

Arduino For Beginners - Complete Course

>> Arduino Programming For Beginners <<