import board # Import board pin definitions (GP pins)
import digitalio # Import GPIO control module
import time # Import time module for delays
# ------------------------
# Keypad setup (4x4)
# ------------------------
# Define row pins (outputs)
rows = [board.GP13, board.GP12, board.GP11, board.GP10]
# Define column pins (inputs)
cols = [board.GP9, board.GP8, board.GP7, board.GP6]
row_pins = [] # List to store configured row pins
col_pins = [] # List to store configured column pins
# Initialize row pins as OUTPUT
for r in rows:
pin = digitalio.DigitalInOut(r) # Create pin object
pin.direction = digitalio.Direction.OUTPUT # Set as output
pin.value = True # Default HIGH (inactive)
row_pins.append(pin) # Store in list
# Initialize column pins as INPUT with pull-up resistor
for c in cols:
pin = digitalio.DigitalInOut(c) # Create pin object
pin.direction = digitalio.Direction.INPUT # Set as input
pin.pull = digitalio.Pull.UP # Enable pull-up resistor
col_pins.append(pin) # Store in list
# ------------------------
# Key map (button layout)
# ------------------------
keys = [
['1','2','3','A'], # Row 1
['4','5','6','B'], # Row 2
['7','8','9','C'], # Row 3
['*','0','#','D'] # Row 4
]
# ------------------------
# 7-Segment Character Map
# Format: a,b,c,d,e,f,g
# ------------------------
segments = {
'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
'A':[1,1,1,0,1,1,1], # Display A
'B':[0,0,1,1,1,1,1], # Display b
'C':[0,0,0,1,1,0,1], # Display c
'D':[0,1,1,1,1,0,1], # Display d
'-':[0,0,0,0,0,0,1], # Display dash "-"
'h':[0,1,1,0,1,1,1], # Display h
' ':[0,0,0,0,0,0,0] # Blank (all OFF)
}
# ------------------------
# Segment pins (a-g)
# ------------------------
seg_pins = [
board.GP16, # Segment a
board.GP17, # Segment b
board.GP18, # Segment c
board.GP19, # Segment d
board.GP20, # Segment e
board.GP21, # Segment f
board.GP22 # Segment g
]
seg_outputs = [] # Store segment output pins
# Initialize segment pins as OUTPUT
for p in seg_pins:
pin = digitalio.DigitalInOut(p) # Create pin object
pin.direction = digitalio.Direction.OUTPUT # Set as output
seg_outputs.append(pin) # Store in list
# ------------------------
# Function: Display character
# ------------------------
def display(char):
pattern = segments.get(char, segments[' ']) # Get segment pattern
for i in range(7): # Loop through 7 segments
seg_outputs[i].value = pattern[i] # Set ON/OFF
# ------------------------
# Function: Scan keypad
# ------------------------
def scan_keypad():
for i, row in enumerate(row_pins): # Loop through each row
row.value = False # Activate current row (LOW)
for j, col in enumerate(col_pins): # Check each column
if not col.value: # If column reads LOW → key pressed
row.value = True # Deactivate row
return keys[i][j] # Return key value
row.value = True # Deactivate row after checking
return None # No key pressed
# ------------------------
# Main loop
# ------------------------
last_key = None # Store previous key (for debounce)
while True:
key = scan_keypad() # Read keypad
if key and key != last_key: # If new key detected
time.sleep(0.15) # Debounce delay
key = scan_keypad() # Confirm key again
print("Key:", key) # Print key to console
# ---- Numeric keys ----
if key in ['0','1','2','3','4','5','6','7','8','9']:
display(key)
# ---- Letters A-D ----
elif key in ['A','B','C','D']:
display(key)
# ---- Special keys ----
elif key == '*':
display('-') # Show dash
elif key == '#':
display('h') # Show h
last_key = key # Save last key
# If no key pressed → clear display
if not key:
display(' ')
last_key = None
time.sleep(0.05) # Small delay for stability