CircuitPython's touchio module makes it easy to use the two capacitive touch pads on the Neo Trinkey. With a few lines of code, you can use each touch pad as a separate input. This means you can control aspects of the built-in NeoPixel LEDs, such as brightness, simply by touching the board!

All the necessary modules and libraries for this example are included with CircuitPython for the Neo Trinkey, so you do not need to load any separate files onto your board.

NeoPixel and Touch Pad Location

The four NeoPixels on Neo Trinkey (indicated by the green box below) are located towards the opposite side of the board from the USB connector, arranged in a square.

The touch pads (indicated by the magenta squares), are on the opposite end of the board from the USB connector, next to the NeoPixels. When the "NeoPixel Trinkey" label is upright, the bottom pad is 1 and the top pad is 2.

Controlling NeoPixel Brightness

In the example below, click the Download Project Bundle button below to download the necessary files in a zip file. Extract the contents of the zip file, open the directory Adafruit_Neo_Trinkey/cap_touch_neopixel_brightness/ and then click on the directory that matches the version of CircuitPython you're using and copy to your CIRCUITPY drive.

You do not need to copy the entire Project Bundle to your board - all the necessary libraries are included in CircuitPython for Neo Trinkey!
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT

"""CircuitPython Capacitive Touch NeoPixel Brightness Control Example"""
import time
import board
import touchio
import neopixel
from rainbowio import colorwheel

touch1 = touchio.TouchIn(board.TOUCH1)
touch2 = touchio.TouchIn(board.TOUCH2)

pixels = neopixel.NeoPixel(board.NEOPIXEL, 4, auto_write=False)

def rainbow(color_index):
    for led in range(4):
        pixel_index = (led * 256 // 4) + color_index
        pixels[led] = colorwheel(pixel_index & 255)

touched = time.monotonic()
color = 0
while True:
    color = color + 1
    if color > 255:
        color = 0


    if time.monotonic() - touched < 0.15:
    if touch1.value:
        # Touch pad 1 to increase the brightness.
        pixels.brightness += 0.05
        touched = time.monotonic()
    elif touch2.value:
        # Touch pad 2 to decrease the brightness.
        pixels.brightness -= 0.05
        touched = time.monotonic()

NeoPixel rainbow!

Touch pad 2 to decrease the brightness. You can continue touching it to turn off the LEDs completely.

Touch pad 1 to increase the brightness. You can continue touching it to increase to maximum brightness.

Now, a look at the code.

First you import all the necessary modules and libraries to make them available in your code.

Next, you set up the touch pads and NeoPixels. To interact with hardware in CircuitPython, your code must let the board know where to look for the hardware and what to do with it. So, you create two touchio.TouchIn() objects, and provide the two touch pad pins using the board module. Then, you create a neopixel.NeoPixel() object, provide it the NeoPixel LED pin using the board module, tell it there are four LEDs, and set auto_write=False. You save this object to the variable pixels.

Next is the helper function to create the rainbow. It uses colorwheel to iterate over the rainbow across the four LEDs. This function requires a color index.

Before the loop, we create a touched variable and assign it to time.monotonic(), and a color variable and set it to 0.

Inside the loop, the first thing you do is set up the color variable to cycle from 0-255 and back to 0. This creates the color index that the rainbow helper requires. Then you call the rainbow helper and provide it the color variable.

Next, you use time.monotonic() to create a short delay. This is done the first time through the loop by comparing the current time to the time taken before the loop, and checking to see if it is less than 0.15 seconds. The subsequent times through the loop, it is compared to the last time you touched a pad. (Typically you would simply include a time.sleep(), but the only way to keep the rainbow going while you are controlling the brightness is to use time.monotonic().)

Finally, you check to see if touch pad 1 is touched, and if so, increase the brightness by 0.05, and set touched to time.monotonic() for use in the delay.

You do the same with touch pad 2, except this time, you decrease the brightness by 0.05.

That's all there is to controlling NeoPixel brightness using the capacitive touch pads on the Neo Trinkey with CircuitPython!

This guide was first published on Apr 10, 2021. It was last updated on May 18, 2024.

This page (Capacitive Touch NeoPixel Brightness) was last updated on May 18, 2024.

Text editor powered by tinymce.