Arduino relay module: switch a mains lamp (wiring, code and live simulator)
A 1-channel relay module lets an Arduino switch a load that it could never power itself, such as a mains lamp. The Arduino drives the module's IN pin from one digital pin, the module's coil does the switching, and the lamp is wired through the relay contacts only. On the common blue SRD-05VDC module the input is active-low: a LOW on IN pulls the relay in, a HIGH lets it go. Test the logic in the simulator first, then wire the real thing with the mains unplugged.
Run it in your browser (free simulator)
Press ▶ Simulate in the box above. Nothing to install and no account needed. Open it in the workbench to change it (free account).
Press Simulate
The Uno starts the sketch: D7 starts HIGH, so the relay is off and the lamp is dark. The LED on the relay module shows its state.
Watch the relay click
Once a second the sketch toggles D7. When D7 goes LOW the module shows ON, COM joins NO, and the lamp lights. A second later it switches off again.
Switch the outlet off
Select the wall outlet and switch its breaker off in the Inspector. The relay still toggles, but the lamp stays dark: the mains side has no power. This is what "the Arduino works but the lamp does not" looks like.
Move the lamp to NC
Open the circuit in the workbench, move the lamp wire from NO to NC and run it again: now the lamp is lit while the relay is off.
What you need
- An Arduino Uno (or any 5 V Arduino) and its USB cable
- A 1-channel 5 V relay module (the common blue board with an SRD-05VDC relay and three control pins: VCC, GND, IN)
- 3 female-to-male jumper wires for the control side
- A mains lamp with a lamp holder, a mains cable with a plug, and insulated wire rated for mains
- A small screwdriver for the screw terminals, and an enclosure that keeps the mains terminals covered
Careful: Mains voltage can kill. Build and test the control side with the mains unplugged, and connect the mains wiring only when you know how to do it safely. If you are not sure, ask a qualified person, or use a low-voltage lamp on a battery for the real board and keep the mains for the simulator.
Wiring: Arduino, relay module and lamp
The control side uses three wires between the Arduino and the module. The mains side never touches the Arduino: the live wire goes into COM, out of NO to the lamp, and the neutral wire goes straight to the lamp.
| From | To | Why |
|---|---|---|
| Arduino 5V | Relay module VCC | Powers the coil and the module electronics |
| Arduino GND | Relay module GND | Common ground for the control signal |
| Arduino D7 | Relay module IN | The signal the sketch switches (LOW = relay on) |
| Wall outlet live (L) | Relay COM | The switched contact comes from the live wire |
| Relay NO | Lamp, first wire | NO (normally open) is closed only while the relay is on |
| Lamp, second wire | Wall outlet neutral (N) | Neutral goes straight to the lamp, not through the relay |
Use NO if the lamp should be off when the relay is off. NC (normally closed) does the opposite: the lamp is on while the relay is off. The module has its own optocoupler and driver transistor, so one digital pin is enough, but keep the mains wires well away from the 5 V wires.
How the code works
The sketch uses one pin, D7. In setup() it writes HIGH first and only then makes the pin an output. With an active-low module this order matters: the other way round, the pin is briefly LOW and the relay clicks once at every reset.
loop() reads the pin back with digitalRead(), inverts it and writes it, then waits a second with delay(). That is the whole toggle, with no extra variable. To switch on a button or a sensor instead, replace the toggle with digitalWrite(RELAY, LOW) to turn the lamp on and digitalWrite(RELAY, HIGH) to turn it off.
If your module turns on with a HIGH signal (some boards have a jumper to choose), swap HIGH and LOW. In the simulator the "Active-low input" setting of the module does the same.
Put it on the real Arduino
Wire the control side first
5V to VCC, GND to GND and D7 to IN, with the Arduino on USB and nothing connected to the mains. Upload the sketch and listen: the relay should click once a second and the module LED should follow it.
Unplug everything, then wire the mains side
With the plug out of the wall, connect live to COM, NO to the lamp and neutral to the lamp, and tighten the screw terminals so no wire strands show. Use mains-rated wire and a lamp holder, never bare lamp pins.
Close it up
Put the module and the terminals in an enclosure so nothing live can be touched. Check that no mains wire touches the Arduino, then plug in.
Test with a small load
Start with a lamp of a few watts. Most blue modules are rated 10 A at 250 V AC, but long runs of wire and heavy loads need proper fuses and thicker wire.
The code (Arduino C++)
This is the Arduino sketch for the real Uno. The simulator runs the same code.
Show the code (13 lines)
// D7 -> relay IN (active-low: pin LOW energises the coil).
const int RELAY = 7;
void setup() {
digitalWrite(RELAY, HIGH); // start with the relay off
pinMode(RELAY, OUTPUT);
}
void loop() {
digitalWrite(RELAY, !digitalRead(RELAY));
delay(1000);
}
Troubleshooting
- The relay clicks but the lamp stays off
- The relay works, so look at the mains side: is the plug in, is the lamp good, is live on COM and the lamp on NO (not NC)? Switch the power off and check each screw terminal by pulling lightly on the wire.
- The relay is on when the sketch says off, and the other way round
- Your module is active-low and the code assumed active-high, or the opposite. Swap HIGH and LOW in the code.
- The relay clicks at every reset or when the Arduino starts
- The pin is floating or LOW for a moment while the board starts. Write HIGH before pinMode(OUTPUT) as in the code, or add a 10 kohm pull-up from IN to 5V.
- The Arduino resets when the relay switches on
- The coil pulls about 70 mA and the 5 V rail dips. Use a better USB cable or a separate 5 V supply for the module and join the grounds.
- The module LED is on but the relay does not click
- The module is not getting enough voltage on VCC. Use 5 V, not 3.3 V, for a 5 V relay module.
Frequently asked questions
How do I control a mains lamp with an Arduino?
Use a relay module between them: the Arduino switches the module's IN pin, and the module's contacts switch the lamp's live wire. The Arduino never touches the mains. Wire the mains side with the power off and keep it covered.
What is the difference between NO, NC and COM?
COM is the common contact. NO (normally open) is joined to COM only while the relay is on. NC (normally closed) is joined to COM while the relay is off. For a lamp that should be off by default use COM and NO.
Why is my relay module active-low?
On the common blue boards an optocoupler and a transistor drive the coil, and the input LED of the optocoupler lights when IN is pulled to ground. So a LOW signal turns the relay on. Some boards have a jumper to change it.
Can I power the relay module from the Arduino 5V pin?
One module, yes: it takes about 70 mA while the coil is pulled in. For several modules use a separate 5 V supply and join its ground to the Arduino ground.
Can I use a 3.3 V board such as an ESP32 or Pico?
The signal usually works, because most modules accept 3.3 V on IN (check the module), but the module still needs 5 V on VCC, from VIN or a separate supply, and the grounds must be joined.
Can I try this without a relay module?
Yes. The simulator on this page runs the same sketch on a virtual Uno with a virtual relay module, wall outlet and lamp, for free in your browser. Nothing in it can hurt you.
Embed this circuit on your own page
Copy this code into your blog, tutorial or course page. Visitors can run the circuit right there.
<iframe src="https://voltbench.click/embed/gallery/arduino-relay-module-mains-lamp" width="100%" height="560" style="border:1px solid #2a3140;border-radius:8px" loading="lazy" allowfullscreen title="Arduino relay module: switch a mains lamp"></iframe>
Build your own version
Open this circuit in the VoltBench workbench, change the wiring or the code, and run it. When it works, flash a real board. Free to use.