circuitpython-tricks icon indicating copy to clipboard operation
circuitpython-tricks copied to clipboard

Using Matrix Keypad with I2C Port Expander

Open djairjr opened this issue 1 year ago • 0 comments

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)

djairjr avatar Feb 03 '23 13:02 djairjr