Search Code

Using an Arduino push button to switch a relay and AC bulb

Using an Arduino push button to switch a relay and AC bulb

In this tutorial, we will learn how to control an AC bulb or LED light using a push button and a relay. The push button will toggle the light on and off, maintaining its state even when the button is released. This setup is not only practical but also a great introduction to working with relays and AC loads.

5V LOW-LEVEL trigger relay

As we progress, we'll cover the necessary components, wiring instructions, and the Arduino code that makes this project possible. Make sure to watch the associated video for a visual guide and detailed explanations (in video at 00:00).

5V LOW-Level triggered Relay module

Hardware Explained

For this project, we will use a relay module, an Arduino board, and a push button. The relay module acts as a switch that can control high voltage AC loads, such as a bulb, while being controlled by the low voltage Arduino signals. The relay module has three main terminals for connecting the load: Common (COM), Normally Open (NO), and Normally Closed (NC).

The push button serves as a user interface to toggle the state of the relay. It is connected to one of the digital pins on the Arduino. When the button is pressed, it sends a signal to the Arduino, which then activates or deactivates the relay accordingly. This simple interaction allows us to control the light easily.

Datasheet Details for Relay

ManufacturerSongle
Part numberSRD-05VDC-SL-C
Coil voltage5 VDC
Switching voltageAC 250 V / DC 30 V
Switching current10 A max.
Contact resistance≤ 100 mΩ
Insulation resistance≥ 1000 MΩ
Operating temperature-40 to +70 °C
PackageStandard Relay Module

  • Ensure the relay is rated for the load you are switching.
  • Keep the relay module powered with 5 VDC.
  • Use appropriate insulation for AC connections.
  • Be cautious while working with AC power; ensure the circuit is disconnected before making changes.
  • Connect the push button to the designated input pin on the Arduino.
  • Utilize the INPUT_PULLUP mode for the push button pin to avoid the need for external resistors.
  • Test the relay operation with a lower voltage before connecting AC loads.
  • Verify connections to avoid short circuits.

Wiring Instructions

Arduino wiring for AC bulb, LED and push button
Arduino wiring for AC bulb, LED and push button

To wire the relay module, start by connecting the VCC pin of the relay to the 5V pin on the Arduino, and the GND pin of the relay to the GND pin on the Arduino. The IN pin on the relay should be connected to digital pin 10 on the Arduino. This pin will send the signal to activate the relay.

For the push button, connect one terminal to digital pin 2 on the Arduino. The other terminal should be connected to GND. This setup uses the internal pull-up resistor of the Arduino, so ensure that the pin is set to INPUT_PULLUP in the code. When the button is pressed, it will bring the pin to LOW, sending a signal to the Arduino.

For the AC load, connect one wire to the COM terminal on the relay and the other wire to the NO terminal. This configuration will allow the AC load to turn on when the relay is activated. Always ensure that the AC connections are secure and insulated.

Code Examples & Walkthrough

The following code initializes the necessary pins and sets the relay to remain off initially. The button press toggles the state of the relay and updates the light status accordingly.

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

void setup() {
  Serial.begin(9600);
  pinMode(pbuttonPin, INPUT_PULLUP); 
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH); // keep the load OFF at the beginning
}

In the code, the variable pbuttonPin is set to pin 2, which is used for the push button. The relayPin is set to pin 10, controlling the relay. The digitalWrite function is used to ensure that the relay starts in the OFF state.

void loop() {
  val = digitalRead(pbuttonPin); // read the push button value
  if(val == HIGH && lightON == LOW) {
    pushed = 1 - pushed; // toggle the push status
    delay(100);
  }
  if(pushed == HIGH) {
    digitalWrite(relayPin, LOW); // turn the relay ON
  } else {
    digitalWrite(relayPin, HIGH); // turn the relay OFF
  }
}

This section of code continuously checks the state of the push button. When the button is pressed, it toggles the pushed variable. Depending on the value of pushed, the relay is turned ON or OFF, effectively controlling the light. This loop runs continuously, ensuring the light status is updated in real-time.

For full code details, please refer to the code loaded below the article.

Demonstration / What to Expect

When you press the push button, the relay will activate, turning on the connected AC bulb. Pressing the button again will turn the bulb off. The serial monitor will display the status messages, indicating whether the light is ON or OFF. If you encounter issues, ensure that the wiring is correct, and check for any floating inputs that could cause unexpected behavior (in video at 10:00).

Video Timestamps

  • 00:00 - Introduction
  • 01:30 - Hardware Overview
  • 03:15 - Wiring Instructions
  • 05:45 - Code Walkthrough
  • 08:00 - Demonstration

Images

5V LOW-Level triggered Relay module
5V LOW-Level triggered Relay module
5V LOW-LEVEL trigger relay
5V LOW-LEVEL trigger relay
Arduino wiring for AC bulb, LED and push button
Arduino wiring for AC bulb, LED and push button
5V LOW-LEVEL trigger relay
5V LOW-LEVEL trigger relay
34-Using an Arduino push button to push-on and push-off a relay and AC bulb (toggle)
Language: C++
/*
 * This is the Arduino code for a push button to turn ON and OFF a relay and AC bulb.
It toggles the bulb ON and OFF.
 * The output pin 10 is connected to the relay.
 * Watch video instructions on YouTube: https://youtu.be/7CCSRs5bvH0
 * Be careful working with AC; it is dangerous. Disconnect from AC power when working and wear protective gloves when touching AC components.
 
 * Full explanation of this code and wiring diagram is available in
 * my Arduino Course at Udemy.com here: http://robojax.com/L/?id=62

 * Written by Ahmad Shamshiri on December 14, 2017 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 a structured course with all materials, wiring diagrams, and libraries
all in one 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 a 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 downloaded 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);
}

Resources & references

Files📁

No files available.