Touch Sensor- LED Interface

About The Project

In this project, we will study how to turn on an LED when the touch sensor is touched. When the sensor is not touched, the LED will turn off.

Touch Sensor

The TTP223-1 is a capacitive touch sensor module used to detect touch input. When touched, its output pin goes HIGH; otherwise, the output remains LOW.

Circuit Wiring

Program Code

C++
// www.matthewtechub.com
//touch sensor to trigger led

const int touchPin = 2;  
const int ledPin=3;
// Pin connected to the touch sensor module
void setup() {
  Serial.begin(9600);   
   // Initialize serial communication
  pinMode(touchPin, INPUT); 
  pinMode(ledPin, OUTPUT);
  // Set the touch pin as input
}
void loop() {
  // Read the state of the touch sensor
  int touchState = digitalRead(touchPin);
if(touchState == 1)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
  // Print (1 for touched, 0 for not touched) 
  Serial.println(touchState);

  delay(1000);  // Delay for stability
}

This code monitors a touch sensor connected to pin 2. When the sensor is touched, the LED connected to pin 3 turns on. When the sensor is not touched, the LED turns off. The touch state (1 for touched, 0 for not touched) is printed to the Serial Monitor every second.

Code Explanation

C++
const int touchPin = 2;  
const int ledPin = 3;

Variable Declarations and Pin Definitions

  • touchPin is the pin number connected to the touch sensor’s output.
  • ledPin is the pin number connected to the LED.
C++
void setup() {
  Serial.begin(9600);   
  // Initialize serial communication at a baud rate of 9600.
  
  pinMode(touchPin, INPUT); 
  // Set the touch pin as input to read from the touch sensor.
  
  pinMode(ledPin, OUTPUT);
  // Set the LED pin as output to control the LED.
}

Setup Function

  • Serial.begin(9600); starts serial communication at 9600 baud rate for debugging or monitoring the sensor state.
  • pinMode(touchPin, INPUT); sets the touch sensor pin as an input, meaning it will read values from this pin.
  • pinMode(ledPin, OUTPUT); sets the LED pin as an output, meaning it will send signals to this pin to control the LED.
C++
void loop() {
  int touchState = digitalRead(touchPin);
  /* Read the state of the touch sensor
 (HIGH or LOW) and store it in touchState.*/
  
  if (touchState == HIGH)
    digitalWrite(ledPin, HIGH);
  /* If the touch sensor is touched (touchState is 
HIGH), turn on the LED.*/
  
  else
    digitalWrite(ledPin, LOW);
  /* If the touch sensor is not touched
 (touchState is LOW), turn off the LED.*/
  
  Serial.println(touchState);
  /* Print the state of the touch sensor to the 
Serial Monitor (1 for touched, 0 for not touched).*/
  
  delay(1000);
  // Delay for 1 second to stabilize.
}

Loop Function

  • int touchState = digitalRead(touchPin); reads the current state of the touch sensor. digitalRead(touchPin) returns HIGH (1) if the sensor is touched and LOW (0) if it is not.
  • The if statement checks the value of touchState:
  • If touchState is HIGH (sensor is touched), digitalWrite(ledPin, HIGH); turns on the LED.
  • If touchState is LOW (sensor is not touched), digitalWrite(ledPin, LOW); turns off the LED.
  • Serial.println(touchState); sends the state of the touch sensor to the Serial Monitor for debugging purposes.
  • delay(1000); introduces a 1-second delay between each reading to stabilize the sensor reading and prevent rapid toggling of the LED.

Try Yourself

Modify the program code to toggle LED when the sensor is touched.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!