Raspberry Pi Pico
Toby's Macropad

This page describes a macropad that Toby made during one of our after-school clubs.

HID stands for Human Interface Device. Adafruit's Circuit Python library allows you to emulate a mouse or keyboard using the Pico. For this project, Toby used some pushbuttons.

This is a photograph of a circuit that is equivalent to the one Toby made (which had to be dismantled at the end of the club). There are 4 pushbuttons, each with one pin connected to GND and the other connected to one of GP14, GP15, GP16 and GP17. These pushbuttons are breadboard friendly and have only 2 pins. Other pushbuttons are fine to use.

Pico Circuit

The following Fritzing shows an equivalent circuit. In the diagram, I have used tiny buttons in the diagram. I have got some of these which I use whenever I need to squeeze a tiny button into a circuit. They are really useful.

Pico Circuit

This is the program we ended up with at the end of the club. It was built up over time. We have kept in the import statements for the multimedia shortcuts, even though they aren't included in this particular program.

When the first button (btnA) is pressed, the program types out a message, one letter at a time. Orginally, there was a line break at the end of the message - you can do this yourself by including \n in the quoted text.

After that, Toby wanted to make buttons for copy and paste. He did this by sending keystrokes. In fact, he used his buttons whilst modifying and completing his program, whenever he needed to copy or paste something.

For the final button, Toby wanted a way to select a line of text. Holding down shift and pressing the home key does this. That was added for the final button.

You can replace the code here with other shortcuts. You can read examples and get a full list of keycodes on
https://docs.circuitpython.org/projects/hid/en/latest/api.html You can also follow the link to download the library and copy it to your libs folder.

import board
from time import sleep
from digitalio import DigitalInOut, Pull
import usb_hid

from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode

kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(kbd)
cc = ConsumerControl(usb_hid.devices)

btnA = DigitalInOut(board.GP14)
btnA.switch_to_input(pull = Pull.UP)

btnB = DigitalInOut(board.GP15)
btnB.switch_to_input(pull = Pull.UP)

btnC = DigitalInOut(board.GP17)
btnC.switch_to_input(pull = Pull.UP)

btnD = DigitalInOut(board.GP16)
btnD.switch_to_input(pull = Pull.UP)

lastA = lastB = lastC = lastD = 1

while True:
    if btnA.value != lastA:
        if btnA.value:
            print("A released")
        else:
            print("A pressed")
            msg="Tobi-Wan Kenobi."
            for c in msg:
                layout.write(c)
                sleep(0.2)
    if btnB.value != lastB:
        if btnB.value:
            print("B released")
        else:
            print("B pressed")
            kbd.send(*[Keycode.CONTROL,Keycode.C])
    if btnC.value != lastC:
        if btnC.value:
            print("C released")
        else:
            print("C pressed")
            kbd.send(*[Keycode.CONTROL,Keycode.V])
    if btnD.value != lastD:
        if btnD.value:
            print("D released")
        else:
            print("D pressed")
            kbd.send(*[Keycode.SHIFT,Keycode.HOME])
    lastA = btnA.value
    lastB = btnB.value
    lastC = btnC.value
    lastD = btnD.value
    sleep(0.01)