- Python Programming for Arduino
- Pratik Desai
- 1819字
- 2025-04-04 21:08:18
Introducing the Firmata protocol
Before Arduino, the domain of microcontroller-based applications was limited to hardware programmers. Arduino made it simple for developers that came from other software fields and even for the non-coding community to develop microcontroller-based hardware applications. Arduino consists of a simple hardware design with a microcontroller and I/O pins to interface external devices. If one can write an Arduino sketch that can transfer the control of the microcontroller and these pins to an external software mechanism, then it will reduce one's efforts to upload Arduino sketches for every modification. This process can be performed by developing such an Arduino program that can then be controlled using a serial port. There exists a protocol called Firmata, which does exactly that.
What is Firmata?
Firmata is a generic protocol that allows communication between the microcontroller and the software that is hosted on a computer. Any software from any computer host that is capable of serial communication can communicate with the microcontroller using Firmata. Firmata gives complete access of Arduino directly to the software and eliminates the processes of modifying and uploading Arduino sketches.
To utilize the Firmata protocol, a developer can upload a sketch that supports the protocol to the Arduino client as a onetime process. Afterwards, the developer can write custom software on the host computer and perform complex tasks. This software will provide commands via a serial port to the Arduino board that is equipped with Firmata. He or she can keep altering the logic on the host computer without interrupting the Arduino hardware.
The practice of writing custom Arduino sketches is still valid for standalone applications where the Arduino board has to perform a task locally. We will explore both these options in the upcoming chapters.
Note
You can learn more about the Firmata protocol and its latest version from the official website at http://www.firmata.org.
Uploading a Firmata sketch to the Arduino board
The best way to start testing the Firmata protocol is to upload a standard Firmata program to the Arduino board and use the testing software from the host. In this section, we are going to demonstrate a method to upload an Arduino sketch, which has this standard Firmata program, to the board. This is going to be the default method to upload any sketch in the future.
Implementation of the Firmata protocol requires the latest version of the Firmata firmware and you don't have to worry about writing it. The latest Arduino IDE ships with a standard version of the Firmata firmware, and we recommend that you use the latest IDE to avoid any conflict. Now, follow the following steps to upload the program to your Arduino board:
- As shown in the following screenshot, open the StandardFirmata sketch by navigating to File | Examples | Firmata | StandardFirmata in the Arduino IDE:
- This action will open another sketchbook in a new window with the StandardFirmata sketch loaded in the editor. Do not modify anything in the sketch and go ahead with the compiling process that is described in the next step. It is important not to modify anything in the code as the test software that we are going to use complies with the latest unchanged firmware.
- Once the StandardFirmata sketch is opened, the next step is to compile it for your Arduino board. In the previous section, we already connected the Arduino board to the computer and selected the proper serial port. However, if the new sketchbook has a different configuration than that, follow the steps from the previous section, that is, select the appropriate serial port and the Arduino board type.
- To compile the current sketch, click on the Verify icon from the toolbar as displayed in the following screenshot. You can also compile it by navigating to Sketch | Verify / Compile or clicking on Ctrl + R (command + R if you are using Mac OS X):
The compilation process should complete without any errors as we are using default example code from the IDE itself. Now it's time to upload the sketch to the board. Make sure that you have connected the board.
- Press the upload icon in the toolbar as displayed in the following screenshot. This action will upload the compiled code to your Arduino board:
On completion, you should see the Done uploading. text in the IDE, as displayed in the following screenshot:

Your Arduino board is now ready with the latest Firmata firmware and is waiting for a request from your computer. Let's move on to the next section and start testing the Firmata protocol.
Testing the Firmata protocol
In the previous chapter, we used an on-board LED at pin 13 to test the Blink program. This time, we are going to use an external LED to get you started with the assembly of hardware components using your Arduino board. As all the upcoming exercises and projects will require you to interface hardware components such as sensors and actuators to your Arduino board using a breadboard, we want you to start getting hands-on experience with wiring these components.
Now is the time to use the LED that we asked you to get at the beginning of the chapter. Before we start wiring the LED, let's first understand the physics of it. The LED that you obtained should have two legs: a short one and a long one. The short leg is connected to the cathode of the LED and it needs to be connected to the ground via a resistor. As you can see in the following figure, we are using a 1 k-ohm resistor to ground the cathode of the LED. The long leg, which is connected to the anode, needs to connect to one of the digital pins of the Arduino board.
As shown in the following figure, we have connected the anode to the digital pin number 13. Look at the figure and wire the connection as displayed. Make sure that you disconnect the Arduino board from the host computer to avoid any kind of damage from static electricity.

In this example, we are going to use an LED to test some basic functionalities of the Firmata protocol. We have already uploaded the Firmata code to the Arduino board and we are ready to control the LED from the host computer.
There are multiple ways to communicate with the Arduino board from the host computer using Firmata, such as writing your own program in Python using the supported library or using the prebuilt testing software. Starting from the next section, we are going to write our own programs to use Firmata, but at this stage, let's use a freely available tool for testing purposes. The official Firmata website, http://www.firmata.org, also provides test tools that you can download from the Firmata Test Program section on the main page. The website includes a different variant of the tool called firmata_test
for different operating systems. Using the following steps, you can test the implementation of the Firmata protocol:
- Download the appropriate version of the
firmata_test
program to your computer. - Now, connect your Arduino board with the LED to the host computer using the USB cable and run the downloaded
firmata_test
program. You will be able to see an empty window on the successful execution of the program. - As displayed in the following screenshot, select the appropriate port from the drop-down menu. Make sure to select the same port that you used to upload the Arduino sketch.
- Once you select the Arduino serial port, the program will load multiple drop-down boxes and buttons with labels that contain the pin number. You can see in the following screenshot that the program is loaded with 12 digital pins (from pin 2 to pin 13) and six analog pins (from pin 14 to pin 19). As we are using the Arduino Uno board for our applications, the test program only loads pins that are part of Arduino Uno. If you are using Arduino Mega or any other board, the number of pins displayed in the program will be according to the pins supported by that particular variant of the Arduino board.
Tip
Working with the firmata_test program on Linux
On a Linux platform, you might have to modify the property of the downloaded file and make it executable. From the same directory, run the following command in the terminal to make it executable:
$ chmod +x firmata_test
Once you have changed the permissions, use the following command to run the program from the terminal:
$ ./firmata_test
- As you can see in the program window, you have two other columns as well as the column containing the labels. The second column in the program lets you select the role for the appropriate pins. You can specify the role of digital pins (in the case of Arduino Uno, from 2 to 13) as input or output. As displayed in the following screenshot, you will see Low in the third column as soon as you select the role of pins 2 and 3 as input pins. This is correct, as we don't have any input connected to these pins. You can play with the program by changing the roles and values of multiple pins.
As we have connected the LED to digital pin 13, we are not expecting any physical changes on the board while you are playing around with the other pins.
- Now, select pin 13 as an output pin and press the Low button. This will change the button's label to High and you will see that the LED is turned on. By performing this action, we have changed the logic of the digital pin 13 to 1, that is, High, which translates to +5 volts at the pin. This potential will be sufficient to light the LED. You can change the level of pin 13 back to 0 by clicking on the button again and turning it to Low. This will change the potential back to 0 volts.
The program that we used here is perfect to test the fundamentals, but it cannot be used to write complex applications using the Firmata protocol. In real-world applications, we really need to execute the Firmata methods using custom code, which in addition to switching the LED status also includes the implementation of smart logic and algorithms, interfacing other components, and so on. We are going to use Python for these applications, starting from the next section.