Example Code for Arduino-Touch Control LED

Last revision 2025/12/04

This guide demonstrates how to control an LED with an Arduino using a touch sensor, featuring a wiring diagram and sample code to enable users to easily create a touch-controlled LED project.

Wiring Diagram

Touch_Sensor_Connect_Diagram

Sample Code

int ledPin = 13;                // Connect LED on pin 13, or use the onboard one
int KEY = 2;                 // Connect Touch sensor on Digital Pin 2

void setup(){
  pinMode(ledPin, OUTPUT);      // Set ledPin to output mode
  pinMode(KEY, INPUT);       //Set touch sensor pin to input mode
}

void loop(){
   if(digitalRead(KEY)==HIGH) {      //Read Touch sensor signal
        digitalWrite(ledPin, HIGH);   // if Touch sensor is HIGH, then turn on
     }
   else{
        digitalWrite(ledPin, LOW);    // if Touch sensor is LOW, then turn off the led
     }
}

Result

After the uploade , if you use finger or metal object touch the metal surface of the transducer , the red LED lights on the UNO will light up.

Was this article helpful?

TOP