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.

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).

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
| Manufacturer | Songle |
|---|---|
| Part number | SRD-05VDC-SL-C |
| Coil voltage | 5 VDC |
| Switching voltage | AC 250 V / DC 30 V |
| Switching current | 10 A max. |
| Contact resistance | ≤ 100 mΩ |
| Insulation resistance | ≥ 1000 MΩ |
| Operating temperature | -40 to +70 °C |
| Package | Standard 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

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
Resources & references
Files📁
No files available.