BeginnerArduino about 45 min

Touch-Controlled LED

Tap a capacitive pad to toggle a green LED, using stable input detection and one action per touch.

Touch-Controlled LED
Illustrative cover. Follow the written wiring steps.

Steps

  1. Check the touch module mode

    Use a TTP223 module in momentary, active-HIGH mode: OUT is LOW when idle and HIGH while touched. Module solder bridges may select toggle or active-LOW modes. Confirm your module configuration before using this sketch; adjust TOUCH_ACTIVE for active-LOW operation.

  2. Wire the touch sensor

    With USB unplugged, connect TTP223 VCC to UNO 5V, GND to GND and OUT to D2. Use the PCB pin labels rather than assuming a universal pin order. Do not connect OUT directly to an LED load.

  3. Wire the indicator

    Connect D8 through a 220 ohm resistor to the green LED anode and its cathode to UNO GND. All parts share ground. Upload the sketch; leave the touch pad untouched during the two-second startup calibration period.

  4. Test touches and releases

    Tap the pad: the LED turns on. Hold a finger on it: the LED keeps its state. Release and tap again: the LED turns off. The sketch requires a stable signal for 30ms and reacts only to a newly detected touch. If behaviour differs, check the module polarity and whether it is in hardware toggle mode.

Code

#include <stdint.h>
const byte TOUCH = 2, LED = 8;
const int TOUCH_ACTIVE = HIGH;
const uint32_t STABLE_MS = 30;
bool ledOn = false;
bool lastRaw = false, stableTouch = false;
uint32_t changedAt = 0;

void setup() {
  pinMode(TOUCH, INPUT);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
  delay(2000); // Leave the pad untouched during calibration.
  lastRaw = digitalRead(TOUCH) == TOUCH_ACTIVE;
  stableTouch = lastRaw; // Ignore a touch already held at startup.
  changedAt = millis();
}

void loop() {
  uint32_t now = millis();
  bool raw = digitalRead(TOUCH) == TOUCH_ACTIVE;
  if (raw != lastRaw) { lastRaw = raw; changedAt = now; }
  if ((uint32_t)(now - changedAt) >= STABLE_MS && raw != stableTouch) {
    stableTouch = raw;
    if (stableTouch) {
      ledOn = !ledOn;
      digitalWrite(LED, ledOn ? HIGH : LOW);
    }
  }
}
Notes. USB-powered LED demonstration. Requires momentary touch output; a module already in toggle mode needs a different sketch. Leave the pad untouched at power-up. Cover is illustrative.