import time
import board
import digitalio
# Define segment pins for the 7-segment display
# Each key ('a' to 'g') represents a segment of the display
# Each segment is connected to a specific GPIO pin
segments = {
'a': digitalio.DigitalInOut(board.GP16), # Segment A connected to GP16
'b': digitalio.DigitalInOut(board.GP17), # Segment B connected to GP17
'c': digitalio.DigitalInOut(board.GP18), # Segment C connected to GP18
'd': digitalio.DigitalInOut(board.GP19), # Segment D connected to GP19
'e': digitalio.DigitalInOut(board.GP20), # Segment E connected to GP20
'f': digitalio.DigitalInOut(board.GP21), # Segment F connected to GP21
'g': digitalio.DigitalInOut(board.GP22), # Segment G connected to GP22
}
# Set all segment pins as OUTPUT so they can send signals to the display
for seg in segments.values():
seg.direction = digitalio.Direction.OUTPUT
# Define digit patterns for numbers 0–9
# Each tuple represents the ON (1) or OFF (0) state of segments (a–g)
digits = {
0: (1, 1, 1, 1, 1, 1, 0), # Display 0
1: (0, 1, 1, 0, 0, 0, 0), # Display 1
2: (1, 1, 0, 1, 1, 0, 1), # Display 2
3: (1, 1, 1, 1, 0, 0, 1), # Display 3
4: (0, 1, 1, 0, 0, 1, 1), # Display 4
5: (1, 0, 1, 1, 0, 1, 1), # Display 5
6: (1, 0, 1, 1, 1, 1, 1), # Display 6
7: (1, 1, 1, 0, 0, 0, 0), # Display 7
8: (1, 1, 1, 1, 1, 1, 1), # Display 8
9: (1, 1, 1, 1, 0, 1, 1), # Display 9
}
# Define the order of segments to match the tuple positions above
segment_keys = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
# Function to display a digit on the 7-segment display
def display_digit(num):
# Get the ON/OFF pattern for the given number
pattern = digits[num]
# Loop through each segment and apply the corresponding value
for i, seg in enumerate(segment_keys):
segments[seg].value = pattern[i] # Turn segment ON (1) or OFF (0)
# Main loop runs forever
while True:
# Loop through digits 0 to 9
for i in range(10):
print("Displaying:", i) # Print current digit to console (for debugging)
display_digit(i) # Call function to show digit on display
time.sleep(1) # Wait 1 second before showing next digit