from machine import Pin
import time
# -------------------------------
# Keypad configuration (4x4)
# -------------------------------
rows = [
Pin(2, Pin.OUT, Pin.PULL_DOWN),
Pin(3, Pin.OUT, Pin.PULL_DOWN),
Pin(4, Pin.OUT, Pin.PULL_DOWN),
Pin(5, Pin.OUT, Pin.PULL_DOWN)
]
cols = [
Pin(6, Pin.IN, Pin.PULL_DOWN),
Pin(7, Pin.IN, Pin.PULL_DOWN),
Pin(8, Pin.IN, Pin.PULL_DOWN),
Pin(9, Pin.IN, Pin.PULL_DOWN)
]
keys = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
# -------------------------------
# LED configuration (8 LEDs)
# -------------------------------
led_pins = [
Pin(10, Pin.OUT, Pin.PULL_DOWN),
Pin(11, Pin.OUT, Pin.PULL_DOWN),
Pin(12, Pin.OUT, Pin.PULL_DOWN),
Pin(13, Pin.OUT, Pin.PULL_DOWN),
Pin(14, Pin.OUT, Pin.PULL_DOWN),
Pin(15, Pin.OUT, Pin.PULL_DOWN),
Pin(16, Pin.OUT, Pin.PULL_DOWN),
Pin(17, Pin.OUT, Pin.PULL_DOWN),
]
# -------------------------------
# Helper functions
# -------------------------------
def clear_leds():
for led in led_pins:
led.value(0)
def all_leds_on():
for led in led_pins:
led.value(1)
def display_binary(value):
"""
Display an integer value in 8-bit binary on LEDs
"""
for i in range(8):
led_pins[i].value((value >> i) & 1)
# -------------------------------
# Keypad scanning function
# -------------------------------
def read_key():
for r in range(4):
rows[r].value(1)
for c in range(4):
if cols[c].value() == 1:
rows[r].value(0)
return keys[r][c]
rows[r].value(0)
return None
# -------------------------------
# Main loop
# -------------------------------
while True:
key = read_key()
if key:
print("Key pressed:", key)
if key.isdigit():
# Convert digit to integer and show binary
value = int(key)
clear_leds()
display_binary(value)
else:
# Special characters (*, #, A–D)
all_leds_on()
time.sleep(0.3) # debounce delay
Loading
pi-pico-w
pi-pico-w