import time
from machine import I2C, Pin
import pcf8574
time.sleep(0.1) # Wait for USB to become ready
class keypad_module:
I2CADDR = 0x20 # valid range is 0x20 - 0x27
PULUPA = 0x0F # PullUp enable register base address
PULUPB = 0xF0 # PullUp enable register base address
# Keypad Keycode matrix
KEYCODE = [['1','4','7','*'], # KEYCOL0
['2','5','8','0'], # KEYCOL1
['3','6','9','#'], # KEYCOL2
['A','B','C','D']] # KEYCOL3
# Decide the row
DECODE = [0,0,1,0,2,0,0,0,3]
# initialize I2C comm
i2c = I2C(0, scl=Pin(1), sda=Pin(0))
pcf = pcf8574.PCF8574(i2c, 0x20)
# get a keystroke from the keypad
def getch(self):
while 1:
time.sleep(0.01)
self.pcf.port = self.PULUPA
row = self.pcf.port
if (row) != 0b1111:
row = row >> 4
row = self.DECODE[row]
self.pcf.port = self.PULUPB
col = self.pcf.port & 0x0F
col = self.DECODE[col]
return self.KEYCODE[row][col]
# initialize the keypad class
def __init__(self,addr):
self.I2CADDR = addr
print("Hello, Pi Pico!")
# test code
keypad = keypad_module(0x20)
while 1:
ch = keypad.getch()
print(ch)
Adress bits
bit0
bit1
bit2