from machine import Pin
from time import sleep
# Define rows and columns (adjust GPIOs if needed)
rows = [Pin(i, Pin.OUT) for i in (2,3,4,5)] # R1, R2, R3, R4
cols = [Pin(i, Pin.IN, Pin.PULL_UP) for i in (6,7,8,9)] # C1, C2, C3, C4
def scan_keypad():
for r in range(4):
rows[r].low() # Pull this row LOW
for c in range(4):
if not cols[c].value(): # If this column is LOW, key is pressed
rows[r].high()
return r, c
rows[r].high()
return None
# Initialize all rows HIGH
for row in rows:
row.value(1)
while True:
key = scan_keypad()
if key:
print(f"Button pressed at [row, col]: {key}")
while scan_keypad(): # Wait for key release (debounce)
sleep(0.05)
sleep(0.05)