import board # Access Raspberry Pi Pico GPIO pins
import digitalio # Control digital input/output pins
import time # Used for delay and debounce timing
# ==============================================
# KEYPAD INPUT SETUP (MM74C922 ENCODER)
# ==============================================
# Data Available pin (signals when key is pressed)
data_available = digitalio.DigitalInOut(board.GP14)
data_available.direction = digitalio.Direction.INPUT # Set as input
# Keypad data lines (D3D0), A,B,C,D
data_pins = [board.GP13, board.GP12, board.GP11, board.GP10]
data_inputs = [] # Store input pin objects
# Convert GPIO pins into digital input objects
for pin in data_pins:
dio = digitalio.DigitalInOut(pin) # Create digital input
dio.direction = digitalio.Direction.INPUT # Set as input
data_inputs.append(dio) # Add to list
# Keypad mapping (binary value character) #MM74C922 datasheet
keypad_map = {
12: '?', 13: '?', 14: '?', 15: '?',
8: '?', 9: '?', 10: '?', 11: '?',
4: '?', 5: '?', 6: '?', 7: '?',
0: '?', 1: '?', 2: '?', 3: '?'
}
# =================================================
# FUNCTION: READ KEYPAD
# =================================================
def read_keypad():
# Check if keypad has sent data
if data_available.value:
value = 0 # Store 4-bit binary value
# Convert 4 input bits into a number
for i, pin in enumerate (data_inputs):
if pin.value:
value = (1 << (3 - i)) # Build binary number (D3+D0)
return value # Return raw keypad value
return None # No key pressed
# ====================================================
# CD4511 BCD OUTPUT (7-SEGMENT DISPLAY)
# ====================================================
A = digitalio.DigitalInOut (board.GP5) # BCD bit 0
B = digitalio.DigitalInOut (board.GP4) # BCD bit 1
C = digitalio.DigitalInOut (board.GP3) # BCD bit 2
D = digitalio.DigitalInOut (board.GP2) # BCD bit 3
# Set all BCD pins as OUTPUT
for pin in [A, B, C, D]:
pin.direction = digitalio.Direction.OUTPUT
data_pins = [A, B, C, D] # Store BCD output pins
# ====================================
# LATCH CONTROL PINS
# ====================================
s72 = digitalio.DigitalInOut(board.GP7) # Controls tens display
s72.direction = digitalio. Direction.OUTPUT
s71 = digitalio.DigitalInOut(board.GP6) # Controls ones display
s71.direction = digitalio.Direction.OUTPUT
# ====================================================
# FUNCTION: SEND BCD TO DISPLAY
# ====================================================
def send_bcd(value):
# Send 4-bit binary value to CD4511
for i in range(4):
data_pins[i].value = (value >> i) & 1
# ====================================================
# FUNCTION: LATCH DATA TO DISPLAY
# ====================================================
def latch(pin):
pin.value = 0 # Enable latch (active low)
time.sleep(0.001) # Small delay for hardware stability
pin.value = 1 # Disable latch
# ====================================================
# MAIN TEST LOOP
# ====================================================
while True:
bcd_val = read_keypad() # Read keypad binary value
if bcd_val is not None: # If key is pressed
# Convert binary value to readable key
key = keypad_map.get(bcd_val, '?')
print("Key Pressed:", key) # Show on Thonny Shell
# Only send numeric keys (0-9) to CD4511
if key.isdigit():
send_bcd(int(key)) # Send number to 7-segment
# Wait until key is released (debounce handling)
while data_available.value:
pass
time.sleep(0.05) # Small delay to avoid bouncing