Your microcontroller board has capacitive touch capabilities on multiple pins. The CircuitPython touchio module makes it simple to detect when you touch a pin, enabling you to use it as an input.

This section first covers using the touchio module to read touches on one pin. You'll learn how to setup the pin in your program, and read the touch status. Then, you'll learn how to read touches on multiple pins in a single example. Time to get started!

One Capacitive Touch Pin

The first example covered here will show you how to read touches on one pin.

Pin Location

  • 1MΩ resistor from KB2040 pin 9 to KB2040 GND

Reading Touch on the Pin

Download the following file by clicking the Download Project Bundle button. Drag the contents of the bundle, the code.py file, to your CIRCUITPY drive.

Then, connect to the serial console.

# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: Unlicense
"""
CircuitPython Capacitive Touch Pin Example - Print to the serial console when one pin is touched.
"""
import time
import board
import touchio

touch = touchio.TouchIn(board.D9)

while True:
    if touch.value:
        print("Pin touched!")
    time.sleep(0.1)

Now touch the pin indicated in the diagram above. You'll see Pin touched! printed to the serial console!

First you import three modules: time, board and touchio. This makes these modules available for use in your code. All three are built-in to CircuitPython, so you don't find any library files in the Project Bundle.

Next, you create the touchio.TouchIn() object, and provide it the pin name using the board module. You save that to the touch variable.

Inside the loop, you check to see if thepin is touched. If so, you print to the serial console. Finally, you include a time.sleep() to slow it down a bit so the output is readable.

That's all there is to reading touch on a single pin using touchio in CircuitPython!

Multiple Capacitive Touch Pins

The next example shows you how to read touches on multiple pins in a single program.

Pin Location

  • 1MΩ resistor from KB2040 pin 9 to KB2040 GND
  • 1MΩ resistor from KB2040 pin 10 to KB2040 GND

Reading Touch on the Pins

Download the following file by clicking the Download Project Bundle button. Drag the contents of the bundle, the code.py file, to your CIRCUITPY drive.

Then, connect to the serial console.

# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: Unlicense
"""
CircuitPython Capacitive Two Touch Pin Example - Print to the serial console when a pin is touched.
"""
import time
import board
import touchio

touch_one = touchio.TouchIn(board.D9)
touch_two = touchio.TouchIn(board.D10)

while True:
    if touch_one.value:
        print("Pin one touched!")
    if touch_two.value:
        print("Pin two touched!")
    time.sleep(0.1)

Touch the pins to see the messages printed to the serial console!

This example builds on the first. The imports remain the same.

The touchio.TouchIn() object is created, but is instead saved to touch_one. A second touchio.TouchIn() object is also created, the second pin is provided to it using the board module, and is saved to touch_two.

Inside the loop, we check to see if pin one and pin two are touched, and if so, print to the serial console Pin one touched! and Pin two touched!, respectively. The same time.sleep() is included.

If more touch-capable pins are available on your board, you can easily add them by expanding on this example!

The Available Touch-Capable Pins

  • A0 - CircuitPython board.A0. Arduino 26.
  • A1 - CircuitPython board.A1. Arduino 27.
  • A2 - CircuitPython board.A2. Arduino 28.
  • A3 - CircuitPython board.A3. Arduino 29.
  • TX - CircuitPython board.TX. Arduino 0.
  • RX - CircuitPython board.RX. Arduino 1.
  • D10 - CircuitPython board.D10. Arduino 10.
  • D2 - CircuitPython board.D2. Arduino 2.
  • D3 - CircuitPython board.D3. Arduino 3.
  • D4 - CircuitPython board.D4. Arduino 4.
  • D5 - CircuitPython board.D5. Arduino 5.
  • D6 - CircuitPython board.D6. Arduino 6.
  • D7 - CircuitPython board.D7. Arduino 7.
  • D8 - CircuitPython board.D8. Arduino 8.
  • D9 - CircuitPython board.D9. Arduino 9.
  • MISO - CircuitPython board.MISO. Arduino 20.
  • MOSI - CircuitPython board.MOSI. Arduino 19.
  • SCK - CircuitPython board.SCK. Arduino 18.

This guide was first published on Dec 03, 2021. It was last updated on 2021-12-26 23:44:32 -0500.

This page (Capacitive Touch) was last updated on Jan 24, 2022.

Text editor powered by tinymce.