import machine
import time
# Define your keypad matrix (rows and columns) and corresponding GPIO pins
keypad_rows = [machine.Pin(13), machine.Pin(12), machine.Pin(11), machine.Pin(10)]
keypad_cols = [machine.Pin(9), machine.Pin(8), machine.Pin(7), machine.Pin(6)]
# Define the keypad layout (4x4)
keypad_layout = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
# Initialize the keypad pins
for row in keypad_rows:
row.init(machine.Pin.IN, pull=machine.Pin.PULL_UP)
for col in keypad_cols:
col.init(machine.Pin.OUT)
# Function to scan the keypad for keypress
def scan_keypad():
key = None
for i in range(len(keypad_cols)):
# Activate the column
keypad_cols[i].low()
for j in range(len(keypad_rows)):
if not keypad_rows[j].value():
key = keypad_layout[j][i]
# Deactivate the column
keypad_cols[i].high()
return key
while True:
key_pressed = scan_keypad()
if key_pressed:
print("Key Pressed:", key_pressed)
time.sleep(0.2)