'''
PIN13 - R1 (OUTPUT)
PIN14 - R2 (OUTPUT)
PIN15 - R3 (OUTPUT)
PIN16 - R4 (OUTPUT)
PIN32 - C1 (INPUT)
PIN33 - C2 (INPUT)
PIN34 - C3 (INPUT)
PIN35 - C4 (INPUT)
'''
import machine
import time
# Define rows and columns
ROWS = [13, 14, 15, 16] # GPIO pins for rows - OUTPUTS
COLS = [32, 33, 34, 35] # GPIO pins for columns - INPUTS
# Keypad layout
KEYPAD = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']]
# Configure rows as outputs
row_pins = [machine.Pin(pin, machine.Pin.OUT) for pin in ROWS]
# Configure columns as inputs with pull-up resistors
col_pins = [machine.Pin(pin, machine.Pin.IN, machine.Pin.PULL_UP) for pin in COLS]
def scan_keypad():
for row_idx, row_pin in enumerate(row_pins):
# Activate the current row
row_pin.value(1)
for col_idx, col_pin in enumerate(col_pins):
if col_pin.value() == 0: # Check if the column is pulled LOW
row_pin.value(0) # Deactivate the row before returning
return KEYPAD[row_idx][col_idx] # Return the key
row_pin.value(0) # Deactivate the current row
return None # No key pressed
# Main loop
while True:
key = scan_keypad()
if key:
print(f"Key Pressed: {key}")
time.sleep(0.1) # Debounce delay