import time
import board
import digitalio
# Keypad setup (4x4)
# Define row pins (connected to keypad rows)
rows = [board.GP13, board.GP12, board.GP11, board.GP10]
# Define column pins (connected to keypad columns)
cols = [board.GP9, board.GP8, board.GP7, board.GP6]
# Lists to store configured pins
row_pins = []
col_pins = []
# Configure 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 # Set HIGH (inactive state)
row_pins.append(pin) # Save in list
# Configure column pins as INPUT
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 (default HIGH)
col_pins.append(pin) # Save in list
# Keypad layout (mapping row + column to characters)
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
# Each list represents segments a,b,c,d,e,f,g (1 = ON, 0 = OFF)
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],
'3':[1,1,1,1,0,0,1],
'4':[0,1,1,0,0,1,1],
'5':[1,0,1,1,0,1,1],
'6':[1,0,1,1,1,1,1],
'7':[1,1,1,0,0,0,0],
'8':[1,1,1,1,1,1,1],
'9':[1,1,1,1,0,1,1],
'A':[1,1,1,0,1,1,1], # Display A
'B':[0,0,1,1,1,1,1], # Display b-like
'C':[0,0,0,1,1,0,1],
'D':[0,1,1,1,1,0,1],
'-':[0,0,0,0,0,0,1], # Dash
'h':[0,1,1,0,1,1,1], # Small h
' ':[0,0,0,0,0,0,0] # Blank
}
# Segment pins (a–g)
seg_pins = [
board.GP16, # a segment
board.GP17, # b segment
board.GP18, # c segment
board.GP19, # d segment
board.GP20, # e segment
board.GP21, # f segment
board.GP22 # g segment
]
# Store segment outputs
seg_outputs = []
# Configure 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) # Save in list
# Display function
def display(char):
# Get pattern for character, default to blank if not found
pattern = segments.get(char, segments[' '])
# Loop through 7 segments
for i in range(7):
seg_outputs[i].value = pattern[i] # Turn ON/OFF segment
# Keypad scanning function
def scan_keypad():
# Loop through each row
for r_idx, r_pin in enumerate(row_pins):
r_pin.value = False # Activate row (set LOW)
# Check each column
for c_idx, c_pin in enumerate(col_pins):
# If column reads LOW → key is pressed
if not c_pin.value:
r_pin.value = True # Reset row
return keys[r_idx][c_idx] # Return pressed key
r_pin.value = True # Reset row after scanning
return None # No key pressed
# Main loop
last_key = None # Store previous key to avoid repeat
while True:
key = scan_keypad() # Read keypad
# If key is pressed and different from last key
if key and key != last_key:
print("Key:", key) # Print to serial monitor
# If numeric key (0–9)
if key.isdigit():
display(key)
# If A, B, C, D
elif key in ['A','B','C','D']:
display(key)
# If '*' pressed → show dash
elif key == '*':
display('-')
# If '#' pressed → show 'h'
elif key == '#':
display('h')
last_key = key # Save current key
# If no key pressed, reset last_key
if not key:
last_key = None
# Small delay to prevent bouncing
time.sleep(0.05)