import time # Import time module for delay function
import board # Import board module to access GPIO pins
import digitalio # Import digital I/O for button control
from cd4511 import CD4511 # Import CD4511 library for 7-segment display
# BCD pins for 7-segment displays
bcd_tens = [board.GP2, board.GP3, board.GP4, board.GP5] # GPIO pins for tens
digit (BCD input)
bcd_ones = [board.GP6, board.GP7, board.GP8, board.GP9] # GPIO pins for ones
digit (BCD input)
# Initialize CD4511 display drivers
tens_display = CD4511(bcd_tens, board.GP0) # Create object for tens display
(enable pin GP0)
ones_display = CD4511(bcd_ones, board.GP1) # Create object for ones display
(enable pin GP1)
# Button configuration
btn_up = digitalio.DigitalInOut(board.GP10) # Assign GP10 as COUNT UP button
btn_up.direction = digitalio.Direction.INPUT # Set as input
btn_up.pull = digitalio.Pull.UP # Enable internal pull-up
resistor
btn_down = digitalio.DigitalInOut(board.GP11) # Assign GP11 as COUNT DOWN
button
btn_down.direction = digitalio.Direction.INPUT
btn_down.pull = digitalio.Pull.UP
# Initialize variables
count = 0 # Starting value of counter
mode = 1 # Mode = 1 (UP), -1 (DOWN)
# Main loop (runs continously)
while True:
# UP button
if not btn_up.value: # If button is pressed (LOW signal)
mode = 1 # Set mode to COUNT UP
print("Mode: UP") # Display mode in serial monitor
time.sleep(0.3) # Delay for debounce (avoid multiple
triggers)
# DOWN button
if not btn_down.value: # If button is pressed
mode = -1 # Set mode to COUNT DOWN
print("Mode: DOWN")
time.sleep(0.3)
# Split number into digits
tens = count // 10 # Get tens digit (e.g., 45 → 4)
ones = count % 10 # Get ones digit (e.g., 45 → 5)
# Display on 7-segment
tens_display.display(tens) # Send tens digit to display
ones_display.display(ones) # Send ones digit to display
# Print output (debugging)
print("Displaying:", count) # Show current value in serial monitor
# Counting logic
if mode == 1:
count = (count + 1) % 100 # Count UP (0 → 99 → 0)
else:
count = (count - 1) % 100 # Count DOWN (0 → 99 → 98...)
# Delay for speed control
time.sleep(0.5) # Wait 0.5 seconds before next count