Binary Counter with MicroPython Code on Raspberry Pi Pico

This counter counts numbers from 0 to 31 in binary format and displays them by 5 LEDs.

For converting decimal numbers to binary numbers, and displaying them by LEDs, we can use the following table:

Decimal NumberBinary Number LED5LED4LED3LED2LED1
00offoffoffoffoff
11offoffoffoffon
210offoffoffonoff
311offoffoffonon
4100offoffonoffoff
5101offoffonoffon
6110offoffononoff
7111offoffononon
81000offonoffoffoff
91001offonoffoffon
101010offonoffonoff
111011offonoffonon
121100offononoffoff
131101offononoffon
141110offonononoff
151111offonononon
1610000onoffoffoffoff
—–—–

From this table you can see that binary sum can be done as follows:

1+0=1
0+1=1
0+0=0
1+1=10

For example the sum of 4+7 is given by:

 100
+111  
1011

So in this project we need to start from 0 and add binary number 1 to it step by step, and at every step turn LEDs on/off accordingly. For this purpose 5 LEDs are connected to 5 GPIO pins of Raspberry Pi Pico through 5 numbers of 220 ohms resistors. MicroPython code for this project is available here, and in this video you can see how it works:

Binary Counter Using the Raspberry Pi Pico W: This counter counts from 0 to 31 and represent them by 5 LEDs.

MicroPython code for this project:

# This program counts binary numbers from 0 to 31 and represents them using 5 LEDs
from machine import Pin
#imports Pin method from machine library
from time import sleep
#imports sleep method from time library

# Creating LED objects using Pin method:

LED1=Pin(15, Pin.OUT)
LED2=Pin(14, Pin.OUT)
LED3=Pin(13, Pin.OUT)
LED4=Pin(12, Pin.OUT)
LED5=Pin(11, Pin.OUT)


# variables used for storing the values of binary numbers:
V1=0
V2=0
V3=0
V4=0
V5=0


# variables used for storing temporary values of binary numbers:
n1=0
n2=0
n3=0
n4=0

n5=0

while True:

if (V1==0):
n1=1
if (V1==1):
n1=0
if (V2==0):
n2=1

if (V2==1):
n2=0
if (V3==0):
n3=1
if (V3==1):
n3=0
if (V4==0):
n4=1
if (V4==1):
n4=0
if (V5==0):
n5=1
if (V5==1):
n5=0


V1=n1
V2=n2
V3=n3
V4=n4
V5=n5
LED1.value(V1)
LED2.value(V2)
LED3.value(V3)
LED4.value(V4)
LED5.value(V5)
print(V5,V4,V3,V2,V1)

sleep(1) # wait 1 second

For any question about this project you can contact me using the following form or by leaving a comment on YouTube.

Submit your inquiry: