Touch Sensor Toggle Relay – Arduino Tutorial

2,381 views

Introduction

Toggling a 5V SPDT relay with a TTP223 touch sensor to run AC appliances using an Arduino UNO is a process of controlling an AC appliance using an Arduino microcontroller and an SPDT relay. The TTP223 touch sensor is used to trigger the relay to turn on or off the AC appliance.

The TTP223 touch sensor is a touch-sensitive switch that can detect a touch and output a digital signal (HIGH or LOW). It has three pins: VCC, GND, and OUT. The VCC and GND pins are used to power the touch sensor, while the OUT pin outputs a digital signal depending on whether the sensor is touched or not.

Hardware Components

You will require the following hardware for Touch Sensor Toggle Relay with Arduino.

S.noComponentValueQty
1.Arduino UNO1
2.USB Cable Type A to B1
3.Touch Sensor1
4.Relay1
5.Warning Light Bright Waterproof1
6. Power Adapter12V-1
7.DC Power Jack1
8.Power Adapter for Arduino9V1
9.Jumper Wires1

Touch Sensor Toggle Relay with Arduino

  1. Connect the TTP223 touch sensor to the Arduino UNO. The TTP223 has three pins, connect the VCC pin to 5V on the Arduino, the GND pin to GND on the Arduino, and the OUT pin to any digital pin on the Arduino.
  2. Connect the relay to the Arduino UNO. The relay has three pins, connect the VCC pin to 5V on the Arduino, the GND pin to GND on the Arduino, and the IN pin to any digital pin on the Arduino.
  3. In the Arduino IDE, include the default digitalRead() and digitalWrite() functions in your sketch.
  4. In the setup() function, initialize the serial monitor with a baud rate of 9600 and configure the touch sensor pin as an input
void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT); // 2 is the digital pin number connected to the OUT of TTP223
}
  1. In the loop() function, check the state of the touch sensor pin using the digitalRead() function and toggle the relay accordingly and display the status on the serial monitor
void loop() {
  if (digitalRead(2) == HIGH) {
    digitalWrite(4, !digitalRead(4)); // 4 is the pin number connected to the IN of Relay
    if(digitalRead(4) == HIGH) {
      Serial.println("Relay is On");
    } else {
      Serial.println("Relay is Off");
    }
  }
  delay(100);
}
  1. Upload the code to the Arduino UNO and open the serial monitor to see the status of the relay.

Schematic

Make connections according to the circuit diagram given below.

Touch-Sensor-Toggle-Relay-Circuit

Wiring / Connections

ArduinoTouch SensorRelay
5VVCCVCC
GNDGNDGND
D7SIG PIN
D3INP

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

const int TOUCH_SENSOR_PIN = 7; // Arduino pin connected to touch sensor's pin
const int RELAY_PIN        = 3; // Arduino pin connected to relay's pin

// variables will change:
int relayState = LOW;  // the current state of relay
int lastTouchState;    // the previous state of touch sensor
int currentTouchState; // the current state of touch sensor

void setup() {
  Serial.begin(9600);               // initialize serial
  pinMode(TOUCH_SENSOR_PIN, INPUT); // set arduino pin to input mode
  pinMode(RELAY_PIN, OUTPUT);       // set arduino pin to output mode

  currentTouchState = digitalRead(TOUCH_SENSOR_PIN);
}

void loop() {
  lastTouchState    = currentTouchState;             // save the last state
  currentTouchState = digitalRead(TOUCH_SENSOR_PIN); // read new state

  if(lastTouchState == LOW && currentTouchState == HIGH) {
    Serial.println("The sensor is touched");

    // toggle state of relay
    relayState = !relayState;

    // control relay arccoding to the toggled state
    digitalWrite(RELAY_PIN, relayState); 
  }
}

Working Explanation

In the setup() function, the serial monitor is initialized with a baud rate of 9600, and the touch sensor pin is configured as an input using the pinMode() function.

In the loop() function, the state of the touch sensor pin is read using the digitalRead() function. If the touch sensor is pressed, the state of the relay is toggled using the digitalWrite() function. It uses the ! operator to toggle the current state of the pin connected to the relay. The digitalRead() function is used to check the current state of the relay pin. If the relay is on, the code will print “Relay is On” on the serial monitor, otherwise, it will print “Relay is Off”.

Applications

  • Remote Control of AC Appliances
  • Automatic lighting control
  • Power Management
  • Smart home devices
  • Building Automation
  • HVAC control
  • Energy Management systems
  • Industrial process control.

Conclusion.

We hope you have found this Touch Sensor Toggle Relay Circuit very useful. If you feel any difficulty in making it feel free to ask anything in the comment section.