Similar to reading the standard buttons, the capacitive touch buttons can be checked to see if they have been touched to perform some action.

Using the library, there are 7 touch buttons: cpx.touch_A1 through cpx.touch_A7. The sensitivity of the touchpads is one value for all pads set via cpx.adjust_touch_threshold. Setting the threshold is not required if the default sensitivity is ok.

Below is an example of turning on NeoPixels green if pads A4 through A7 are touched. Pressing Button A clears the lights.

# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries
# SPDX-License-Identifier: MIT

from import cpx

# NeoPixel blank out
cpx.pixels.fill( (0, 0, 0) )

# Set sensitivity of all touch pads

# Check for touch, if so light the nearby NeoPixel green
while True:
    if cpx.touch_A4:
        cpx.pixels[0] = (0, 30, 0)
    if cpx.touch_A5:
        cpx.pixels[1] = (0, 30, 0)
    if cpx.touch_A6:
        cpx.pixels[3] = (0, 30, 0)
    if cpx.touch_A7:
        cpx.pixels[4] = (0, 30, 0)
    if cpx.button_a:  # blank lights on Button A
        cpx.pixels.fill( (0, 0, 0) )

Using Lower-Level Library Calls to Save Memory

The following code is very lightweight for reading the capacitive touch pads. It is perfect for when you want to add additional code such as Crickit or other library modules which may require a lot of memory.

Reading keys uses the following libraries:

  • touchio - contains the touch functions
  • board - we use board definitions for the switches
  • time - contains the sleep function to pause between readings

The code to set up four cap touch switches and print their values is as follows: 

# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT

import time

import board
import touchio

touch_A1 = touchio.TouchIn(board.A1)
touch_A2 = touchio.TouchIn(board.A2)
touch_A5 = touchio.TouchIn(board.A5)
touch_A6 = touchio.TouchIn(board.A6)

while True:
    value_A1 = touch_A1.raw_value
    value_A2 = touch_A2.raw_value
    value_A5 = touch_A5.raw_value
    value_A6 = touch_A6.raw_value
    print((value_A1, value_A2, value_A5, value_A6))

The documentation for touchio can be found here. There is not a current setting for sensitivity.

This guide was first published on Jul 26, 2018. It was last updated on Jun 18, 2024.

This page (CircuitPython) was last updated on Jun 18, 2024.

Text editor powered by tinymce.