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
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read