import time
import board
import digitalio
# ============================================
# 4x4 KEYPAD SETUP
# ============================================
# Define keypad rows and columns (GP6-GP13 as per your diagram)
rows = [board.GP6, board.GP7, board.GP8, board.GP9] # Row pins (GP6-GP9)
cols = [board.GP10, board.GP11, board.GP12, board.GP13] # Column pins (GP10-GP13)
# Keypad mapping (4x4)
KEYPAD = {
(0, 0): '1', (0, 1): '2', (0, 2): '3', (0, 3): 'A',
(1, 0): '4', (1, 1): '5', (1, 2): '6', (1, 3): 'B',
(2, 0): '7', (2, 1): '8', (2, 2): '9', (2, 3): 'C',
(3, 0): '*', (3, 1): '0', (3, 2): '#', (3, 3): 'D'
}
# Initialize row pins as outputs
row_pins = []
for pin in rows:
row = digitalio.DigitalInOut(pin)
row.direction = digitalio.Direction.OUTPUT
row.value = False # Start with all rows LOW
row_pins.append(row)
# Initialize column pins as inputs with pull-down
col_pins = []
for pin in cols:
col = digitalio.DigitalInOut(pin)
col.direction = digitalio.Direction.INPUT
col.pull = digitalio.Pull.DOWN # Pull-down for column detection
col_pins.append(col)
# ============================================
# 7-SEGMENT DISPLAY SETUP (Active HIGH, Common Cathode)
# ============================================
# 7-segment character map (a,b,c,d,e,f,g)
# 1 = ON, 0 = OFF (Active HIGH)
segments = {
'0': [1, 1, 1, 1, 1, 1, 0], # a,b,c,d,e,f on, g off
'1': [0, 1, 1, 0, 0, 0, 0], # b,c on
'2': [1, 1, 0, 1, 1, 0, 1], # a,b,d,e,g on
'3': [1, 1, 1, 1, 0, 0, 1], # a,b,c,d,g on
'4': [0, 1, 1, 0, 0, 1, 1], # b,c,f,g on
'5': [1, 0, 1, 1, 0, 1, 1], # a,c,d,f,g on
'6': [1, 0, 1, 1, 1, 1, 1], # a,c,d,e,f,g on
'7': [1, 1, 1, 0, 0, 0, 0], # a,b,c on
'8': [1, 1, 1, 1, 1, 1, 1], # all segments on
'9': [1, 1, 1, 1, 0, 1, 1], # a,b,c,d,f,g on
'A': [1, 1, 1, 0, 1, 1, 1], # a,b,c,e,f,g on
'B': [0, 0, 1, 1, 1, 1, 1], # c,d,e,f,g on
'C': [1, 0, 0, 1, 1, 1, 0], # a,d,e,f on
'D': [0, 1, 1, 1, 1, 0, 1], # b,c,d,e,g on
'E': [1, 0, 0, 1, 1, 1, 1], # a,d,e,f,g on
'F': [1, 0, 0, 0, 1, 1, 1], # a,e,f,g on
'-': [0, 0, 0, 0, 0, 0, 1], # g segment only
'h': [0, 0, 1, 0, 1, 1, 1], # c,e,f,g on
' ': [0, 0, 0, 0, 0, 0, 0] # all off
}
# 7-segment pins (GP16-GP22)
# a=GP16, b=GP17, c=GP18, d=GP19, e=GP20, f=GP21, g=GP22
seg_pins = [
board.GP16, # a
board.GP17, # b
board.GP18, # c
board.GP19, # d
board.GP20, # e
board.GP21, # f
board.GP22 # g
]
# Initialize segment pins as outputs
seg_outputs = []
for pin in seg_pins:
seg = digitalio.DigitalInOut(pin)
seg.direction = digitalio.Direction.OUTPUT
seg_outputs.append(seg)
# ============================================
# KEYPAD SCANNING FUNCTION
# ============================================
def scan_keypad():
"""Scan the 4x4 keypad and return the pressed key or None"""
for row_idx, row_pin in enumerate(row_pins):
# Set current row HIGH
row_pin.value = True
# Check each column
for col_idx, col_pin in enumerate(col_pins):
if col_pin.value: # Button pressed
row_pin.value = False # Reset row
return KEYPAD.get((row_idx, col_idx))
# Reset row to LOW
row_pin.value = False
return None # No key pressed
# ============================================
# DISPLAY FUNCTION
# ============================================
def display(char):
"""Display a character on the 7-segment display"""
# Get the pattern for the character (default to space if not found)
pattern = segments.get(char, segments[' '])
# Set each segment pin
for i in range(7):
seg_outputs[i].value = pattern[i]
def display_number(num):
"""Display a single digit number (0-9)"""
display(str(num))
def clear_display():
"""Turn off all segments"""
display(' ')
# ============================================
# TEST FUNCTION
# ============================================
def test_display():
"""Test all segments by displaying 8.8.8.8"""
print("Testing 7-segment display...")
for i in range(8):
display('8')
time.sleep(0.2)
clear_display()
time.sleep(0.1)
display('0')
print("Test complete")
# ============================================
# MAIN LOOP
# ============================================
# Test display on startup
test_display()
print("=== 4x4 Keypad to 7-Segment Display ===")
print("Press any key on the keypad")
print("Available keys: 0-9, A-D, *, #")
print("-" * 40)
last_key = None
while True:
# Scan for key press
key = scan_keypad()
# Only process if a key is pressed and it's different from last key
if key is not None and key != last_key:
print(f"Key pressed: {key}")
# Display based on key type
if key.isdigit(): # Numbers 0-9
display(key)
elif key in ['A', 'B', 'C', 'D']: # Letters A-D
display(key)
elif key == '*': # Star key
display('-') # Show dash
elif key == '#': # Hash key
display('h') # Show 'h' for hash
else:
print(f"Unknown key: {key}")
last_key = key
# Reset last_key when no key is pressed
elif key is None:
last_key = None
# Small delay to prevent excessive scanning
time.sleep(0.05)