# Matrix Keypad
# NerdCave - https://www.youtube.com/channel/UCxxs1zIA4cDEBZAHIJ80NVg - Subscribe if you found this helpful.
# Github - https://github.com/Guitarman9119
from machine import Pin
import utime
inKey = ""
# Create a map between keypad buttons and characters
keyMatrix = [['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']]
# PINs according to schematic - Change the pins to match with your connections
keypadRows = [9,8,7,6]
keypadCols = [5,4,3,2]
# Create two empty lists to set up pins ( Rows output and columns input )
colPins = []
rowPins = []
# Loop to assign GPIO pins and setup input and outputs
for x in range(0,4):
rowPins.append(Pin(keypadRows[x], Pin.OUT))
rowPins[x].value(1)
colPins.append(Pin(keypadCols[x], Pin.IN, Pin.PULL_DOWN))
colPins[x].value(0)
def scankeys():
keyPress = ""
for row in range(4):
for col in range(4):
rowPins[row].high()
key = None
if colPins[col].value() == 1:
keyPress = keyMatrix[row][col]
rowPins[row].low()
if keyPress != "":
utime.sleep(0.2)
global inKey
inKey = keyPress
while True:
scankeys()
if inKey != "":
print(inKey)