Other Arduino Codes and Videos by Robojax

Using Arduino push button to Push ON and Push OFF relay and AC bulb Robojax.com

Using Arduino push button to Push ON and Push OFF relay and AC bulb

This is the Arduino code for push button to push ON and Push OFF a relay and AC bulb http://robojax.com/learn/arduino/ You ask questions via the comment in the video page here
  1. download TTP223B Capacitive Touch Manual

 /*
 * This is the Arduino code for push button to push ON and Push OFF a relay and AC bulb
 * the output pen 10 is connected to relay 
 * watch video instruction on video https://youtu.be/7CCSRs5bvH0
 * Be carefull working with AC is dangrous. Disconnect from AC power when working and wear protective gloves when touch the AC components.

 * 
 * Written by Ahmad Nejrabi for Roboja Video, www.Robojax.com
 * Date: Dec 14, 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.
 * http://robojax.com/learn/arduino/
 * 
 */


int pbuttonPin = 2;// connect output to push button
int relayPin = 10;// Connected to relay (LED)

int val = 0; // push value from pin 2
int lightON = 0;//light status
int pushed = 0;//push status


void setup() {
	// Robojax.com code and video tutorial for push button ON and OFF
  Serial.begin(9600);
  pinMode(pbuttonPin, INPUT_PULLUP); 
  pinMode(relayPin, OUTPUT);

}

void loop() {
// Robojax.com code and video tutorial for push button ON and OFF
  val = digitalRead(pbuttonPin);// read the push button value

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

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

  lightON = val;

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

// Robojax.com code and video tutorial for push button ON and OFF

  delay(100);
}


   

Code for 2 push button and 2 relay control

I have added 2nd button and 2nd relay without testing it. So if you face a problem, please use the code above for 1 relay and make sure it works before start using this code. You ask questions via the comment in the video page here


 /*
 * This is the Arduino code for 2 push button to push ON and Push OFF 2 relays and 2 AC bulb
 * the output pen 10 is connected to relay 
 * watch video instruction on video https://youtu.be/7CCSRs5bvH0
 * Be carefull working with AC is dangrous. Disconnect from AC power when working and wear protective gloves when touch the AC components.

 * 
 * Written by Ahmad Nejrabi for Roboja Video, www.Robojax.com
 * Date: Dec 14, 2017, in Ajax, Ontario, Canada
 * 2 button added Feb 09, 2018 for 1 button at 08:51
 * 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.
 * http://robojax.com/learn/arduino/
 * 
 */


int pbuttonPin = 2;// connect output to push button
int relayPin = 10;// Connected to relay (LED)

int val = 0; // push value from pin 2
int lightON = 0;//light status
int pushed = 0;//push status


int pbuttonPin2 = 3;// connect output to push button 2
int relayPin2 = 11;// Connected to relay 2 (LED)

int val2 = 0; // push value from pin 3
int lightON2 = 0;//light 2 status
int pushed2 = 0;//push 2 status

void setup() {
	// Robojax.com code and video tutorial for push button ON and OFF
  Serial.begin(9600);
  pinMode(pbuttonPin, INPUT_PULLUP); 
  pinMode(relayPin, OUTPUT);
   pinMode(pbuttonPin2, INPUT_PULLUP); 
  pinMode(relayPin2, OUTPUT);

}

void loop() {
 // Robojax.com code and video tutorial for push button ON and OFF (2 relay)
  val = digitalRead(pbuttonPin);// read the push button value
  val2 = digitalRead(pbuttonPin2);// read the push button 2 value

  
//	Start of Relay 1 ********** /  
  if(val == HIGH && lightON == LOW){

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

   // Robojax.com code and video tutorial for push button ON and OFF (2 relay)
  lightON = val;

      if(pushed == HIGH){
        Serial.println("Light ON");
        digitalWrite(relayPin, LOW); 
       
      }else{
        Serial.println("Light OFF");
        digitalWrite(relayPin, HIGH);
   
      }   
	
//	End of Relay 1 ********** /  

//	Start of Relay 2 ********** /  

 
  if(val2 == HIGH && lightON2 == LOW){

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

  lightON2 = val2;

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

//	End of Relay 2 ********** /  
  delay(100);
}