from machine import Pin
from time import sleep
# Pin definitions for rows and columns of the keypad
rows = [Pin(2, Pin.OUT),
Pin(3, Pin.OUT),
Pin(4, Pin.OUT),
Pin(5, Pin.OUT)]
cols = [Pin(6, Pin.IN, Pin.PULL_UP),
Pin(7, Pin.IN, Pin.PULL_UP),
Pin(8, Pin.IN, Pin.PULL_UP),
Pin(9, Pin.IN, Pin.PULL_UP)]
# Keypad matrix (mapping rows and columns to keypad keys)
keypad = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
while True:
for row_num, row in enumerate(rows):
row.value(0) # Activate the current row
for col_num, col in enumerate(cols):
if col.value() == 0: # Check if the key is pressed
print("Key pressed: " + keypad[row_num][col_num])
sleep(0.2) # Debounce delay
row.value(1) # Deactivate the current row
sleep(0.1)