Control Arduino with Python and pyFirmata
Last Updated :
28 Apr, 2025
In this article, we will learn how to link an Arduino to a Python script in order to operate the Arduino. This example of constructing a 4-bit binary up-counter using Python script to control Arduino helps us understand this better.
Components Required:
- An Arduino Board (We will be using Arduino UNO, but other similar boards like Arduino Mini, MEGA, or even Node MCU will also work with suitable declarations in the code)
- USB cable for Arduino
- Breadboard
- Jumper wires
- LEDs (we will need 4, but you can try with more)
- 4 resistors of resistance 200-500Ω (any in this range will work)
- An Arduino board.
- Computer with Arduino IDE. (Raspberry Pi 4, 3B+, or even 3B will also work)
Now let us quickly summarise the steps to achieve our Goal. You can directly skip to the required parts by scrolling along:
- Install PyFirmata Module
- Upload "StandardFirmata" to Arduino
- Making the connections
- Write the Python program and Run it
Installing PyFirmata Module
You should have Python and pip Installed in your system. Then you can run the following command to install the PyFirmata module in your system.
pip install pyFirmata
Upload "StandardFirmata" to Arduino
StandardFirmata is a code that helps Python get access to the Arduino board.
First, connect your Arduino to the computer/raspberry pi/laptop using the USB cable.
Know the port name the Arduino is connected to. In windows, the port name will be something like "COMx" (where x is an integer), while in Linux it will be a string starting with "/dev/tty". You might find this information by opening Device Manager in Windows.
Windows Device Manager view
If you are using Linux, please refer to the official Arduino Documentation.
Next, you can open the Arduino IDE and follow the steps to upload the StandardFirmata to the board.
Get StandardFirmata: File -> Examples -> Firmata -> Standard Firmata
Specify Correct Board and Port: Tools -> Board -> Select Arduino UNO (or your own board) -> Tools -> Port -> Select your Port
Upload the StandardFirmata: Click on the upload button to upload the code to Arduino.
Selecting StandardFirmataMaking the connections
Make the connections like the image above. Here I have connected the 4 LEDs to the 13th, 12th, 11th, and 10th pins. There was no specific reason to connect them in that manner. You can use any other digital pin.
Write the Python program and Run it
Python
from pyfirmata import Arduino
from time import sleep
# Connecting to the board
board = Arduino('COM8')
# initializing the LEDs
led1 = board.get_pin('d:13:o')
led2 = board.get_pin('d:12:o')
led3 = board.get_pin('d:11:o')
led4 = board.get_pin('d:10:o')
# wait for 1s at every count value
wait = 1
# initialise all to False (off)
val_1 = val_2 = val_3 = val_4 = False
# led4 is the least significant bit and led1 is the most significant bit
while True: # this is an infinite loop which won't end untill the terminal is killed
for ____ in range(2):
for ___ in range(2):
for __ in range(2):
for _ in range(2):
sleep(wait)
# Updating the values and printing them
led1.write(val_1)
led2.write(val_2)
led3.write(val_3)
led4.write(val_4)
print(int(val_1), int(val_2), int(val_3), int(val_4))
val_4 = not val_4
val_3 = not val_3
val_2 = not val_2
val_1 = not val_1
print("\n\n")
C++
// C++ code
int led1 = 0;
int led2 = 0;
int led3 = 0;
int led4 = 0;
int counter;
int counter2;
int counter3;
int counter4;
void setup()
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
Serial.begin(9600);
}
void loop()
{
for (counter4 = 0; counter4 < 2; ++counter4) {
for (counter3 = 0; counter3 < 2; ++counter3) {
for (counter2 = 0; counter2 < 2; ++counter2) {
for (counter = 0; counter < 2; ++counter) {
delay(1000);
digitalWrite(13, led4);
digitalWrite(12, led3);
digitalWrite(11, led2);
digitalWrite(10, led1);
Serial.print(led4);
Serial.print(led3);
Serial.print(led2);
Serial.println(led1);
led1 = 1 - led1;
}
led2 = 1 - led2;
}
led3 = 1 - led3;
}
led4 = 1 - led4;
}
Serial.println();
}
Output:
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
...
Here is the Simulation Output!
simulation GIF
Similar Reads
Introduction to Python Pydantic Library In modern Python development, data validation and parsing are essential components of building robust and reliable applications. Whether we're developing APIs, working with configuration files, or handling data from various sources, ensuring that our data is correctly validated and parsed is crucial
6 min read
Django Sign Up and login with confirmation Email | Python Django provides a built-in authentication system that handles users, login, logout, and registration. In this article, we will implement a user registration and login system with email confirmation using Django and django-crispy-forms for elegant form rendering.Install crispy forms using the termina
7 min read
Creating Your Own Python IDE in Python In this article, we are able to embark on an adventure to create your personal Python Integrated Development Environment (IDE) the usage of Python itself, with the assistance of the PyQt library. What is Python IDE?Python IDEs provide a characteristic-rich environment for coding, debugging, and goin
3 min read
PyQt5 Input Dialog | Python PyQt5 provides a class named QInputDialog which is used to take input from the user. In most of the application, there comes a situation where some data is required to be entered by the user and hence input dialog is needed. Input can be of type String or Text, Integer, Double and item.Used methods:
2 min read
How to handle alert prompts in Selenium Python ? Seleniumâs Python Module is built to perform automated testing with Python. Alerts are a way to show popups in the browser for either accepting data or displaying data. Selenium provides methods to handle alerts of all kinds. Seleniumâs Python Module is built to perform automated testing with Python
2 min read
Make Python Wait For a Pressed Key Halting the execution until the occurrence of an event is a very common requirement in prompt-based programs. In earlier times, the presence of functions such as getch in C/C++ was almost necessary for the code to make the console stay up to display the output. Now such shortcomings of the compiler
3 min read
Create a Credential file using Python A credential file is nothing but just a configuration file with a tad bit of encryption and an unseen security structure in the backend. There might be a situation where you might come across these kinds of files while using some kind of cloud platforms. All you do to login to the instance or give t
5 min read
How to automate system administration with Python Python has become one of the most popular programming languages for system administrators due to its simplicity, flexibility, and extensive support for various system management tasks. Whether you're automating repetitive tasks, managing files and directories, or handling user permissions, Python pr
5 min read
How to make a voice assistant for E-mail in Python? As we know, emails are very important for communication as each professional communication can be done by emails and the best service for sending and receiving mails is as we all know GMAIL. Gmail is a free email service developed by Google. Users can access Gmail on the web and using third-party pr
9 min read
Python | Prompt for Password at Runtime and Termination with Error Message Say our Script requires a password, but since the script is meant for interactive use, it is likely to prompt the user for a password rather than hardcode it into the script. Pythonâs getpass module precisely does what it is needed. It will allow the user to very easily prompt for a password without
3 min read