Other Arduino Codes and Videos by Robojax

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

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

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

  1. download TTP223B Capacitive Touch Manual
  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 push button to push ON and Push OFF a relay and AC bulb
it toggles the bulb to ON and OFF
 * 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.
 
 * Full explanation of this code and wiring diagram is available at
 * my Arduino Course at Udemy.com here: http://robojax.com/L/?id=62

 * Written by Ahmad Shamshiri on Dec 14, 2017 at in Ajax, Ontario, Canada
 * in Ajax, Ontario, Canada. www.robojax.com
 * 

 * Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon http://robojax.com/L/?id=63

or make donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.


*/


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);
 digitalWrite(relayPin, HIGH);// keep the load OFF at the begining. If you wanted to be ON, change the HIGH to LOW
}

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);
}


   

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