Once you've finished setting up your KBRP2040 with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.

To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.

# SPDX-FileCopyrightText: 2021 Collin Cunningham for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from digitalio import DigitalInOut, Direction, Pull

# The pins connected to each switch/button
buttonpins = [board.D3, board.D4, board.D5]
# The pins connected to each LED
ledpins = [board.D2, board.D6, board.D7]

# our array of button & LED objects
buttons = []
leds = []

# The keycode sent for each switch/button
buttonkeys = [Keycode.B, Keycode.C, Keycode.A]
buttonspressed = [False, False, False]
buttonspressedlast = [False, False, False]

# the keyboard object!
kbd = Keyboard(usb_hid.devices)
# we're americans :)
layout = KeyboardLayoutUS(kbd)

# make all button pin objects, make them inputs w/pullups
for pin in buttonpins:
    button = DigitalInOut(pin)
    button.direction = Direction.INPUT
    button.pull = Pull.UP

# make all LED objects, make them outputs
for pin in ledpins:
    led = DigitalInOut(pin)
    led.direction = Direction.OUTPUT

# set up the status LED
statusled = DigitalInOut(board.D13)
statusled.direction = Direction.OUTPUT

print("Waiting for button presses")

def pressbutton(index):
    switch_led = leds[index]  # find the switch LED
    k = buttonkeys[index]  # get the corresp. keycode/str
    switch_led.value = True  # turn on LED
    kbd.press(k)  # send keycode

def releasebutton(index):
    switch_led = leds[index]  # find the switch LED
    k = buttonkeys[index]  # get the corresp. keycode/str
    switch_led.value = False  # turn on LED
    kbd.release(k)  # send keycode

while True:
    # check each button
    for button in buttons:
        i = buttons.index(button)
        if button.value is False:  # button is pressed?
            buttonspressed[i] = True  # save pressed button
            # was button not pressed last time?
            if buttonspressedlast[i] is False:
                print("Pressed #%d" % i)
            buttonspressed[i] = False  # button was not pressed
            if buttonspressedlast[i] is True:  # was button pressed last time?
                print("Released #%d" % i)
    # save pressed buttons as pressed last
    buttonspressedlast = list(buttonspressed)

Upload the Code and Libraries to the KBRP2040

After downloading the Project Bundle, plug your KBRP2040 into the computer USB port. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the KBRP2040's CIRCUITPY drive. 

  • lib folder
  • code.py

Your KBRP2040 CIRCUITPY drive should look like this after copying the lib folder and code.py file.

This guide was first published on Dec 06, 2021. It was last updated on Jun 17, 2024.

This page (Code) was last updated on Jun 17, 2024.

Text editor powered by tinymce.