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 Number | Binary Number | LED5 | LED4 | LED3 | LED2 | LED1 |
0 | 0 | off | off | off | off | off |
1 | 1 | off | off | off | off | on |
2 | 10 | off | off | off | on | off |
3 | 11 | off | off | off | on | on |
4 | 100 | off | off | on | off | off |
5 | 101 | off | off | on | off | on |
6 | 110 | off | off | on | on | off |
7 | 111 | off | off | on | on | on |
8 | 1000 | off | on | off | off | off |
9 | 1001 | off | on | off | off | on |
10 | 1010 | off | on | off | on | off |
11 | 1011 | off | on | off | on | on |
12 | 1100 | off | on | on | off | off |
13 | 1101 | off | on | on | off | on |
14 | 1110 | off | on | on | on | off |
15 | 1111 | off | on | on | on | on |
16 | 10000 | on | off | off | off | off |
—– | —– | — | — | — | — | — |
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:
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.