from machine import Pin
import time
rows = [Pin(7, Pin.OUT),
Pin(6, Pin.OUT),
Pin(5, Pin.OUT),
Pin(4, Pin.OUT)]
cols = [Pin(3, Pin.IN, Pin.PULL_UP),
Pin(2, Pin.IN, Pin.PULL_UP),
Pin(1, Pin.IN, Pin.PULL_UP),
Pin(0, Pin.IN, Pin.PULL_UP)]
keypad_layout = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
def scan_keypad():
for i in range(4):
for j in range(4):
if i == j: rows[j].value(0)
else: rows[j].value(1)
for col_num, col in enumerate(cols):
if col.value() == 0: return keypad_layout[i][col_num]
return None
while True:
key = scan_keypad()
if key: print("Key pressed:", key)
time.sleep(0.01)