The keypad module makes using the NeoKey 5x6 Ortho Snap-Apart with CircuitPython super simple. Combined with the Adafruit CircuitPython NeoPixel library, you can easily write Python code to read key presses and light up the NeoPixel LEDs.

The following example is written for the Adafruit Feather, but you can use the NeoKey Snap with any CircuitPython compatible microcontroller as long as it has enough pins available. Depending on the configuration you choose, you'll need a maximum of twelve IO pins (eleven pins for the keys and one pin for the NeoPixels), a ground pin, and a power pin.

CircuitPython Wiring

The pins are explained on the Pinouts page, but here is a quick summary. The COL pins across the tops of the keys are the column pins. The ROW pins across the sides of the keys are the row pins. The I or IN pins are input for the NeoPixels. The O or OUT pins are NeoPIxel output (for connecting to the input on another set of NeoPixels).

You'll want to solder wires (leave them long enough to connect to a breadboard or solder to a microcontroller directly!) to the following pins, viewing the board from the front: all of the COL pins along the top, all of the ROW pins along the left side, the I pin in the top left, and any VIN and GND.

Connect the NeoKey 5x6 Ortho Snap-Apart sheet to your board exactly as follows. The following shows it wired to a Feather RP2040, but this exact setup will work with any Adafruit Feather. For the purposes of this diagram, references to column pin numbers are 1 to 6, left to right, and references to row pin numbers are 1 to 5, top to bottom.

The diagram shows the NeoKey Snap from the front, e.g. the side to which the keys are socketed. The pin labels are on the back of the NeoKey Snap sheet.

  • Board 3V to NeoKey Snap VIN
  • Board GND to NeoKey Snap GND
  • Board D5 to NeoKey Snap IN (I) top left
  • Board D13 to NeoKey Snap COL 1
  • Board D12 to NeoKey Snap COL 2
  • Board D11 to NeoKey Snap COL 3
  • Board D10 to NeoKey Snap COL 4
  • Board D9 to NeoKey Snap COL 5
  • Board D6 to NeoKey Snap COL 6
  • Board D4 to NeoKey Snap ROW 1
  • Board A3 to NeoKey Snap ROW 2
  • Board A2 to NeoKey Snap ROW 3
  • Board A1 to NeoKey Snap ROW 4
  • Board A0 to NeoKey Snap ROW 5

The number of available GPIO pins on your microcontroller determines how many keys you can support. The RP2040 has 23 GPIO pins, so in theory, the biggest key matrix it could support would be an 11x11 matrix and still utilise the NeoPixels. That's 121 keys in an 11x11 matrix, in which you would need 22 GPIO pins to handle 11 rows and 11 columns.

CircuitPython Usage

To demonstrate using NeoKey 5x6 Ortho Snap-Apart sheet with CircuitPython, you'll install the necessary library, update your code, and then optionally connect to the serial console to see information printed out.

To use a  with CircuitPython, you need to first install the NeoPixel library into the lib folder on your CIRCUITPY drive.

Then you need to update code.py.

Click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.

# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
NeoKey 5x6 Ortho Snap-Apart simple key press NeoPixel demo.
"""
import board
import keypad
import neopixel

COLUMNS = 6
ROWS = 5

pixels = neopixel.NeoPixel(board.D5, 30, brightness=0.3)

keys = keypad.KeyMatrix(
    row_pins=(board.D4, board.A3, board.A2, board.A1, board.A0),
    column_pins=(board.D13, board.D12, board.D11, board.D10, board.D9, board.D6),
    columns_to_anodes=False,
)


def key_to_pixel_map(key_number):
    row = key_number // COLUMNS
    column = (key_number % COLUMNS)
    if row % 2 == 1:
        column = COLUMNS - column - 1
    return row * COLUMNS + column


pixels.fill((0, 0, 0))  # Begin with pixels off.
while True:
    key_event = keys.events.get()
    if key_event:
        print(key_event)
        if key_event.pressed:
            pixels[key_to_pixel_map(key_event.key_number)] = (255, 0, 0)
        else:
            pixels.fill((0, 0, 0))

Now, press a key to see the associated LED light up! If you connect to the serial console, you'll see the key event printed out when you press or release a key.

After importing the necessary library and modules, you configure the number of columns and rows. For the full sheet, there are 6 columns and 5 rows.

Then, you setup the NeoPixels and the keys. There are 30 NeoPixels, and the brightness is set to 30%. The keys use the KeyMatrix feature of the keypad module. KeyMatrix expects a tuple of row_pins, and a tuple of column_pins. For the NeoKey Snap, you'll also want to set columns_to_anodes=False.

Next, is a helper function to map the NeoPixels to the keys. The key numbers read from left to right across each row from top to bottom. The NeoPixels begin in the upper left, and then alternate direction across each row. Simply using the raw NeoPixel number won't match the key number! Therefore, you include a helper function to map the NeoPixels to the appropriate key.

Before the loop, you turn off the NeoPixels by setting them to (0, 0, 0) so the program begins with the NeoPixels off.

Inside the loop, we begin by getting the key events. Next, we check to see if there has been a key event, e.g. a key has been pressed or released. If so, we print the key event to the serial console, and then check to see if the key event is a key press event. If it is a press event, you light up the associated pixel red using the helper function to identify the pixel number based on key number. Otherwise, you turn all the pixels off.

That's all there is to reading key events and lighting up the associated NeoPixel LED using CircuitPython!

Snap it!

The clever design of the NeoKey Snap is that it is made to be snapped apart! This example shows you how to make a 2x4 keypad. The wiring is essentially the same, except with less wires. Carefully break off the top and bottom bar of PCB (the bits with the product name and "Sold As Single Sheet" written on them). Then, snap off a full row. Finally, snap off two columns. Ta-da! 2x4 keypad.

Wire it up as follows.

  • Board 3V to NeoKey Snap VIN
  • Board GND to NeoKey Snap GND
  • Board D5 to NeoKey Snap IN (I) top left
  • Board D13 to NeoKey Snap COL 1
  • Board D12 to NeoKey Snap COL 2
  • Board D4 to NeoKey Snap ROW 1
  • Board A3 to NeoKey Snap ROW 2
  • Board A2 to NeoKey Snap ROW 3
  • Board A1 to NeoKey Snap ROW 4

Then, simply change the number of COLUMNS and ROWS at the top of the file so COLUMNS = 2 and ROWS = 4.

# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
NeoKey 4x2 Ortho Snap-Apart simple key press NeoPixel demo.
"""
import board
import keypad
import neopixel

COLUMNS = 2
ROWS = 4

pixels = neopixel.NeoPixel(board.D5, 30, brightness=0.3)

keys = keypad.KeyMatrix(
    row_pins=(board.D4, board.A3, board.A2, board.A1),
    column_pins=(board.D13, board.D12),
    columns_to_anodes=False,
)


def key_to_pixel_map(key_number):
    row = key_number // COLUMNS
    column = (key_number % COLUMNS)
    if row % 2 == 1:
        column = COLUMNS - column - 1
    return row * COLUMNS + column


pixels.fill((0, 0, 0))  # Begin with pixels off.
while True:
    key_event = keys.events.get()
    if key_event:
        print(key_event)
        if key_event.pressed:
            pixels[key_to_pixel_map(key_event.key_number)] = (255, 0, 0)
        else:
            pixels.fill((0, 0, 0))

Now, press a key to see the associated LED light up! If you connect to the serial console, you'll see the key event printed out when you press or release a key.

That's all there is to customising the size of your NeoKey Ortho Snap-Apart key matrix!

This guide was first published on Jul 27, 2021. It was last updated on 2021-07-27 16:54:09 -0400.

This page (CircuitPython) was last updated on May 19, 2022.

Text editor powered by tinymce.