CAP1188 Capacitive Touch Sensing - Raspberry Pi Pico

The CAP11888 Capacitive Touch Sensor Breakout board can be used to create up to eight touch sensors.

Adafruit CAP1188 Sample Code

 1# Adafruit CAP1188 Capacitive Touch Sensor demo for Raspberry Pi Pico
 2
 3# To run, this file should be copied to the CIRCUITPY drive as code.py
 4
 5# Note: this sketch requires the optional adafruit_cap1188 library
 6# which should be copied as a folder to CIRCUITPY/lib on the Pico.
 7# The adafruit_cap1188 folder can be found within the library bundle
 8# available from https://circuitpython.org/libraries
 9
10# related docs:
11#  https://www.adafruit.com/product/1602
12#  https://learn.adafruit.com/adafruit-cap1188-breakout
13#  https://learn.adafruit.com/adafruit-cap1188-breakout/python-circuitpython
14#  https://docs.circuitpython.org/projects/cap1188/en/latest/
15
16# wiring for I2C on a Raspberry Pi Pico:
17#  pin 36, 3V3(OUT)      ->      sensor VIN
18#  pin 38, GND           ->      sensor GND
19#  pin 1, I2C0 SDA, GP0  ->      sensor SDA
20#  pin 2, I2C0 SCL, GP1  ->      sensor SCK
21
22#------------------------------------------------------------------
23import time
24import board
25import busio
26
27from adafruit_cap1188.i2c import CAP1188_I2C
28
29#------------------------------------------------------------------
30# initialize the hardware interface objects
31
32# this assumes the wiring noted above
33i2c = busio.I2C(scl=board.GP1, sda=board.GP0)
34cap = CAP1188_I2C(i2c)
35
36#------------------------------------------------------------------
37print("initial thresholds:", cap.thresholds)
38print("sensitivity:", cap.sensitivity)
39print("averaging:", cap.averaging)
40print("touch state:", cap.touched_pins)
41
42# Notes:
43#    cap.recalibrate()    will recalibrate all channels,
44#    cap[n].recalibrate() will recalibrate a single channel.
45#------------------------------------------------------------------
46
47# Constantly read the touch sensors and print messages.
48while True:
49    for i in range(1, 9):
50        if cap[i].value:
51            print("Pin", i, "touched, raw value is", cap[i].raw_value, "threshold is", cap[i].threshold)
52
53