Step-by-Step Guide: Build a Raspberry Pi Smart Light Switch for Under $30

Ever walked into a room and fumbled for the light switch, only to wish you could just tap your phone? With a Raspberry Pi and a few cheap parts you can turn that wish into a reality – and you’ll spend less than a dinner out. I built one last weekend while waiting for my coffee to brew, and the whole thing clicked together in a single afternoon. Here’s how you can do the same.

What You’ll Need

The hardware

PartApprox. CostWhy it matters
Raspberry Pi Zero W$10Small, Wi‑Fi ready, cheap
Micro‑USB power supply (5 V, 2 A)$5Keeps the Pi happy
2‑channel relay board (5 V)$4Lets the Pi turn mains power on/off
Momentary push‑button (6 mm)$0.50Optional manual override
Breadboard & jumper wires$2Easy prototyping
Small project box (optional)$3Keeps everything tidy
Total≈ $24.50Leaves room for a spare LED or a case

All of these can be found on Amazon, eBay, or a local electronics shop. If you already have a Pi Zero lying around, you’re already under budget.

The software

  • Raspberry Pi OS Lite (headless, no desktop) – free download from the Raspberry Pi website.
  • Python 3 (comes pre‑installed).
  • gpiozero library – a simple way to control pins.
  • flask – a tiny web server so you can toggle the light from any phone or laptop.

Wiring the Relay

  1. Power the Pi – Plug the micro‑USB supply into the Pi. The Pi Zero draws less than 200 mA, so 2 A is more than enough.
  2. Connect the relay board – The relay has three pins on each channel: VCC, GND, and IN.
    • Tie VCC to the Pi’s 5 V pin (pin 2).
    • Tie GND to any ground pin (pin 6).
    • Connect IN1 to GPIO 17 (pin 11) and IN2 to GPIO 27 (pin 13).
  3. Wire the light – Cut the live wire of the lamp you want to control, strip the ends, and attach them to the “COM” and “NO” terminals of one relay channel. “COM” is common, “NO” means normally open – the circuit closes when the relay is energized. Safety tip: always unplug the lamp before touching wires, and use a proper plug or terminal block.
  4. Optional button – If you like a physical toggle, connect one side of the button to 3.3 V (pin 1) and the other side to a free GPIO pin (say GPIO 22, pin 15). Add a 10 kΩ pull‑down resistor from the GPIO pin to ground (or enable the internal pull‑down in software).

Setting Up the Pi

Flash the OS

  1. Download the Raspberry Pi OS Lite image.
  2. Use a tool like Balena Etcher to write it to a micro‑SD card (8 GB is fine).
  3. After flashing, open the boot partition and create an empty file named ssh. This enables SSH so you can work headless.
  4. Add a file called wpa_supplicant.conf with your Wi‑Fi credentials (use plain text, no fancy quotes).

First boot

Insert the card, power the Pi, and find its IP address (check your router or use arp -a). SSH in with pi@<ip> and the default password raspberry. Change the password right away with passwd.

Install required packages

sudo apt update
sudo apt install -y python3-pip
pip3 install gpiozero flask

Writing the Control Script

Create a file called switch.py in your home folder:

from gpiozero import OutputDevice, Button
from flask import Flask, jsonify

app = Flask(__name__)

# Relay on GPIO17 controls the lamp
lamp = OutputDevice(17, active_high=False, initial_value=False)

# Optional manual button on GPIO22
button = Button(22, pull_up=False)

# Flip lamp when button pressed
def toggle():
    lamp.toggle()
button.when_pressed = toggle

@app.route('/on')
def turn_on():
    lamp.on()
    return jsonify(state='on')

@app.route('/off')
def turn_off():
    lamp.off()
    return jsonify(state='off')

@app.route('/status')
def status():
    return jsonify(state='on' if lamp.is_active else 'off')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

A few notes:

  • active_high=False tells the relay that a low signal turns it on – most cheap relay boards work that way.
  • The Flask routes give you three URLs you can call from any browser or a simple shortcut on your phone.

Run the script with python3 switch.py. You’ll see the Flask server start on port 5000.

Making It Run on Boot

You don’t want to type the command every time. Edit the rc.local file:

sudo nano /etc/rc.local

Add the line before exit 0:

/usr/bin/python3 /home/pi/switch.py &

Save and exit. The & runs it in the background.

Testing It Out

  1. Open a browser on your phone and go to http://<pi-ip>:5000/status. You should see {"state":"off"}.
  2. Tap http://<pi-ip>:5000/on. The lamp should click on.
  3. Tap .../off – the lamp goes off.
  4. Press the physical button – the lamp toggles.

If anything doesn’t work, double‑check your wiring and make sure the GPIO pins match the script. The gpiozero library will give a clear error if a pin is already in use.

Tidying Up

Once you’re happy, slide the Pi, relay board, and button into the project box. Drill a small hole for the button and a larger one for the power cable. The box not only looks neat, it also keeps the electronics away from curious pets.

Going Further

  • Voice control: Hook the Flask endpoints into Alexa or Google Assistant with a simple IFTTT webhook.
  • Multiple lights: Add more relay channels or use a second Pi Zero to control several circuits.
  • Energy monitoring: Pair a cheap current sensor with the Pi and log usage in Home Assistant.

Building this smart switch taught me that a $10 computer can do more than you’d expect. The real magic isn’t the hardware; it’s the feeling of flipping a light on with a tap on your phone while the coffee brews. Give it a try, and you’ll find yourself adding more “smart” tweaks to the house before the weekend is over.

Reactions
Do you have any feedback or ideas on how we can improve this page?