Skip to content

Instantly share code, notes, and snippets.

@rodnaxel
Created April 27, 2026 16:16
Show Gist options
  • Select an option

  • Save rodnaxel/6b293d098b2bb90d3338908b797687e1 to your computer and use it in GitHub Desktop.

Select an option

Save rodnaxel/6b293d098b2bb90d3338908b797687e1 to your computer and use it in GitHub Desktop.
Simple test connection controller matrix keyboard PCF8574
#!/usr/bin/env python3
"""
1. Enable I2C
sudo raspi-config
Interface Options → I2C → Enable
sudo reboot
2. Install libraries
sudo apt update
sudo apt install -y i2c-tools python3-smbus
3. Find address i2c
i2cdetect -y -1
"""
import time
import smbus
I2C_ADDR = 0x20
bus = smbus.SMBus(1)
# P0..P2 - columns, P4..P7 - rows
COLS = [2, 1, 0]
ROWS = [6, 5, 4, 3]
KEYS = [
["1", "2", "3"],
["4", "5", "6"],
["7", "8", "9"],
["*", "0", "#"],
]
def write_pcf(value):
bus.write_byte(I2C_ADDR, value & 0xFF)
def read_pcf():
return bus.read_byte(I2C_ADDR)
def scan_keypad():
for col_index, col_pin in enumerate(COLS):
# All lines HIGH, except for current columns LOW
value = 0xFF & ~(1 << col_pin)
write_pcf(value)
time.sleep(0.002)
data = read_pcf()
for row_index, row_pin in enumerate(ROWS):
if not (data & (1 << row_pin)):
return KEYS[row_index][col_index]
return None
print("Check keypad. Press buttons. Ctrl+C - exit")
last_key = None
try:
while True:
key = scan_keypad()
if key and key != last_key:
print("Pressed", key)
last_key = key
if not key:
last_key = None
time.sleep(0.1)
except KeyboardInterrupt:
write_pcf(0xFF)
print("Exit")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment