Text Editor

Adafruit recommends using the Mu editor for editing your CircuitPython code. You can get more info in this guide.

Alternatively, you can use any text editor that saves simple text files.

Code

Copy the code shown below and paste it into Mu, then save it to your CIRCUITPY drive with the name code.py

# SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import time
import board
import busio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
import usb_hid
import adafruit_mpr121

# Create I2C bus.
i2c = busio.I2C(board.SCL, board.SDA)
# Create MPR121 object.
mpr121 = adafruit_mpr121.MPR121(i2c)
# Note you can optionally change the address of the device:
# mpr121 = adafruit_mpr121.MPR121(i2c, address=0x91)
kbd = Keyboard(usb_hid.devices)
keylist = [
    Keycode.RIGHT_ARROW,
    Keycode.ONE,
    Keycode.TWO,
    Keycode.THREE,
    Keycode.FOUR,
    Keycode.W,
    Keycode.A,
    Keycode.S,
    Keycode.D,
    Keycode.B,
    Keycode.I,
    Keycode.H,
]

# Loop forever testing each input and sending keystrokes when they're touched.
while True:
    # Loop through all 12 inputs (0-11).
    for i in range(12):
        # Call is_touched and pass it then number of the input.  If it's touched
        # it will return True, otherwise it will return False.
        if mpr121[i].value:
            # print("Input {} touched!".format(i))
            kbd.send(keylist[i])
    time.sleep(0.15)  # Small delay to keep from spamming output messages.

How It Works

Libraries

First, the libraries are imported, including the USB HID keyboard and adafruit_mpr121 library for the cap touch breakout.

import time
import board
import busio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
import usb_hid
import adafruit_mpr121

Setup

The i2c bus and sensor objects are created next:

i2c = busio.I2C(board.SCL, board.SDA)
mpr121 = adafruit_mpr121.MPR121(i2c)

Keyboard

The keyboard object is created and a list of keycodes that will be sent. You can adjust these to suit other uses if you like.

kbd = Keyboard(usb_hid.devices)
keylist = [
    Keycode.RIGHT_ARROW,
    Keycode.ONE,
    Keycode.TWO,
    Keycode.THREE,
    Keycode.FOUR,
    Keycode.W,
    Keycode.A,
    Keycode.S,
    Keycode.D,
    Keycode.B,
    Keycode.I,
    Keycode.H,
]

Main Loop

The main loop is super simple! It loops through twelve times, checking each cap touch pad to see if it's being touched.

If a pad is being touched, the corresponding key from the list is sent.

Then, a short pause for debouncing and it's done! This cycle repeats forever and ever.

while True:
    # Loop through all 12 inputs (0-11).
    for i in range(12):
        if mpr121[i].value:
            kbd.send(keylist[i])
    time.sleep(0.15)  # Small delay to keep from spamming output messages.

Use the Touch Control

First, download Animus from this link and then install it. For non mac os operating systems, you will need to grab your code from the GitHub page.

With the QT Py plugged into your computer over USB, launch Animus and start some music playing for it to react to.

Notice the UI on the side of the screen? You don't need to mouse and click on those any longer! Try touching your touch controller pads instead!

Now, you can mix the visuals in style!

This guide was first published on Feb 02, 2021. It was last updated on Feb 02, 2021.

This page (Code the Touch Controller) was last updated on Oct 21, 2021.

Text editor powered by tinymce.