What is Firmata firmware
Firmata is a generic firmware protocol (often installed as StandardFirmata on Arduino) that allows a microcontroller to be controlled directly by a host computer or device, such as a Raspberry Pi, laptop, or phone. It eliminates the need to write custom C++ code on the board, It enabling users to control pins directly using language like Python
Once Firmata is installed, the Arduino waits for commands coming from the computer through USB, Bluetooth, or Wi-Fi, and immediately executes them, such as turning LEDs on or off, reading sensor values, controlling motors, or checking button inputs.
That means you can write your main program in languages like Python, JavaScript, Java, or C#, and use that program to control the Arduino live, making it extremely useful for rapid prototyping, robotics, IoT projects, testing, and building applications with graphical interfaces
How to Install Firmata firmware
Install the Arduino IDE: Download the latest version from the official Arduino website.
Connect Your Board: Plug your Arduino (Uno, Mega, Nano, etc.) into your computer using a compatible USB cable.
Selection : Now you need to select board and port
2) Upload the Firmata Firmware
Open The File : File → Examples → Firmata → StandardFirmata.
Note: If you need specific features like Wi-Fi or Bluetooth, you can choose variants like StandardFirmataWiFi or StandardFirmataBLE.
Upload: Click the Upload button (the right-pointing arrow) at the top of the editor.
Confirm: Wait for the status bar at the bottom to display "Done Uploading
3) Install pyfirmata: After upload the firmata firmware in arduino board.Now you need to insatll pyfirmata
Use cmd: pip install pyfirmata and pip install pyserial
Pyfirmata: used for control the Arduino board pins
Pyserial : used for serial communication
Step 1: File → Examples → Firmata → StandardFirmata
Step 2: upload the firmata program in Arduino board
Step 3: Python library install: pip install pyfirmata
Step 4: Python code (direct Arduino control)
Complete process of activation the Firmata
Arduino | Python (Firmata) |
pinMode() | pin.mode |
digitalWrite() | pin.write() |
digitalRead() | pin.read() |
analogRead() | analog[x].read() |
analogWrite() | pin.write(0.0-1.0) |
delay() | time.sleep() |
Servo.write() | pin.write(angle) |
Difference between Arduino and Firmata/Python
from pyfirmata import Arduino
board = Arduino('COM3') # COM port l
How to connect Arduino board
1) First program: Arduino LED blink
from pyfirmata import Arduino
import time
board = Arduino('COM3')
while True:
board.digital[13].write(1)
time.sleep(2)
board.digital[13].write(0)
time.sleep(2)
First.py
Python pyfirmata
board.digital[13].write(1) # ON
board.digital[13].write(0) # OFF
Arduino board
digitalWrite(13, HIGH);
digitalWrite(13, LOW);
Digital Output (LED ON/OFF)
Pin Mode
board.digital[13].mode = 1 # OUTPUT
0 = INPUT
1 = OUTPUT
2 = ANALOG
3 = PWM
4 = SERVO
Note: You can change mode according to your program
pip install pyfirmata
2) How to display the message form python program to Arduino serial communication
import serial
import time
arduino = serial.Serial('COM3', 9600)
#time.sleep(2)
msg = input("Enter Message : ")
arduino.write(msg.encode())
print("Sent:", msg)
Display.py
pip install pyserial
import serial
import time
arduino = serial.Serial('COM3',9600)
time.sleep(2)
while True:
data = arduino.readline().decode('utf-8', errors='ignore').strip()
print("Button State:", data)
3) Check 0 and 1 state by using button
Note 1: strip() 🡺 it remove the default \n and \r from python
Note 2: decode()🡺 it convert mesg in human readable
Note 3: arduino.readline()🡺 read complete line from Arduino board
void setup()
{
pinMode(2, INPUT_PULLUP);
� pinMode(13, OUTPUT);
� Serial.begin(9600);
}
�void loop()
{
int button = digitalRead(2);
� Serial.println(button);
� if(button == 0)
{
digitalWrite(13, HIGH);
}
� else
{
digitalWrite(13, LOW);
}
� delay(200);
}
4) Button state 0 and 1 with LED blink
import serial
import time
arduino = serial.Serial('COM3',9600)
time.sleep(2)
while True:
data = arduino.readline().decode('utf-8', errors='ignore').strip()
print("Button State:", data)
Note 1: first upload Arduino code after that use python program and run
Because python only control the pins
Step 1 run this program in Arduino ide
Step 2 run this program in python IDLE
5) Multiple leds blink program
from pyfirmata import Arduino
import time
board = Arduino('COM3')
leds = [8, 9, 10, 11, 12] #ledpins
while True:
# Forward
for pin in leds:
board.digital[pin].write(1)
time.sleep(0.5)
board.digital[pin].write(0)
# Reverse
for pin in reversed(leds):
board.digital[pin].write(1)
time.sleep(0.5)
board.digital[pin].write(0)
Note1: first upload standardfirmata program in Arduino after that run python program