// Mutual-style capacitive sensor via charge transfer // TX pin drives, RX pin senses — works through plastic/dielectric // Pad 1 (TX) → GPIO 32 // Pad 2 (RX) → GPIO 33, also connected via 1MΩ resistor to GPIO 32 const int TX_PIN = 12; const int RX_PIN = 13; const int SAMPLES = 50; // more = slower but more sensitive const int PROX_THRESHOLD = 200; const int TOUCH_THRESHOLD = 800; long readCapacitance() { long total = 0; for (int i = 0; i < SAMPLES; i++) { // discharge RX pinMode(RX_PIN, OUTPUT); digitalWrite(RX_PIN, LOW); delayMicroseconds(10); // set TX low, float RX digitalWrite(TX_PIN, LOW); pinMode(RX_PIN, INPUT); // drive TX high — charge transfers through pad coupling & resistor digitalWrite(TX_PIN, HIGH); // count time until RX goes high long count = 0; while (digitalRead(RX_PIN) == LOW && count < 20000) { count++; } total += count; // reset digitalWrite(TX_PIN, LOW); } return total / SAMPLES; } void setup() { Serial.begin(115200); pinMode(TX_PIN, OUTPUT); } void loop() { long val = readCapacitance(); Serial.println(val); if (val > TOUCH_THRESHOLD) { Serial.println("TOUCH"); } else if (val > PROX_THRESHOLD) { Serial.println("PROXIMITY"); } delay(50); }