Other Arduino Codes and Videos by Robojax
/*
* 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);
}
/*
* This is the Arduino code for 2 TTP223 Capacitive Touch module switch with relay to turn 2 AC or DC loads
* with first touch the Light will go ON and then with next touch the light goes OFF
// December 13, 2017, Updated for 2 channel on Mar 15, 2018 by the request Marcio A. on YouTube Channel
// Written for Robojax.com video
// Using TTP223 Touch module to turn AC (or DC) load ON or OFF.
// When the touch pad 1 is touched the light 1 turn the relay will ON and the COM and PO pin will be connected
// When the touch pad 2 is touched the light 3 turn the relay will ON and the COM and PO pin will be connected
* watch video for details https://youtu.be/Nqh1vYQEEjs
*
*
* Written by Ahmad S. for Roboja Video
* 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.
*
*/
// channel 1 settings
int touchPin = 2;// connect output from TTP223 (module 1) 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
// channel 2 settings
int touchPin2 = 3;// connect output from TTP223 (module 1) to this
int relayPin2 = 11;// Connected to relay
int val2 = 0; // touch value from pin 2
int lightON2 = 0;//light status
int touched2 = 0;//touch status
void setup() {
Serial.begin(9600);
pinMode(touchPin, INPUT); //input from module 1
pinMode(relayPin, OUTPUT);// output to relay 1
pinMode(touchPin2, INPUT); //input from module 2
pinMode(relayPin2, OUTPUT);// output to relay 2
}
void loop() {
/* **************** start of code for channel 1 ********* */
val = digitalRead(touchPin);// read channel 1 touch
if(val == HIGH && lightON == LOW){
touched = 1-touched;
delay(100);
}
lightON = val;
if(touched == HIGH){
Serial.println("Light ON");
digitalWrite(relayPin, LOW); // set relay 1 to turn ON
}else{
Serial.println("Light OFF");
digitalWrite(relayPin, HIGH);// set relay 2 to turn OFF
}
/* **************** END of code for channel 1 ********* */
// the two channels are independent. You can remove one and it will not affect the other from this inside loop()
/* **************** start of code for channel 2 ********* */
val2 = digitalRead(touchPin2);// read channel 2 touch
if(val2 == HIGH && lightON2 == LOW){
touched2 = 1-touched2;
delay(100);
}
lightON2 = val2;
if(touched == HIGH){
Serial.println("Light 2 ON");
digitalWrite(relayPin2, LOW); // set relay 2 to turn ON
}else{
Serial.println("Light 2 OFF");
digitalWrite(relayPin2, HIGH);// set relay 2 to turn OFF
}
/* **************** END of code for channel 2 ********* */
delay(100);
}
If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal