All articles

    The TTP223 Touch Button and the ESP32's Touch Sensor

    How to make a button that needs no pressing: the idea of measuring capacitance, the TTP223 module and its wiring, the jumpers that switch the output to LOW or to toggle mode, the long press in code, enlarging the touch pad and touching through plastic and glass, the ESP32's built-in touch sensors with touchRead and the interrupt, and false-touch problems.

    Updated 6 min read

    Touch buttons look cleaner, have no moving parts to wear out, and can be placed behind a sheet of acrylic or glass. That makes them excellent for smart home projects and devices you want to look clean.

    The finger approaches the pad, the capacitance changes, and the output goes HIGH
    The finger approaches the pad, the capacitance changes, and the output goes HIGH

    How does it work?

    The circuit measures the capacitance of the copper pad. Your body has capacitance, so when your finger approaches, the capacitance rises a little, and the chip senses the difference. You don't need to touch the metal itself.

    The idea in detail: the chip charges and discharges the pad thousands of times a second, and measures how long the charging takes. The pad on its own has a small capacitance (a few picofarads), so it charges quickly. Your finger adds capacitance, so charging slows, and the chip says "there's a touch". That's why the chip calibrates itself to the normal capacitance as soon as it starts, and tracks it slowly if humidity or temperature change it.

    The TTP223 module

    • A supply of 2 to 5.5 V (so it works with Arduino and ESP32).
    • 3 pins: VCC, GND and SIG (or I/O).
    • A ready digital output; no library needed.
    • Its consumption is very low (microamps), so it suits devices on a battery.
    pinMode(2, INPUT);
    if (digitalRead(2) == HIGH) {
      // touched
    }

    Wiring: VCC to 5V or 3.3V (the same voltage as the board you'll read it from), GND to ground, and SIG to any digital pin. No pull-down resistor is needed, because the output drives HIGH or LOW by itself.

    The two jumpers (A and B)

    The module has two solder pads that change its behaviour:

    ABBehaviour
    OpenOpenHIGH as long as you're touching (the default)
    ClosedOpenLOW as long as you're touching
    OpenClosedEach touch toggles the state
    ClosedClosedToggle with an inverted output

    Check with the seller for your model, because the arrangement can differ.

    Toggle mode is useful if the module will switch something without a microcontroller: for example connected directly to a relay module or a transistor, each touch turns it on or off. But note: if the power goes off, the state returns to the default.

    Short and long touch in code

    It's better to leave the module in its default mode and put the logic in code. That way you can tell a short touch from a long press:

    const int TOUCH = 2;
    unsigned long t0 = 0;
    bool was = false;
    
    void loop() {
      bool now = digitalRead(TOUCH);
      if (now && !was) t0 = millis();          // the touch began
      if (!now && was) {                       // the finger was lifted
        unsigned long d = millis() - t0;
        if (d > 800) Serial.println("Long press");
        else if (d > 30) Serial.println("Touch");
      }
      was = now;
    }

    A short touch might turn on the light, and a long one change the mode. And the 30 ms ignores any fast false pulse. It's the same idea as buttons and debouncing.

    Behind a cover

    • It works behind thin plastic or glass (a few millimetres).
    • The thicker the cover, the lower the sensitivity.
    • Stick the module firmly to the cover, with no air between them.

    Enlarge the pad

    The pad on the module is small. If the cover is thick or you want a bigger button:

    • Stick a larger piece of copper or aluminium (foil) to the back of the cover, and connect it with a short wire to the module's pad.
    • A suitable size is close to the size of a fingertip (1–2 cm). Larger than that can sense things from a distance.
    • A long wire adds capacitance and picks up noise, so keep it as short as possible.
    • Some modules have a place for a small capacitor (Cs) that reduces the sensitivity if the button senses from too far away.

    Things to watch out for

    • The chip calibrates itself when it starts. Don't touch it while it's powering up.
    • Water and moisture on the surface can cause false touches.
    • A noisy supply (a cheap adapter or a motor) confuses it. Add a capacitor, and see reducing noise.
    • Nearby metal (a steel chassis or screws) changes the capacitance. Keep the module away from it.
    • Some modules have a timeout: if you touch for more than about 10 seconds, it recalibrates and treats the touch as "normal".

    ESP32: touch without a module

    The ESP32 has built-in touch pins (T0 to T9 on the original ESP32). Connect a wire or a piece of copper to the pin:

    void loop() {
      int v = touchRead(T0);   // T0 = GPIO4
      Serial.println(v);
      delay(200);
    }
    • On the original ESP32, the value falls when you touch.
    • Watch the numbers in the Serial Monitor, and choose a threshold between "touched" and "not touched".
    • These pins can also wake the chip from deep sleep.

    With an interrupt instead of continuous reading

    void gotTouch() {
      touched = true;          // a volatile bool variable
    }
    
    void setup() {
      touchAttachInterrupt(T0, gotTouch, 40);  // threshold = 40
    }

    If the reading drops below 40, the function is called. Choose the threshold from the numbers you saw: for example, if untouched is 70 and touched is 15, set about 40 (in the middle).

    Note: on the ESP32-S2 and S3 the value rises with a touch instead of falling, and the numbers are much larger. So look at the numbers yourself before setting the threshold.

    Common problems

    SymptomUsual cause
    Works on the laptop but misbehaves on the adapterNoise in the adapter or the ground
    It triggers by itself every so oftenMoisture, a long wire, or a pad that's too large
    It doesn't sense through the coverThe cover is thick, or there's air between it and the module
    The state is inverted after power-upYou touched it while it was calibrating
    The ESP32 numbers jump aroundThe wire is long or not firmly fixed

    FAQ

    Can I touch it with gloves on? An ordinary glove keeps the finger away, so sensing weakens or disappears, exactly like a phone screen.

    Should I use a TTP223 or the ESP32 pins? If you're using an ESP32 anyway, the built-in pins are enough and you don't need extra parts. If you're using an Arduino Uno or want a ready, stable output with no tuning, the TTP223 is easier.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.