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)
# -------------------------------
segments = [
Pin(10, Pin.OUT),
Pin(11, Pin.OUT),
Pin(12, Pin.OUT),
Pin(13, Pin.OUT),
Pin(14, Pin.OUT),
Pin(15, Pin.OUT),
Pin(16, Pin.OUT),
]
# 7-segment encoding for 0–9
digit_map = {
0: [1,1,1,1,1,1,0],
1: [0,1,1,0,0,0,0],
2: [1,1,0,1,1,0,1],
3: [1,1,1,1,0,0,1],
4: [0,1,1,0,0,1,1],
5: [1,0,1,1,0,1,1],
6: [1,0,1,1,1,1,1],
7: [1,1,1,0,0,0,0],
8: [1,1,1,1,1,1,1],
9: [1,1,1,1,0,1,1],
"off": [0,0,0,0,0,0,0]
}
def display_digit(num):
pattern = digit_map[num]
for i in range(7):
if pattern[i] == 1:
segments[i].value(True)
else:
segments[i].value(False)
# -------------------------------
# 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
key = int(key)
display_digit(key)
else:
print(f"Only digits are allowed")
display_digit("off")
time.sleep(0.3) # debounce delay
Loading
pi-pico-w
pi-pico-w