import machine
import utime
# Define GPIO pins connected to the 7-segment display segments
segments = [
machine.Pin(2, machine.Pin.OUT), # a (GP2)
machine.Pin(3, machine.Pin.OUT), # b (GP3)
machine.Pin(4, machine.Pin.OUT), # c (GP4)
machine.Pin(5, machine.Pin.OUT), # d (GP5)
machine.Pin(6, machine.Pin.OUT), # e (GP6)
machine.Pin(7, machine.Pin.OUT), # f (GP7)
machine.Pin(8, machine.Pin.OUT), # g (GP8)
]
# Define the display patterns for each digit from 0 to 9
digit_patterns = [
[0, 0, 0, 0, 0, 0, 1], # 0
[1, 0, 0, 1, 1, 1, 1], # 1
[0, 0, 1, 0, 0, 1, 0], # 2
[0, 0, 0, 0, 1, 1, 0], # 3
[1, 0, 0, 1, 1, 0, 0], # 4
[0, 1, 0, 0, 1, 0, 0], # 5
[0, 1, 0, 0, 0, 0, 0], # 6
[0, 0, 0, 1, 1, 1, 1], # 7
[0, 0, 0, 0, 0, 0, 0], # 8
[0, 0, 0, 0, 1, 0, 0] # 9
]
def display_digit(digit):
# Set the display to show the given digit
for i in range(7):
segments[i].value(digit_patterns[digit][i])
try:
while True:
for i in range(10):
display_digit(i)
utime.sleep(1)
except KeyboardInterrupt:
# Press Ctrl+C to stop the counting
for segment in segments:
segment.value(0) # Turn off all segments