from machine import Pin
from time import sleep
# Configure rows as output pins
rows = [Pin(0, Pin.OUT), Pin(1, Pin.OUT), Pin(2, Pin.OUT)]
# Configure columns as input pins with pull-down resistors
columns = [Pin(3, Pin.IN, Pin.PULL_DOWN), Pin(4, Pin.IN, Pin.PULL_DOWN), Pin(5, Pin.IN, Pin.PULL_DOWN),
Pin(6, Pin.IN, Pin.PULL_DOWN), Pin(7, Pin.IN, Pin.PULL_DOWN)]
def scan_matrix():
"""Scan the button matrix and print pressed buttons."""
for row_idx, row in enumerate(rows):
row.high() # Set the current row HIGH
for col_idx, col in enumerate(columns):
if col.value() == 1: # Check if the column pin reads HIGH
print(f"Button pressed at Row {row_idx + 1}, Column {col_idx + 1}")
sleep(0.1) # Debounce delay
row.low() # Set the row back to LOW
sleep(0.1)
while True:
scan_matrix()