Programming Arduino using Python

In this tutorial we show how to write Python program for Arduino. For illustration purpose we will write Python Program to blink a LED connected to Arduino. In the process you will learn how to install firmata protocol on Arduino and what python libraries you need to use firmata protocol, how to install the pySerial and pyFimata libaries.

Programming Arduino using Python is just as easy as programming arduino with other coding languages. This tutorial teaches you how to program arduino using python programming languages. For illustration purpose a simple python program to turn off and on a LED connected to Arduino UNO. Along the way, you will learn what software program and python modules you need to install in order to program arduino using python. How to compile and upload the compiled code to Arduino.

First of all you need to understand Firmata protocol.

The firmata protocol is a standard that allows you to interact microcontroller and software program. Any microcontroller and any software running in your computer can be bridge using Firmata protocol. In our case the microcontroller is the Arduino UNO microcontroller ATmega328P and the software is the Python program.

So how do you bridge python software and Arduino microcontroller using Firmata protocol? You need to first tell Arduino microcontroller that we will be using firmata protocol and you do this by burning firmata program into Arduino microcontroller. In other words, the first thing you need to do is to install Firmata protocol into the Arduino Microcontroller. You do this by uploading a sketch called StandardFirmata using Arduino IDE into Arduino. Once you have done that, Arduino is ready to accept connection from other Program or Software like Python that supports serial communication. This can think this as the first part of the process of writing python program to Arduino.

Now the software which in our case is python must have support for two things- first a serial communication support with devices like Arduino and second support for firmata protocol. For serial communication purpose, Python has pySerial library/module that can communicate to microcontroller using serial communication. And for the support of firmata protocol there is library called pyFirmata.

Now once we have installed firmata protocol into Arduino and also installed pySerial and pyFirmata libraries you are good to go to write python program for arduino. But in this tutorial before writing any python program we will show one step in between to teach you how to test whether firmata protocol was installed on the Arduino. Then we write the python program which will be a program that will continuously blink LED on and off at pin 6.

This tutorial assumes you have already installed Arduino IDE and you have some experience with programming Arduino. If not, you can see some Arduino tutorials here. In this tutorial windows 7 OS has been used but the process follows similar in Linux or MAC.

Steps for Programming Arduino using Python

Step 1: Install Firmata Protocol into Arduino

Connect Arduino USB to your computer. Then open Arduino IDE. Check board and port are correctly configured. Now open the StandardFirmata sketch by going to File > Examples > Firmata > StandardFirmata.

Arduino Firmata Protocol install

Now compile and upload the StandardFirmata Sketch.

compile and upload standardfirmata

At this point your Arduino Microcontroller is programmed with Firmata protocol which means it is ready to accept connection via Program/Software.

Step 2. Testing Arduino Firmata

Although this programming arduino using python step is optional, it is good to know how to test Firmata protocol on your microcontroller. This is optional because it has nothing to do with programming Arduino using python programming language.

To test whether your microcontroller will accept any external software commands because of firmata protocol installed on the microcontroller we will use download and install a test program.

Go to and look for Firmata Test Program section. There you will find download link of the test program for various operating system. In this example we are using windows OS so we download the windows supported test program.

Download and install this program. Before we test the firmata protocol using the test program we will create a simple LED blinking circuit with the Arduino. Connect the positive side of the LED to the pin 6 and connect the negative side to the ground via 220Ohm resistor.

This is shown below.

single led connection for testing firmata

Now open the firmata_test.exe that you downloaded previously. When it is open go to Port and select your com port, which in this case is COM3. This is shown below.


Now change the Pin 6 to HIGH by double clicking on the button that says LOW.

Watch the LED and you should see that it is now turned ON.

If this test program is working, the LED is turning ON on the button click then it is confirmed that the Arduino microcontroller is programmed with firmata protocol and you can use software like python to program the arduino microcontroller.

Note: Optionally, you can change the pin 6 pin mode to PWM and then use the slider to change the brightness of the LED. This is similar to tutorial How to use arduino analogWrite function for PWM and Arduino Simulink Tutorial 3 – PWM.

Step 3: Install pySerial and pyfirmata

In this tutorial we used the Spyder python IDE that comes bundled with Python Anaconda. To install pySerial library type !pip install pyserial in the python console as shown below and the pyserial library should get installed without any problem.

Next install pyFirmata using !pip install pyfirmata from the python console.

Step 4: Programming Arduino using Python

This is the final step where we will write a python program to blink the LED connected to pin 6.

After you have installed pySerial and pyFirmata libraries in your Python Software you are now ready to write your first Python test program for arduino.

Create a new python script and save it with some name like Then write the following python code:

Explaining this LED blinking program will take some space so it is explained in great details in another Programming Arduino using Python tutorial Blinking LED python program for Arduino.

Once you have written the program, Run the program.

If there is no error you should see the LED blinking on pin 6. The following video shows this.

Video demonstration of Programming Arduino using Python

That’s it! Now you know how to write Python program for Arduino (see also How to read and write to Arduino using Python).

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

Your email address will not be published. Required fields are marked *

Real Time Oscilloscope using Simulink and Arduino

Real Time Oscilloscope using Simulink and Arduino

single led connection for testing firmata

Blinking LED python program for Arduino