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: 2022 Ruiz Bros for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import digitalio
import board
import usb_hid
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode

# The button pins we'll use, each will have an internal pullup
buttonpins = [board.A1, board.A2, board.A3]

# The keycode sent for each button, will be paired with a control key
buttonkeys = [
    ConsumerControlCode.PLAY_PAUSE, # Center Foot Pad
    ConsumerControlCode.VOLUME_DECREMENT, #Left Foot Pad
    ConsumerControlCode.VOLUME_INCREMENT, # Right Foot Pad
]

# 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)

print("Waiting for button presses")

while True:
    # 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 three switches are assigned the consumer controls for play/pause volume up, and volume down. 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]

# The keycode sent for each button, will be paired with a control key
buttonkeys = [
    ConsumerControlCode.PLAY_PAUSE,
    ConsumerControlCode.VOLUME_INCREMENT,
    ConsumerControlCode.VOLUME_DECREMENT
]

This guide was first published on Feb 09, 2022. It was last updated on Feb 09, 2022.

This page (Code) was last updated on May 31, 2023.

Text editor powered by tinymce.