The code for the 5-pad capacitive touch sensor can be found below. It cannot be swapped with either of the single pad boards as they are wired differently.
# SPDX-FileCopyrightText: 2019 Mikey Sklar for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import time
import board
from digitalio import DigitalInOut, Direction

# set the GPIO input pins
pad0_pin = board.D22
pad1_pin = board.D21
pad2_pin = board.D17
pad3_pin = board.D24
pad4_pin = board.D23

pad0 = DigitalInOut(pad0_pin)
pad1 = DigitalInOut(pad1_pin)
pad2 = DigitalInOut(pad2_pin)
pad3 = DigitalInOut(pad3_pin)
pad4 = DigitalInOut(pad4_pin)

pad0.direction = Direction.INPUT
pad1.direction = Direction.INPUT
pad2.direction = Direction.INPUT
pad3.direction = Direction.INPUT
pad4.direction = Direction.INPUT

pad0_already_pressed = True
pad1_already_pressed = True
pad2_already_pressed = True
pad3_already_pressed = True
pad4_already_pressed = True

while True:

    if pad0.value and not pad0_already_pressed:
        print("Pad 0 pressed")
    pad0_already_pressed = pad0.value

    if pad1.value and not pad1_already_pressed:
        print("Pad 1 pressed")
    pad1_already_pressed = pad1.value

    if pad2.value and not pad2_already_pressed:
        print("Pad 2 pressed")
    pad2_already_pressed = pad2.value

    if pad3.value and not pad3_already_pressed:
        print("Pad 3 pressed")
    pad3_already_pressed = pad3.value

    if pad4.value and not pad4_already_pressed:
        print("Pad 4 pressed")
    pad4_already_pressed = pad4.value

    time.sleep(0.1)

Running the Code

Open a terminal on your Pi.

Pull Down the script directly onto your Raspberry Pi using wget.

$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/5pad.py
$ sudo python3 ./5pad.py

The program works in a very similar way to the programs for the momentary and toggle sensor boards. If you're having trouble understanding how the code works for the 5-pad, have a look at the code on the previous section of this guide first.

This guide was first published on Apr 07, 2014. It was last updated on Mar 29, 2024.

This page (Programming the 5-Pad Sensor) was last updated on Mar 29, 2024.

Text editor powered by tinymce.