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.
This example reads the buttons on a PyBadge or PyGamer. They both work the same way. (The joystick on the PyGamer is an analog joystick, and is read using AnalogIn
; it can't be read with keypad
).
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, which is the total length of the shift register or registers.
# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries # # SPDX-License-Identifier: MIT 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)
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.
# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries # # SPDX-License-Identifier: MIT import keypad import board 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
Page last edited January 22, 2025
Text editor powered by tinymce.