---
title: Build a Low-Cost Arduino-Powered Smart Light Switch
siteUrl: https://logzly.com/techwiringinsights
author: techwiringinsights (Tech Wiring Insights)
date: 2026-06-20T22:04:04.482422
tags: [smartlighting, arduino, dyelectronics]
url: https://logzly.com/techwiringinsights/build-a-low-cost-arduino-powered-smart-light-switch
---


**Disclosure: We are reader supported, and earn affiliate commissions when you buy through us.**


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](https://www.amazon.com/s?k=smart+switch&tag=organizationtip101-20) 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](https://www.amazon.com/s?k=power+supply&tag=organizationtip101-20)** – 5 V [USB charger](https://www.amazon.com/s?k=USB+charger&tag=organizationtip101-20) or a wall wart  

### Tools
- Small [screwdriver set](https://www.amazon.com/s?k=Screwdriver+Set&tag=organizationtip101-20)  
- [Wire stripper](https://www.amazon.com/s?k=Wire+stripper&tag=organizationtip101-20)/cutter  
- [Soldering iron](https://www.amazon.com/s?k=soldering+iron&tag=organizationtip101-20) (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](https://www.amazon.com/s?k=hot+wire&tag=organizationtip101-20) to COM, and the other side of the light to NO. When the Arduino tells the relay to close, power flows to the lamp.  
For a deeper dive, see our [step‑by‑step wiring guide for a smart light switch](/techwiringinsights/step-by-step-guide-to-wiring-a-smart-light-switch-for-beginners).

### Hook Up the [PIR Sensor](https://www.amazon.com/s?k=PIR+sensor&tag=organizationtip101-20)
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](https://www.amazon.com/s?k=USB+port&tag=organizationtip101-20). If you prefer a wall‑wart, make sure it’s a [regulated 5 V power supply](/techwiringinsights/how-to-build-a-lowcost-arduino-power-supply-for-diy-electronics-projects). 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.

```cpp
// 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](https://www.amazon.com/s?k=motion+detection&tag=organizationtip101-20):** 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](https://www.amazon.com/s?k=light+switches&tag=organizationtip101-20) 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](https://www.amazon.com/s?k=living+room&tag=organizationtip101-20) 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](https://www.amazon.com/s?k=loose+connections&tag=organizationtip101-20).

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](https://www.amazon.com/s?k=smart+light+switch&tag=organizationtip101-20) 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.
