from machine import Pin
import time
# Matriks keypad
keys = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
['*', '0', '#']
]
# Baris → input pull-up
row_pins = [Pin(pin, Pin.IN, Pin.PULL_UP) for pin in [23, 22, 21, 19]]
# Kolom → output default HIGH
col_pins = [Pin(pin, Pin.OUT) for pin in [18, 5, 17]]
# Loop pembacaan keypad
def scan_keypad():
for c_idx, c in enumerate(col_pins):
c.off()
for r_idx, r in enumerate(row_pins):
if not r.value():
time.sleep_ms(20)
while not r.value():
pass
c.on()
return keys[r_idx][c_idx]
c.on()
return None
print("Keypad siap dibaca...")
while True:
key = scan_keypad()
if key:
print("Tombol ditekan:", key)
time.sleep(0.1)