It's easy to use the NeoKey 1x4 with CircuitPython using the Adafruit CircuitPython NeoKey library. It allows you to write Python code to read the key presses and control the NeoPixel LEDs.
You can use the NeoKey 1x4 with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.
CircuitPython Microcontroller Wiring
First wire up a NeoKey 1x4 breakout to your board exactly as follows. The following is the breakout wired to a Feather using the STEMMA connector:
- Board 3V to breakout VIN (red wire)
- Board GND to breakout GND (black wire)
- Board SCL to breakout SCL (yellow wire)
- Board SDA to breakout SDA (blue wire)
The following is the breakout wired to a Feather using a solderless breadboard:
- Board 3V to breakout VIN (red wire)
- Board GND to breakout GND (black wire)
- Board SCL to breakout SCL (yellow wire)
- Board SDA to breakout SDA (blue wire)
Python Computer Wiring
Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported.
Here's the Raspberry Pi wired with I2C using the STEMMA connector:
- Pi 3V to breakout VIN (red wire)
- Pi GND to breakout GND (black wire)
- Pi SCL to breakout SCL (yellow wire)
- Pi SDA to breakout SDA (blue wire)
Here's the Raspberry Pi wired with I2C using a solderless breadboard:
- Pi 3V to breakout VIN (red wire)
- Pi GND to breakout GND (black wire)
- Pi SCL to breakout SCL (yellow wire)
- Pi SDA to breakout SDA (blue wire)
Python Installation of NeoKey Library
You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!
Once that's done, from your command line run the following command:
pip3 install adafruit-circuitpython-neokey
If your default Python is version 3 you may need to run pip
instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!
CircuitPython & Python Usage
To demonstrate using this breakout with CircuitPython, you'll install the necessary libraries, update your code, and then connect to the serial console to see the information printed out.
To use the NeoKey 1x4 breakout with CircuitPython, you need to first install the NeoKey library, and its dependencies, 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 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """NeoKey simpletest.""" import board from adafruit_neokey.neokey1x4 import NeoKey1x4 # use default I2C bus i2c_bus = board.I2C() # Create a NeoKey object neokey = NeoKey1x4(i2c_bus, addr=0x30) print("Adafruit NeoKey simple test") # Check each button, if pressed, light up the matching neopixel! while True: if neokey[0]: print("Button A") neokey.pixels[0] = 0xFF0000 else: neokey.pixels[0] = 0x0 if neokey[1]: print("Button B") neokey.pixels[1] = 0xFFFF00 else: neokey.pixels[1] = 0x0 if neokey[2]: print("Button C") neokey.pixels[2] = 0x00FF00 else: neokey.pixels[2] = 0x0 if neokey[3]: print("Button D") neokey.pixels[3] = 0x00FFFF else: neokey.pixels[3] = 0x0
Now press the buttons to see the associated NeoPixel LED light up!
Connect to the serial console to see the messages printed out.
That's all there is to using the NeoKey 1x4 breakout with CircuitPython!
Multi-NeoKey 1x4 Usage
The address jumpers on the back of the NeoKey 1x4 breakout enable you to chain together up to 16 of these breakouts on a single I2C bus. This example shows how to connect two and use them together.
This example requires minimal soldering.
Address Jumper Soldering
Use solder to bridge the A0 jumper (highlighted in green below) on the back of the board.
NeoKey Wiring
Use a STEMMA QT cable to connect a second NeoKey 1x4 breakout to your current wiring setup. The example below is a Feather M4, but it will work the same on any CircuitPython-compatible board or Python computer.
CircuitPython and Python Multi-NeoKey 1x4 Usage
To demonstrate using this breakout with CircuitPython, you'll install the necessary libraries, update your code, and then connect to the serial console to see the information printed out.
To use two NeoKey 1x4 breakouts with CircuitPython, you need to first install the NeoKey library, and its dependencies, 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 """Example for connecting two NeoKey 1x4 breakouts. Requires bridging the A0 jumper on one board.""" import board from rainbowio import colorwheel from adafruit_neokey.neokey1x4 import NeoKey1x4 # Create a NeoKey object neokey1 = NeoKey1x4(board.I2C()) neokey2 = NeoKey1x4(board.I2C(), addr=0x31) keys = [ (neokey1, 0, colorwheel(0)), (neokey1, 1, colorwheel(32)), (neokey1, 2, colorwheel(64)), (neokey1, 3, colorwheel(96)), (neokey2, 0, colorwheel(128)), (neokey2, 1, colorwheel(160)), (neokey2, 2, colorwheel(192)), (neokey2, 3, colorwheel(224)), ] off = (0, 0, 0) # Check each button, if pressed, light up the matching NeoPixel! while True: for i in range(8): neokey, key_number, color = keys[i] if neokey[key_number]: print("Button", i) neokey.pixels[key_number] = color else: neokey.pixels[key_number] = off
Now press the buttons to see the associated NeoPixel LED light up!
Connect to the serial console to see the messages printed out.
That's all there is to using two NeoKey 1x4 breakouts with CircuitPython!
Text editor powered by tinymce.