Other Arduino Codes and Videos by Robojax

Control 2 AC bulbs with two push subuttons Push ON and Push OFF (toggle) with Arduino

دروس آردوینو به فارسی

Control 2 AC bulbs with two push subuttons Push ON and Push OFF (toggle) with Arduino

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

  1. Control 1 relay with 1 push button and AC bulb
  2. Get Schematic diagram for this sketch from Arduino Course on Udemy
  3. Learn Arduino step by step from beginner to Advance (Coruse)
  4. Get Early Access to my videos via Patreon

 /*
 * 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 Shamshiri for Robojax 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);
}


   

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal