todbot / circuitpython-tricks

Some CircuitPython tricks, mostly reminders to myself
MIT License
577 stars 65 forks source link

Using Matrix Keypad with I2C Port Expander #11

Open djairjr opened 1 year ago

djairjr commented 1 year ago

Matrix Keypad

Using I2C Port Expander With Matrix Keypad

import adafruit_matrixkeypad
import adafruit_pcf8574
from digitalio import DigitalInOut
import board
import time
# Using default i2c with board.SDA e board.SCL
i2c = board.I2C()
pcf = adafruit_pcf8574.PCF8574(i2c, 0x20)

# Classic 3x4 matrix keypad
# I directly soldered the pcf8574 module on the keypad
# with pinout C2 R1 C1 R4 C3 R3 R2
cols = [pcf.get_pin(4), pcf.get_pin(6), pcf.get_pin(2)]
rows = [pcf.get_pin(5), pcf.get_pin(0), pcf.get_pin(1), pcf.get_pin(3)]

keys = ((1, 2, 3),
        (4, 5, 6),
        (7, 8, 9),
        ('*', 0, '#'))

keypad = adafruit_matrixkeypad.Matrix_Keypad(rows, cols, keys)

while True:
    keys = keypad.pressed_keys
    if keys:
        print("Pressed: ", keys)
    time.sleep(0.1)