const int TX_PIN = 12; const int RX_PIN = 13; const int SAMPLES = 100; long baseline = 0; long readCapacitance() { long total = 0; for (int i = 0; i < SAMPLES; i++) { pinMode(RX_PIN, OUTPUT); digitalWrite(RX_PIN, LOW); digitalWrite(TX_PIN, LOW); delayMicroseconds(20); pinMode(RX_PIN, INPUT); digitalWrite(TX_PIN, HIGH); delayMicroseconds(50); // longer charge window total += analogRead(RX_PIN); digitalWrite(TX_PIN, LOW); delayMicroseconds(20); } return total / SAMPLES; } void setup() { Serial.begin(115200); pinMode(TX_PIN, OUTPUT); digitalWrite(TX_PIN, LOW); // calibrate baseline over 200 readings at startup // don't touch the pads during this long sum = 0; for (int i = 0; i < 200; i++) { sum += readCapacitance(); delay(10); } baseline = sum / 200; Serial.print("Baseline: "); Serial.println(baseline); } void loop() { long val = readCapacitance(); long delta = baseline - val; // positive delta = more capacitance = hand present Serial.println(delta); // plot this — should be ~0 at rest, positive on approach if (delta > 300) { Serial.println("TOUCH"); } else if (delta > 80) { Serial.println("PROXIMITY"); } delay(20); }