touch.ino

ESP32-S2 · File : touch.ino · Language : Arduino · Use case : Utiliser une broche comme touche tactile capacitive

The S2 carries 14 hardware capacitive touch pads: touchRead() returns a value that drops when a finger nears the wire. Perfect for buttonless interfaces, even through thin fabric.

Hardware required

An ESP32-S2 board (single-core Xtensa, Wi-Fi, native USB-OTG on GPIO19/20, 14 capacitive touch pads) such as a DevKitC-1 or QT Py, powered over USB. A flexible wire (10-20 cm) or a copper pad on the touch pin.

Wiring

Bare wire on GPIO2. All 14 pads (GPIO1-14) work; avoid flash pins. A short wire already makes a fine antenna.

How it works

The touch controller charges/discharges the pin and compares cycles; the finger adds capacitance and lowers the reading. Calibrate the threshold at rest, then filter with 50 ms time hysteresis. In the Arduino IDE (esp32 package), select « ESP32S2 Dev Module »; the S2 flashes straight over its native USB (double reset on some boards to enter the bootloader).

Going further

Three touches = virtual dimmer; or a 3×3 matrix under cardboard for a keypad.

touch.ino

// Tactile capacitif natif du S2 : touchRead() sur GPIO1..GPIO4.
// Approcher le doigt d'un fil nu ou d'une plaque de cuivre sur la broche.
const int TOUCH_PIN = 2;
const int SEUIL = 40;        // a calibrer : voir sortie de setup()

unsigned long precedent = 0;
bool touche = false;

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println("Calibration : ne touchez a rien pendant 3 s");
  delay(3000);
  Serial.printf("Repos ~ %d, touche ~ %d\n", touchRead(TOUCH_PIN), 0);
}

void loop() {
  if (millis() - precedent < 50) return;
  precedent = millis();
  int v = touchRead(TOUCH_PIN);
  bool maintenant = v < SEUIL;      // toucher fait CHUTER la valeur
  if (maintenant != touche) {
    touche = maintenant;
    digitalWrite(LED_BUILTIN, touche ? HIGH : LOW);
    Serial.printf("%s (touch=%d)\n", touche ? "TOUCHE" : "libre", v);
  }
}

← Back to the board