const int TX_PIN = 12; const int RX_PIN = 13; const int SAMPLES = 50; const int PROX_THRESHOLD = 1800; // tune these after watching serial plotter const int TOUCH_THRESHOLD = 3000; long readCapacitance() { long total = 0; for (int i = 0; i < SAMPLES; i++) { // discharge RX fully pinMode(RX_PIN, OUTPUT); digitalWrite(RX_PIN, LOW); digitalWrite(TX_PIN, LOW); delayMicroseconds(10); // float RX, then pulse TX high pinMode(RX_PIN, INPUT); digitalWrite(TX_PIN, HIGH); delayMicroseconds(5); // fixed charge window — tune this (1–20µs) // read the voltage that coupled onto RX total += analogRead(RX_PIN); // reset TX digitalWrite(TX_PIN, LOW); delayMicroseconds(5); } return total / SAMPLES; } void setup() { Serial.begin(115200); pinMode(TX_PIN, OUTPUT); digitalWrite(TX_PIN, LOW); } void loop() { long val = readCapacitance(); Serial.println(val); // open Serial Plotter to watch this live if (val > TOUCH_THRESHOLD) { Serial.println("TOUCH"); } else if (val > PROX_THRESHOLD) { Serial.println("PROXIMITY"); } delay(20); }