---
title: Step-by-Step Guide: Build a Raspberry Pi Smart Light Switch for Under $30
siteUrl: https://logzly.com/techandtinker
author: techandtinker (Tech & Tinker)
date: 2026-06-18T15:12:12.726345
tags: [raspberrypi, diy, smarthome]
url: https://logzly.com/techandtinker/step-by-step-guide-build-a-raspberry-pi-smart-light-switch-for-under-30
---


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


Ever walked into a room and fumbled for the [light switch](https://www.amazon.com/s?k=light+switch&tag=organizationtip101-20), only to wish you could just tap your phone?  With a [Raspberry Pi](https://www.amazon.com/s?k=Raspberry+Pi&tag=organizationtip101-20) that serves as a [smart home hub](/techandtinker/how-to-build-a-smart-home-hub-with-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  

| Part | Approx. Cost | Why it matters |
|------|--------------|----------------|
| Raspberry Pi Zero W | $10 | Small, Wi‑Fi ready, cheap |
| Micro‑USB [power supply](https://www.amazon.com/s?k=power+supply&tag=organizationtip101-20) (5 V, 2 A) | $5 | Keeps the Pi happy |
| 2‑channel relay board (5 V) | $4 | Lets the Pi turn mains power on/off |
| Momentary push‑button (6 mm) | $0.50 | Optional manual override |
| Breadboard & jumper wires | $2 | Easy prototyping |
| Small project box (optional) | $3 | Keeps everything tidy |
| **Total** | **≈ $24.50** | Leaves 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](https://www.amazon.com/s?k=terminal+block&tag=organizationtip101-20).  
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](https://www.amazon.com/s?k=SD+card&tag=organizationtip101-20) (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](https://www.amazon.com/s?k=IP+address&tag=organizationtip101-20) (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  

```bash
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:

```python
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:

```bash
sudo nano /etc/rc.local
```

Add the line before `exit 0`:

```bash
/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](https://www.amazon.com/s?k=tidying+up&tag=organizationtip101-20)  

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](https://www.amazon.com/s?k=power+cable&tag=organizationtip101-20).  The box not only looks neat, it also keeps the electronics away from curious pets.

## Going Further  

* **[Voice control](https://www.amazon.com/s?k=voice+control&tag=organizationtip101-20):** Hook the Flask endpoints into Alexa or [Google Assistant](https://www.amazon.com/s?k=Google+Assistant&tag=organizationtip101-20) with a simple IFTTT webhook.  
* **[Multiple lights](https://www.amazon.com/s?k=multiple+lights&tag=organizationtip101-20):** Add more relay channels or use a second Pi Zero as part of a broader Raspberry Pi [home automation hub](https://www.amazon.com/s?k=home+automation+hub&tag=organizationtip101-20).  
* **[Energy monitoring](https://www.amazon.com/s?k=energy+monitoring&tag=organizationtip101-20):** Pair a cheap current sensor with the Pi and log usage in [Home Assistant](https://www.amazon.com/s?k=Home+Assistant&tag=organizationtip101-20).  
* **Tablet integration:** Extend the switch to a tablet‑based dashboard – see how to turn an old tablet into a [smart home control panel](/techandtinker/turn-an-old-tablet-into-a-smart-home-control-panel).

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