The ShiftRegisterKeys scanner scans keys that are attached to a parallel-in serial out shift register. The key states are loaded into the shift register all at once, and then clocked out of the shift register into an input pin that is read serially.

At this time we support basic clocked/latched shift registers such as 74x165 chips. These are very inexpensive, but require external pull-up/down resistors. We do not support shift-register-to-matrix or I2C/SPI expanders.

Several Adafruit products, such as the PyBadge and the PyGamer, use a shift register to monitor their buttons. Other products such as SNES game controllers also use shift registers.

PyBadge or PyGamer ShiftRegisterKeys Example

This example reads the buttons on a PyBadge or PyGamer. They both work the same way. The joystick on the PyGamer is really just a set of four switches in a single package: left, right, up, and down.

We don't know how many keys are connected to the shift register. There could also be multiple shift registers chained together. So we have to specify a key_count in the example below.

import keypad
import board

k = keypad.ShiftRegisterKeys(
    clock=board.BUTTON_CLOCK,
    data=board.BUTTON_OUT,
    latch=board.BUTTON_LATCH,
    key_count=8,
    value_when_pressed=True,
)

while True:
    event = k.events.get()
    if event:
        print(event)

SNES Controller Example

Controllers for the venerable SNES game system also use shift registers. Adafruit stocks an SNES controller, and they are readily available elsewhere as well. The controller is pictured on the left, with extra labelling for some of the buttons.

These controllers have 12 buttons, so they use two 8-bit shift registers chained together, with the last 4 bits ignored. So we'll set key_count to 12 below.

The pinout for the connector is shown on the left. Note there is a group of 3 and a group of 4 pins, and one end is rounded.

Which Way to Latch?

The PyBadge and PyGamer use 74HC165 shift registers. These shift registers latch the input when the latch pin is high (True). That's the default for ShiftRegisterKeys. But the SNES controller uses CD4021 shift registers. They latch on low, so we need to specify an extra argument to specify how to latch: we set value_to_latch to False in the code below (the default is True).

SNES Key Names

The program below prints out the SNES key names by looking up the key numbers in the SNES_KEY_NAMES tuple. Key number 0 is "B", 1 is "Y", etc.

import keypad
import board
import time

SNES_KEY_NAMES = (
    "B",
    "Y",
    "SELECT",
    "START",
    "UP",
    "DOWN",
    "LEFT",
    "RIGHT",
    "A",
    "X",
    "L",
    "R",
)

shift_k = keypad.ShiftRegisterKeys(
    clock=board.D5,
    latch=board.D6,
    value_to_latch=False,
    data=board.D7,
    key_count=12,
    value_when_pressed=False,
)

while True:
    event = shift_k.events.get()
    if event:
        print(
            SNES_KEY_NAMES[event.key_number],
            "pressed" if event.pressed else "released",
        )

Here's some sample output from the program above:

START pressed
START released
B pressed
B released
B pressed
B released
X pressed
X released
Y pressed
Y released
L pressed
R pressed
R released
R pressed
R released
L released
DOWN pressed
LEFT pressed
B pressed
DOWN released
LEFT released
B released
LEFT pressed
LEFT released
LEFT pressed
LEFT released

This guide was first published on Jun 29, 2021. It was last updated on Jun 29, 2021.

This page (ShiftRegisterKeys) was last updated on Sep 15, 2021.

Text editor powered by tinymce.