Two ways to sense touch

Compare the ESP32's built-in touch pins with the touch module from the Sensor Kit, and light an LED with either.
  • Level:Beginner
  • Duration: 35 min
  • Ages 12+
  • Platform: ESP32

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Two ways to sense touch – EEMENTOR STEM™ lesson

The ESP32 has a hidden superpower: some of its pins can feel touch without any extra parts. In this lesson you compare that built-in touch sensing with the metal touch module from the Sensor Kit. Touching either one lights the LED.

What you'll learn

  • How the ESP32's capacitive touch pins work
  • How to calibrate a touch threshold automatically
  • How a touch module differs from built-in touch

What you need

  • ESP32 board, expansion board and USB cable (ESP32 IoT Starter Kit)
  • Metal touch module (45-in-1 Sensor Kit)
  • 1 LED and 1 resistor, 220 Ω
  • A jumper wire with a small piece of aluminium foil taped to its end, as the touch pad
  • Breadboard and jumper wires (ESP32 IoT Starter Kit)

Wiring

Breadboard wiring diagram: Two ways to sense touch
Breadboard view. Check each connection against the wiring table below.
From To
Touch module G GND
Touch module + 3V3
Touch module DO ESP32 GPIO 27
Touch module AO Not used
ESP32 GPIO 4 (touch pin T0) A loose jumper wire with foil on the end
ESP32 GPIO 26 220 Ω resistor, then the LED's long leg
LED short leg GND

Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.

The sketch

const int TOUCH_PAD = 4;      // built-in touch pin T0
const int MODULE_PIN = 27;
const int LED_PIN = 26;

int padBaseline;              // reading when nobody touches the pad
int moduleRest;               // module output when nobody touches it

void setup() {
  Serial.begin(115200);
  pinMode(MODULE_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  delay(500);

  long sum = 0;               // average a few readings, don't touch anything
  for (int i = 0; i < 20; i++) { sum += touchRead(TOUCH_PAD); delay(20); }
  padBaseline = sum / 20;
  moduleRest = digitalRead(MODULE_PIN);
  Serial.print("Pad baseline: ");
  Serial.println(padBaseline);
}

void loop() {
  int pad = touchRead(TOUCH_PAD);                  // drops when touched
  bool padTouched = pad < padBaseline * 0.7;
  bool moduleTouched = digitalRead(MODULE_PIN) != moduleRest;

  digitalWrite(LED_PIN, (padTouched || moduleTouched) ? HIGH : LOW);

  Serial.print("Pad: ");
  Serial.print(pad);
  Serial.print(padTouched ? " (touched)" : "");
  Serial.print("   Module: ");
  Serial.println(moduleTouched ? "touched" : "-");
  delay(100);
}

Upload the sketch without touching anything, then open the Serial Monitor at 115200 baud and touch the foil and the module's metal pin in turn.

How it works

The ESP32 measures how much electric charge its touch pin can hold. Your finger adds capacitance, so touchRead() returns a lower number when you touch the pad. The sketch measures the normal value at start-up and treats anything 30 % lower as a touch. The touch module works differently: it amplifies the small electrical signals your body picks up and switches a digital output.

Try this next

  • Use several touch pins (GPIO 4, 13, 14, 27…) to make a touch keyboard.
  • Make the foil pad switch the LED on and off with each touch, like the touch lamp lesson.
  • Put the foil under a sheet of paper and check if it still works.

Troubleshooting

  • The pad triggers by itself: keep the touch wire short and restart without touching anything.
  • The module never triggers: adjust its knob until the signal LED reacts to your touch.
  • touchRead isn't recognised: make sure "ESP32 Dev Module" is selected as the board.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

ESP32 IoT Starter Kit | Arduino IDE Compatible | Free Tutorials $57.00

Shop the kit