Skip to main content

Finding Key Matrix

The following program will list the GPIO names connected to anodes and cathodes of a keyboard matrix. Open up the serial connection to the REPL and press keys in sequence. Make sure you go through each row and column methodically so that the order printed out makes sense.

If no diodes are in the key matrix, you may see pins listed in both anodes and cathodes. With this list you will have a starting point on how to configure the keypad scanning routines.

import board
import time
from microcontroller import Pin
from digitalio import DigitalInOut, Direction, Pull

def get_unique_pins():
exclude = ['NEOPIXEL', 'APA102_MOSI', 'APA102_SCK']
pins = [pin for pin in [
getattr(board, p) for p in dir(board) if p not in exclude]
if isinstance(pin, Pin)]
unique = []
for p in pins:
if p not in unique:
return unique

pins = []

for scl_pin in get_unique_pins():
pin = DigitalInOut(scl_pin)
pin.direction = Direction.INPUT
pin.pull = Pull.DOWN
if pin not in pins:
pins.append([scl_pin, pin])

cathodes = []

while True:
for row in pins:
row[1].direction = Direction.OUTPUT
row[1].value = True
for column in pins:
if row is column:
column[1].direction = Direction.INPUT
column[1].pull = Pull.DOWN

if column[1].value:
if row[0] not in anodes:
if column[0] not in cathodes:
print('anodes', end=' ')
print('cathodes', end=' ')