Skip to content

uPesy now ships to the entire European Union

Contents Menu Expand Light mode Dark mode Auto light/dark mode

Using a capacitive sensor with the Raspberry Pi Pico in MicroPython

touch sensor module top

A capacitive sensor provides a touch button that behaves like a push button. They are ubiquitous in everyday devices. Unlike the classic push button, there is no mechanical movement of the button.

The module detects the pressure of a finger as soon as it is placed on a sensitive surface. With this basic module, it is impossible to know the intensity of the pressure exerted by the finger but only its presence.

Note

On ESP32 boards, this type of module is not very useful because the ESP32 already has about ten capacitive sensors on these pins. However, on the Raspberry Pi Pico, it can be used to replace a simple push button.

How does a capacitive sensor work?

As indicated in its name, it is a sensor based on the variation of the capacity of a capacitor. A capacitor consists of two metal plates placed face to face, separated by a substrate (here, the thickness of the FR4 PCB, which acts as a substrate)

touch sensor module bottom

On the back side, there is a metal plate

On this module, there is only one metal plate for the capacitor, the second one represented by your finger😉. A capacitor is formed with a tiny capacitance when your finger is placed on the front panel. This signal variation is sent back by the module to the pin SIG .

Note

It is important to remember that these capacitive sensors will not be as reliable as a mechanical pushbutton. They are susceptible to parasitic environmental capacitances that alter or distort the measurements.

Connections of the capacitive sensor

Here is the pinout to use this module:

Capacitive Sensor Module

Raspberry Pi Pico

SIG

GP26

VCC

3V3

GND

GND

capactive touch sensor pi pico micro python rpi picot

Electrical circuit to be made

Script to test the capacitive sensor of the module

One way to get the value is to use an analog pin (and thus the ADC of the Pi Pico)

from machine import Pin, ADC
import time


# Create an ADC object linked to pin 26
adc = ADC(Pin(26, mode=Pin.IN))

while True:

    # Read ADC and convert to voltage
    val = adc.read_u16()
    val = val * (3.3 / 65535)
    print(round(val, 2), "V") # Keep only 2 digits

    # Wait a bit before taking another reading
    time.sleep_ms(100)

Here is a preview of when you touch the module with your finger:

Pi Pico capacitive sensor test

When the module is touched, the output is either 0.01V or 3.3V (binary). We can take advantage of this and use any digital pin of the Pico to detect when it is pressed, making our code more straightforward to understand.

from machine import Pin

pin_sensor = Pin(26, mode=Pin.IN, pull=Pin.PULL_UP)

while True:
    if pin_sensor.value() == 1:
        print("Finger detected")

We use cookies to make your visit to the site as pleasant as possible. Privacy Policy