Download Project Bundle
Once you've installed the latest version of CircuitPython onto your board, you'll need to grab the code, libraries and any assets included with the project.
Click the download project bundle button below to get the code, libraries and assets all in one!
# SPDX-FileCopyrightText: 2021 Noe Ruiz for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import time
import digitalio
import board
import usb_hid
import neopixel
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.animation.solid import Solid
from adafruit_led_animation.group import AnimationGroup
from adafruit_led_animation.sequence import AnimationSequence
from adafruit_led_animation.color import WHITE, BLACK
pixel_pin = board.A3
num_pixels = 7
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=.3, auto_write=False)
internal_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=1, auto_write=False)
# The button pins we'll use, each will have an internal pullup
buttonpins = [board.A0, board.A1, board.A2, board.D8, board.D9, board.D10]
# The keycode sent for each button, will be paired with a control key
buttonkeys = [
ConsumerControlCode.PLAY_PAUSE,
ConsumerControlCode.FAST_FORWARD,
ConsumerControlCode.VOLUME_INCREMENT,
ConsumerControlCode.MUTE,
ConsumerControlCode.VOLUME_DECREMENT,
ConsumerControlCode.REWIND
]
# the keyboard object!
cc = ConsumerControl(usb_hid.devices)
# our array of button objects
buttons = []
# make all pin objects, make them inputs w/pullups
for pin in buttonpins:
button = digitalio.DigitalInOut(pin)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
buttons.append(button)
animations = AnimationSequence(
AnimationGroup(
Pulse(pixels, speed=0.05, color=WHITE, period=6),
Solid(internal_pixel,color=BLACK),
),
)
print("Waiting for button presses")
while True:
# animate neopixel jewel
animations.animate()
# check each button
for button in buttons:
if not button.value: # pressed?
i = buttons.index(button)
print("Button #%d Pressed" % i)
while not button.value:
pass # wait for it to be released!
# type the keycode!
k = buttonkeys[i] # get the corresp. keycode
cc.send(k)
time.sleep(0.01)
Upload Code, Libraries and Assets
Unzip the project bundle and upload the files to the CIRCUITPY drive.
Your CIRCUITPY drive should look like this after you've uploaded the code, libraries and assets.
USB HID Keyboard and Mouse
Check out the guides linked below for more information on using the USB-HID library for CircuitPython.
Modify Controls
The six key switches are assigned the consumer controls for play/pause, previous track, next track, volume up, volume down and mute. Adjust the following code to change the keys control or the order of the arrangement. The buttonpins and buttonkeys are ordered chronologically.
# The button pins we'll use, each will have an internal pullup
buttonpins = [board.A1, board.A2, board.A3, board.D8, board.D9, board.D10]
# The keycode sent for each button, will be paired with a control key
buttonkeys = [
ConsumerControlCode.PLAY_PAUSE,
ConsumerControlCode.FAST_FORWARD,
ConsumerControlCode.VOLUME_INCREMENT,
ConsumerControlCode.MUTE,
ConsumerControlCode.VOLUME_DECREMENT,
ConsumerControlCode.REWIND
]
Page last edited January 22, 2025
Text editor powered by tinymce.