touchio – Touch related IO — Adafruit CircuitPython 1 documentation (original) (raw)

The touchio module contains classes to provide access to touch IO typically accelerated by hardware on the onboard microcontroller.

All classes change hardware state and should be deinitialized when they are no longer needed if the program continues after use. To do so, either call deinit() or use a context manager. SeeLifetime and ContextManagers for more info.

For more information about working with the touchio module in CircuitPython, see this Learn guide page.

Limitations: touchio on RP2350 must have a pull-up resistor to 3.3V instead of ground and set the pull=Pull.UP parameter when constructing a TouchIn object, due to GPIO hardware limitations.

Example:

import touchio from board import *

touch_pin = touchio.TouchIn(D6) print(touch_pin.value)

This example will initialize the the device, and print thevalue.

Available on these boards

class touchio.TouchIn(pin: microcontroller.Pin, pull: digitalio.Pull | None = None)

Read the state of a capacitive touch sensor

Usage:

import touchio from board import *

touch = touchio.TouchIn(A1) while True: if touch.value: print("touched!")

Use the TouchIn on the given pin.

Parameters:

deinit() → None

Deinitialises the TouchIn and releases any hardware resources for reuse.

__enter__() → TouchIn

No-op used by Context Managers.

__exit__() → None

Automatically deinitializes the hardware when exiting a context. SeeLifetime and ContextManagers for more info.

value_: bool_

Whether the touch pad is being touched or not. (read-only)

True when raw_value > threshold.

raw_value_: int_

The raw touch measurement as an int. (read-only)

threshold_: int | None_

Minimum raw_value needed to detect a touch (and for value to be True).

When the TouchIn object is created, an initial raw_value is read from the pin, and then threshold is set to be 100 + that value.

You can adjust threshold to make the pin more or less sensitive:

import board import touchio

touch = touchio.TouchIn(board.A1) touch.threshold = 7300