Build a Low-Cost Arduino-Powered Smart Light Switch
Ever walked into a dark room and fumbled for the switch, wishing it could sense your presence? With a few dollars and an Arduino, you can turn that wish into a reality. This guide walks you through a simple, budget‑friendly smart switch that you can build in an afternoon.
What You Need
Parts List
- Arduino Nano (or any small Arduino board) – the brain of the project
- Relay module (5 V) – lets the Arduino control the mains voltage safely
- Passive infrared (PIR) sensor – detects motion, the trigger for the light
- Push‑button – for manual override
- Breadboard and jumper wires – for prototyping
- Enclosure – a small project box to keep everything tidy
- Power supply – 5 V USB charger or a wall wart
Tools
- Small screwdriver set
- Wire stripper/cutter
- Soldering iron (optional, but makes the final build sturdier)
- Multimeter (helps catch wiring mistakes)
Having these items on hand means you won’t need to pause the build to run to the store. Most of them are already in my toolbox, so the total cost stays under $20.
Wiring the Circuit
Connect the Relay
The relay is the only part that deals with mains voltage, so treat it with respect. The relay module has three pins on the low‑voltage side: VCC, GND, and IN. Hook VCC to the Arduino’s 5 V pin, GND to GND, and IN to digital pin D7. The high‑voltage side has COM (common), NO (normally open), and NC (normally closed). Connect the light’s hot wire to COM, and the other side of the light to NO. When the Arduino tells the relay to close, power flows to the lamp.
Hook Up the PIR Sensor
The PIR sensor also has three pins: VCC, GND, and OUT. Connect VCC to 5 V, GND to GND, and OUT to digital pin D2. The sensor will output a HIGH signal (about 5 V) when it sees motion, and LOW otherwise.
Add the Manual Button
A simple push‑button gives you control when the sensor isn’t enough. Wire one side of the button to D3 and the other side to GND. Enable the Arduino’s internal pull‑up resistor in code so the button reads HIGH when not pressed and LOW when pressed.
Power the Arduino
Plug the USB charger into the Arduino’s micro‑USB port. If you prefer a wall‑wart, make sure it’s a regulated 5 V supply. Keep the Arduino’s barrel jack free; the relay already handles the high voltage.
Programming the Arduino
Open the Arduino IDE and paste the sketch below. It’s short, but I added comments so you can tweak it later.
// Pin definitions
const int PIR_PIN = 2;
const int RELAY_PIN = 7;
const int BUTTON_PIN = 3;
// Timing variables
unsigned long lastMotion = 0;
const unsigned long timeout = 300000; // 5 minutes
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalWrite(RELAY_PIN, LOW); // start with light off
}
void loop() {
bool motion = digitalRead(PIR_PIN) == HIGH;
bool button = digitalRead(BUTTON_PIN) == LOW; // pressed
if (motion) {
digitalWrite(RELAY_PIN, HIGH); // turn light on
lastMotion = millis(); // reset timer
}
// Manual override: toggle light on each press
static bool lastButtonState = HIGH;
if (button && lastButtonState == HIGH) {
digitalWrite(RELAY_PIN, !digitalRead(RELAY_PIN));
delay(200); // debounce
}
lastButtonState = button;
// Turn off after timeout if no motion
if (millis() - lastMotion > timeout && digitalRead(RELAY_PIN) == HIGH) {
digitalWrite(RELAY_PIN, LOW);
}
}
How It Works
- Motion detection: When the PIR goes HIGH, the Arduino turns the relay on and records the time.
- Auto‑off: If no motion is seen for five minutes, the light switches off.
- Button toggle: Each press flips the relay state, letting you keep the light on even if the room is empty.
Feel free to change the timeout value. I like a shorter interval for a hallway, but a living room feels cozier with a longer delay.
Testing and Tweaking
Before you solder anything permanent, power the circuit from the breadboard and run the code. Use a multimeter to verify that the relay’s COM and NO pins are indeed switching when you move in front of the sensor. If the light flickers, check the wiring for loose connections.
A common hiccup is the PIR’s “warm‑up” period. Most sensors need about 30 seconds after power‑up to stabilize. The sketch ignores motion during that time, so you won’t get a false trigger.
If the button feels “spongy,” add a small 10 kΩ resistor between the button pin and ground. That creates a cleaner pull‑down and reduces bounce.
Final Thoughts
Building a smart light switch with an Arduino is a great way to learn about interfacing low‑voltage electronics with mains power. The project stays cheap, stays safe (thanks to the relay), and gives you a useful upgrade for any room. Plus, you get the satisfaction of walking into a lit space without ever touching a switch—something I still brag about to friends at tech meet‑ups.
Once you’re comfortable with this setup, you can expand it. Add a Bluetooth module and control the light from your phone, or swap the PIR for a light‑dependent resistor (LDR) to make the switch react to ambient brightness. The sky’s the limit, and the Arduino community is full of ready‑made libraries to help you get there.
Happy wiring, and may your evenings be bright without the hassle of hunting for switches.
- → Step-by-Step Guide to Building a Reliable RS-485 Cable for Arduino Projects @serialcablechronicles
- → Designing Smart Commercial Lighting: A Practical Guide to Sustainable, High‑Impact Spaces @luminousspaces
- → Step-by-step guide to building a high‑accuracy thermocouple block for lab and factory @thermotechinsights
- → How to Reflow Solder on Tiny PCBs with a Standard Soldering Iron – A Complete DIY Guide @soldercraft
- → Designing Energy-Efficient Smart Lighting with Optoelectronic Lamps: A Practical Guide for Architects @luminouslabs