ESP32 Touch Pins: Capacitive Touch Sensor Guide
The ESP32 features a built-in capacitive touch sensor system with 10 touch channels (T0–T9) that can detect changes in capacitance when a finger touches or approaches a touch pad. This allows you to replace physical buttons with touch-sensitive surfaces at zero extra component cost.
ESP32 Touch Pin Mapping
Each touch channel is mapped to a specific GPIO pin. Not all GPIOs support touch sensing:
| Touch Channel | GPIO Pin | Status |
|---|---|---|
| T0 | GPIO4 | Available |
| T1 | GPIO0 | Strapping (Boot must be HIGH) |
| T2 | GPIO2 | Strapping (Boot must be LOW) |
| T3 | GPIO15 | Strapping (Boot must be HIGH) |
| T4 | GPIO13 | Available |
| T5 | GPIO12 | Strapping (JTAG + flash voltage) |
| T6 | GPIO14 | Available |
| T7 | GPIO27 | Available |
| T8 | GPIO33 | Available |
| T9 | GPIO32 | Available |
How Touch Sensing Works
The ESP32 touch sensor measures the charge/discharge cycle time of an RC circuit formed by the pin's parasitic capacitance. When a finger touches the pad, the capacitance increases, and the measured value changes:
- Baseline (no touch): Typically 40–60 (low cycle count = low capacitance)
- Touch detected: Value increases to 70–100+ (varies by pad size, wire length, and ambient conditions)
- Threshold: Set midway between baseline and touch values (e.g., baseline + 20)
Basic Touch Read Example
// ESP32 Touch Read Example
// Connect a wire or copper pad to GPIO4 (T0)
#define TOUCH_PIN T0 // GPIO4
int threshold = 30; // Adjust based on your setup
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("ESP32 Touch Test - Touch GPIO4");
}
void loop() {
int touchValue = touchRead(TOUCH_PIN);
Serial.print("Touch value: ");
Serial.println(touchValue);
if (touchValue < threshold) {
Serial.println("-> TOUCH DETECTED");
}
delay(200);
}
Calibrating Touch Thresholds
Touch readings vary significantly based on your physical setup. Follow this calibration routine:
- Measure baseline: Run the example above and note the value when not touching (e.g., 45)
- Measure touch: Touch the pad and note the peak value (e.g., 80)
- Set threshold: Use the midpoint:
threshold = (baseline + touch) / 2 - Add hysteresis: Use separate on/off thresholds 5–10 apart to prevent flickering
Touch Interrupts (No Polling Required)
Instead of polling touchRead() in a loop, configure a touch interrupt that fires when the threshold is crossed:
// ESP32 Touch Interrupt - Fires when touch is detected
#define TOUCH_PIN T0
int threshold = 30;
void setup() {
Serial.begin(115200);
touchAttachInterrupt(TOUCH_PIN, onTouch, threshold);
Serial.println("Touch interrupt configured. Touch GPIO4.");
}
void loop() {
// CPU is free - interrupt handles touch detection
delay(1000);
}
void onTouch() {
Serial.println("TOUCH DETECTED via interrupt!");
}
Wake from Deep Sleep on Touch
One of the most powerful features: wake the ESP32 from Deep Sleep when a touch pad is touched:
// ESP32 Deep Sleep Wake via Touch Sensor
#define TOUCH_PIN T0 // GPIO4
int threshold = 30;
void setup() {
Serial.begin(115200);
delay(100);
// Configure touch as wake-up source
touchSleepWakeUpEnable(TOUCH_PIN, threshold);
esp_sleep_enable_touch_wakeup();
Serial.println("Entering Deep Sleep. Touch GPIO4 to wake.");
esp_deep_sleep_start();
}
void loop() {
// Never reached
}
Touch Sensor Limitations
| Issue | Mitigation |
|---|---|
| Noise from power supply | Use a clean 3.3V regulator; avoid shared supply lines |
| Long wires (>15 cm) | Keep sensor wire short; use shielded cable if necessary |
| Water/humidity | Apply conformal coating; recalibrate baseline periodically |
| Plastic enclosure | Thin plastic (under 3 mm) works; thicker reduces sensitivity |
| Temperature drift | Re-calibrate baseline periodically (e.g., every 60 seconds) |
| Multiple adjacent pads | Space pads >10 mm apart; use guard traces |
Touch Sensor Applications
- Touch buttons — Replace mechanical buttons in waterproof designs
- Proximity detection — Detect a hand approaching (before contact)
- Touch slider / wheel — Use multiple adjacent pads with interpolation
- Wake-on-touch — Battery-powered devices that activate on touch
- Smart lamp controls — Dimmer, color change via touch
- Security touchpads — Simple lock/unlock with a sequence
🔧 Check Touch Pin Compatibility
Our interactive pinout tool shows which GPIOs support touch and flags strapping pin conflicts.
Open Interactive Pinout