Simple test

Ensure your device works with this simple test.

examples/touchpio_simpletest.py
 1# SPDX-FileCopyrightText: Copyright (c) 2023 Tod Kurt
 2#
 3# SPDX-License-Identifier: Unlicense
 4"""
 5touchpio_simpletest.py
 6
 7touchpio test with a single pad. It works just like touchio.
 8"""
 9
10import board
11
12import touchpio
13
14touch = touchpio.TouchIn(board.GP2)
15while True:
16    if touch.value:
17        print("touched!")

TouchInMulti simple test

Demo of TouchInMulti with 16 pads, calibration, and a bar-graph display of active pads.

examples/touchpio_mulit_simpletest.py
 1# SPDX-FileCopyrightText: Copyright (c) 2026 Tod Kurt
 2#
 3# SPDX-License-Identifier: Unlicense
 4
 5"""
 6touchpio_multi_simpletest.py — Demo of touchpio_multi on a Raspberry Pi Pico
 7
 8Wiring:
 9  GPIO 0–15  → touch pads (copper tape, PCB pads, etc.)
10  Each pad  → 1MΩ resistor → GND
11"""
12
13import time
14
15import board
16
17import touchpio
18
19touch = touchpio.TouchInMulti(
20    first_pin=board.GP0,
21    num_pins=16,
22    # num_samples     = 250,
23    touch_threshold=20,
24    # thresholds      = range(0, 5001, 100),
25    # thresholds      = range(0, 5001, 50),
26    # thresholds      = range(0, 2000, 100),
27    # thresholds      = range(100, 2001, 50),   # 40 levels
28    # thresholds      = range(100, 2001, 100),   # 20 levels
29    # touch_threshold = 1,                        # levels above baseline
30)
31
32print(f"Pull type: {touch.pull_type}")
33print("Calibrating — don't touch the pads...")
34time.sleep(1.0)
35touch.calibrate(num_samples=20)
36print("Baseline:", touch.baseline)
37print()
38
39print("Ready — touch the pads!")
40while True:
41    values = touch.read()
42    active = touch.touched_pins()
43
44    if active:
45        bar = " ".join(f"[P{i}:{'█' * min(values[i], 15)}]" for i in active)
46        print(bar)
47
48    time.sleep(0.03)

TouchIn many pins

Demo of 8 individual TouchIn instances. Note this exhausts all PIO state machines; for single-pin use, touchio is preferred.

examples/touchpio_touchin_many.py
 1# SPDX-FileCopyrightText: Copyright (c) 2023 Tod Kurt
 2#
 3# SPDX-License-Identifier: Unlicense
 4"""
 5touchpio_touchin_many.py
 6
 7touchpio test with 8 pads.
 8Note this uses up all PIO state machines, showing why TouchPIO for single
 9pins isn't that useful. Use `touchio` instead.
10"""
11
12import time
13
14import board
15import digitalio
16
17import touchpio
18
19pull_type = digitalio.Pull.UP
20# pull_type = digitalio.Pull.DOWN
21
22touch_pins = (board.GP0, board.GP1, board.GP2, board.GP3,
23              board.GP4, board.GP5, board.GP6, board.GP7, )  # fmt: skip
24
25touchins = []
26for pin in touch_pins:
27    touchin = touchpio.TouchIn(pin, pull_type=pull_type)
28    touchins.append(touchin)
29
30while True:
31    print(" ".join(["%d" % t.value for t in touchins]))
32    time.sleep(0.1)