Programming Arduino Uno as a binary up-counter using Python’s pyfirmata module

In this tutorial, we are going to learn how to establish a connection between Arduino and Python script to control the Arduino. We can understand this better by this example of implementing a 4-bit binary up-counter using Python script to control Arduino.

Flow of implementation

  • Connect your Arduino board to you desktop/laptop physically via a USB cable.
  • Upload ‘StandardFirmata’ code present in the Examples of Arduino IDE to your board.
  • Install ‘pyfirmata’ library and program your Arduino via a Python script.
  • Make the necessary circuit connections.
  • Run the script and observe the LED’s.

Uploading ‘Standard Firmata’ code to your Arduino Uno

Head over to the Arduino IDE and upload the ‘StandardFirmata’ code under the ‘Firmata’ section of ‘Examples’ within ‘File’ tab as shown in the image below. If this is not available in your ‘Examples’ then make sure to install the ‘Firmata’ package from the ‘Manage Libraries’ section under the ‘Tools’ tab in Arduino IDE.


standard firmata

Note: Select your port number and device type appropriately to upload the code program this code onto your board and note down the port number for the Python Script.

Make necessary circuit connections

In our case, we need to hook up 4 LED’s and 4 current limiting resistors to the digital pins of the Arduino. Rig up the circuit as shown in the image below:



Python script to control the Arduino:

Here the implementation of counter is by checking the 1’s in the binary representation of a number to turn the respective representative LED on. Example a value of decimal 12 in binary is 1100, here we must turn on the 3rd and 4th LED (towards MSB, i.e LED’s connected to pin 10 and 11(MSB) respectively) since the 2nd and 3rd bit are set. Below is a Python code that does this by controlling the Arduino’s digital output pins to create a 4 bit binary up-counter.

    import pyfirmata  # importing pyfirmata module if it already exists
except ImportError:
    import pip  # if pyfirmata module doesnt exist then installing pyfirmata module

    pip.main(['install', 'pyfirmata'])
    import pyfirmata  # importing the module post installation

import time  # importing time to stall the program for generating a delay

board = pyfirmata.Arduino('YOUR_PORT_HERE')  # enter the USB port number where USB have connected your Arduino board
print("Communication Successfully started")

# defining pin modes of pins 8,9,10,11 as output and renaming for ease of access
led1 = board.get_pin('d:8:o')  # least significant bit (LSB)
led2 = board.get_pin('d:9:o')
led3 = board.get_pin('d:10:o')
led4 = board.get_pin('d:11:o')  # most significant bit (MSB)

led_pins = [led1, led2, led3, led4]  # list of all led pins for iterating purpose

while True:  # void_loop() equivalent of Arduino IDE
    for i in range(16):  # binary up-count range from 0 to 15(decimal)
        for bit_index in range(4):  # bit set checking to turn the respective LED on
            if i & (1 << bit_index):
                led_pins[bit_index].write(1)  # 1 for logic HIGH
                led_pins[bit_index].write(0)  # 0  for logic LOW
        time.sleep(0.5)  # delay of 500 milliseconds between any two counts

Note: Run this script via the terminal to avoid any discrepancies


Communication Successfully started

You will now be able to observe the LED’s light up in an order to represent the binary equivalent of the up-counts from 0x0 to 0xF. This is just an example of how Arduino’s can be programmed using Python.

3 responses to “Programming Arduino Uno as a binary up-counter using Python’s pyfirmata module”

  1. Spandana Shekar says:



  2. Shashank S Hegde says:

    Wow !! This one is a cracker.
    Codespeedy you have made this very easy to me. Very helpful Article.

  3. Rohan Harish says:

    Thank you,

    I am glad you liked it.

Leave a Reply

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