import machine
import utime

keypad = [
    [1, 2, 3, 'A'],
    [4, 5, 6, 'B'],
    [7, 8, 9, 'C'],
    ['*', 0, '#', 'D']
]


rows = [machine.Pin(pin, machine.Pin.OUT) for pin in [2, 3, 4, 5]]
cols = [machine.Pin(pin, machine.Pin.IN, machine.Pin.PULL_UP) for pin in [6, 7, 8, 9]]

def scan_keypad():
    key_pressed =None
    for i, row in enumerate(rows):
        row.value(0)
        for j, col in enumerate(cols):
            if not col.value():
                key_pressed = keypad[i][j]
                while not col.value():
                    pass
        row.value(1)
    return key_pressed

while True:
    key = scan_keypad()
    if key is not None:
        print("key pressed:", key)
    utime.sleep(0.1)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT