Projects / ESP32 touch pin foil buttons

ESP32 touch pin foil buttons

Beginner1.2 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32

    Make the pads

    Stick three squares of foil to card and tape a jumper wire end under each.

  2. ESP32

    Wire the pads

    GPIO4, GPIO13 and GPIO14.

  3. ESP3211.0 mA

    Wire the LEDs

    GPIO25 and GPIO26, each through 220 ohms, short legs to GND.

  4. ESP32

    Upload the sketch

    Open the Serial Monitor at 115200 baud and keep your hands off the pads.

  5. ESP32

    Read the idle numbers

    Each pad prints its idle value and threshold.

  6. ESP3211.1 mA

    Touch pad 1

    The green LED toggles.

  7. ESP32

    Touch pad 2, then pad 3

    Red toggles, then both go off.

In short

How do you build an ESP32 touch pin foil buttons?

The ESP32 senses touch on some pins with no extra parts. Foil pads on a card are read with touchRead, each with its own threshold, and light an LED or switch both off. You need ESP32-WROOM-32 DevKitC, LED 5 mm red, LED 5 mm green, Resistor 220 Ω × 2, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 70 minutes.

Level
Beginner
Time
About 70 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Foil pad 1 wireESP32 GPIO4 (touch 0)Green LED pad
Foil pad 2 wireESP32 GPIO13 (touch 4)Red LED pad
Foil pad 3 wireESP32 GPIO14 (touch 6)Both off pad
Green LED long legESP32 GPIO25 through a 220 ohm resistorOutput
Red LED long legESP32 GPIO26 through a 220 ohm resistorOutput
Both LED short legsESP32 GNDCommon ground

Code

The classic ESP32 has built-in touch inputs on some pins, which measure how long a pin takes to charge. A finger on a pad adds capacitance and makes the reading fall. At start-up the sketch reads each pad eight times while it is untouched to find its normal value and sets a threshold at sixty percent of that, so it adapts to your own foil and wires. A touch counts only if the pad stays below the threshold for three checks in a row, which stops a brush or a bump from toggling a light. The first pad toggles green, the second toggles red and the third switches both off. Keep your fingers away from the pads at power-up, and avoid pins GPIO2, GPIO12 and GPIO15 for this job because they affect the boot of some boards. This works on the original ESP32. Newer chips such as the S3 read the other way round, so check your values.

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// ESP32 foil touch buttons. On the classic ESP32 the touch reading FALLS when you touch the pad.
const uint8_t PAD_PINS[3] = { 4, 13, 14 };         // touch 0, touch 4, touch 6
const uint8_t LED_GREEN = 25;
const uint8_t LED_RED = 26;

int baseline[3];
int threshold[3];
bool padDown[3] = { false, false, false };
uint8_t hits[3] = { 0, 0, 0 };
bool greenOn = false, redOn = false;

int readAverage(uint8_t pin) {
  long sum = 0;
  for (int i = 0; i < 8; i++) { sum += touchRead(pin); delay(2); }
  return sum / 8;
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_RED, OUTPUT);
  delay(500);                                      // leave the pads untouched while calibrating
  for (int i = 0; i < 3; i++) {
    baseline[i] = readAverage(PAD_PINS[i]);
    threshold[i] = baseline[i] * 6 / 10;           // 60 percent of the idle reading
    Serial.printf("Pad %d idle %d, threshold %d\n", i + 1, baseline[i], threshold[i]);
  }
}

void loop() {
  for (int i = 0; i < 3; i++) {
    bool below = readAverage(PAD_PINS[i]) < threshold[i];
    if (below) { if (hits[i] < 3) hits[i]++; } else { hits[i] = 0; }
    bool nowDown = hits[i] >= 3;                   // must stay touched for three readings
    if (nowDown && !padDown[i]) {                  // a fresh touch
      if (i == 0) greenOn = !greenOn;
      if (i == 1) redOn = !redOn;
      if (i == 2) { greenOn = false; redOn = false; }
      Serial.printf("Pad %d touched\n", i + 1);
    }
    padDown[i] = nowDown;
  }
  digitalWrite(LED_GREEN, greenOn);
  digitalWrite(LED_RED, redOn);
  delay(20);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

Pad 1 idle 71, threshold 42
Pad 2 idle 70, threshold 42
Pad 3 idle 70, threshold 42

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Hands offBoth LEDs off
Touch pad 1Green toggles
Touch pad 2Red toggles
Touch pad 3Both off
Brush a pad quicklyNo change

If it does not work

Touches missed

Raise the threshold fraction, or press more firmly on the foil.

Lights change by themselves

Lower the threshold fraction and keep wires short and still.

Idle value is very low

The wire may be touching something, so check it.

LED stays off

Check the LED direction and the resistor.

Does not boot after wiring

Unplug anything on GPIO2, GPIO12 or GPIO15.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the ESP32 touch pin foil buttons?

ESP32-WROOM-32 DevKitC, LED 5 mm red, LED 5 mm green, Resistor 220 Ω × 2, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the ESP32 touch pin foil buttons take to build?

About 70 minutes for a beginner maker, in 7 steps.

How do I know the ESP32 touch pin foil buttons works?

Hands off: Both LEDs off. Touch pad 1: Green toggles. Touch pad 2: Red toggles.

Touches missed: what should I check?

Raise the threshold fraction, or press more firmly on the foil.

Lights change by themselves: what should I check?

Lower the threshold fraction and keep wires short and still.

Idle value is very low: what should I check?

The wire may be touching something, so check it.

LED stays off: what should I check?

Check the LED direction and the resistor.