code for Turn ON and OFF AC bulb with TTP223 Capacitive Touch Arduino with Relay Robojax.com

Turn ON and OFF AC bulb with TTP223 Capacitive Touch Arduino with Relay Code

This video shows you how to use TTP223 Capacitive Touch with relay and AC bulb and the code.

 /*
 * This is the Arduino code for TTP223 Capacitive Touch switch with relay to turn AC or DC load
 * with first touch the Light will go ON and then with next touch the light goes OFF
 // December 13, 2017
 // Written for Robojax.com video 
 // Using TTP223 Touch module to turn AC (or DC) load ON or OFF.
 // When the touch pad is touched the ligh on the relay will on and the COM and PO pin will be connected
 * watch video for details https://youtu.be/Nqh1vYQEEjs
 * 

 * 
 * Written by Ahmad Nejrabi for Roboja Video
 * Date: Dec 04, 2017, in Ajax, Ontario, Canada
 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * 
 */

int touchPin = 2;// connect output from TTP223 to this
int relayPin = 10;// Connected to relay

int val = 0; // touch value from pin 2
int lightON = 0;//light status
int touched = 0;//touch status


void setup() {
  Serial.begin(9600);
  pinMode(touchPin, INPUT); 
  pinMode(relayPin, OUTPUT);

}

void loop() {

  val = digitalRead(touchPin);

  if(val == HIGH && lightON == LOW){

    touched = 1-touched;
    delay(100);
  }    

  lightON = val;

      if(touched == HIGH){
        Serial.println("Light ON");
        digitalWrite(relayPin, LOW); 
       
      }else{
        Serial.println("Light OFF");
        digitalWrite(relayPin, HIGH);
   
      }     



  delay(100);
}