How to Build a Voice‑Controlled Smart Light Switch with Raspberry Pi for Under $30
Ever walked into a dark room and fumbled for the switch while your phone is glued to your hand? That moment is the perfect excuse to give your lights a brain – and you don’t need a fancy hub or a pricey subscription. With a Raspberry Pi, a few cheap parts, and a dash of code, you can talk to your lights for less than a coffee run. Let’s get into it.
What You’ll Need (and Why It’s Cheap)
| Part | Approx. Cost | Why It Matters |
|---|---|---|
| Raspberry Pi Zero W | $10 | Small, Wi‑Fi ready, and cheap enough for a hobby project. |
| Micro‑USB power supply (5 V, 2 A) | $5 | Keeps the Pi happy and stable. |
| 2‑channel relay board | $6 | Acts as a safe switch for the mains voltage. |
| Mini‑breadboard + jumper wires | $3 | Makes wiring tidy without solder. |
| Micro‑SD card (8 GB) | $4 | Holds the OS and your code. |
| Optional: USB microphone | $2 | Improves voice pickup if you don’t want to use the Pi’s built‑in audio. |
Total: under $30 – and you end up with a reusable kit for future projects.
Step 1 – Get the Pi Ready
Install Raspberry Pi OS
- Download the Raspberry Pi Imager from the official site.
- Choose “Raspberry Pi OS Lite” – it’s a minimal Linux without a desktop, perfect for headless setups.
- Flash the image onto the micro‑SD card and pop it into the Pi.
Enable Wi‑Fi and SSH
After the first boot, open the boot partition on your computer and add an empty file named ssh. Then create a file called wpa_supplicant.conf with your Wi‑Fi credentials:
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
ssid="YourNetwork"
psk="YourPassword"
key_mgmt=WPA-PSK
}
Plug the Pi into power, wait a minute, then SSH into it (ssh [email protected]). Default password is raspberry – change it right away with passwd.
Step 2 – Wire the Relay Safely
Warning: You’re dealing with mains voltage (120 V or 230 V). If you’re not comfortable, ask a qualified electrician to double‑check your work.
- Connect the Pi’s 5 V pin to the relay board’s VCC, and a ground pin to GND.
- Use two GPIO pins (e.g., GPIO17 and GPIO27) for the relay control wires.
- On the relay side, wire the live (hot) wire from your wall switch to the “COM” terminal, and the “NO” (normally open) terminal to the light fixture. When the relay is energized, the circuit closes and the light turns on.
The relay board isolates the Pi from high voltage, so you’re safe as long as the board’s casing stays intact.
Step 3 – Install the Voice Engine
Choose Between Google Assistant and Open‑Source
For a $30 build, the simplest route is an open‑source speech‑to‑text engine like Vosk. It runs locally, no internet needed, and works well on a Pi Zero.
sudo apt update
sudo apt install python3-pip python3-venv
python3 -m venv venv
source venv/bin/activate
pip install vosk sounddevice
Download a small English model (about 50 MB) from the Vosk website and unzip it.
Test the Mic
If you’re using a USB mic, plug it in and run:
arecord -l
You should see the device listed. A quick test:
python - <<EOF
import sounddevice as sd, vosk, json, queue
model = vosk.Model("model")
q = queue.Queue()
def callback(indata, frames, time, status):
q.put(bytes(indata))
with sd.RawInputStream(samplerate=16000, blocksize=8000, device=1, dtype='int16',
channels=1, callback=callback):
rec = vosk.KaldiRecognizer(model, 16000)
while True:
data = q.get()
if rec.AcceptWaveform(data):
print(json.loads(rec.Result())['text'])
EOF
Speak “turn on the light” – you should see the text printed.
Step 4 – Write the Control Script
Create a file called smart_switch.py:
import RPi.GPIO as GPIO
import json
import vosk, sounddevice as sd, queue
# Pin setup
GPIO.setmode(GPIO.BCM)
LIGHT_PIN = 17
GPIO.setup(LIGHT_PIN, GPIO.OUT)
GPIO.output(LIGHT_PIN, GPIO.LOW) # start off
# Vosk setup
model = vosk.Model("model")
q = queue.Queue()
def audio_callback(indata, frames, time, status):
q.put(bytes(indata))
def listen():
with sd.RawInputStream(samplerate=16000, blocksize=8000,
device=1, dtype='int16', channels=1,
callback=audio_callback):
recognizer = vosk.KaldiRecognizer(model, 16000)
while True:
data = q.get()
if recognizer.AcceptWaveform(data):
result = json.loads(recognizer.Result())
text = result.get("text", "")
if "turn on" in text and "light" in text:
GPIO.output(LIGHT_PIN, GPIO.HIGH)
print("Light ON")
elif "turn off" in text and "light" in text:
GPIO.output(LIGHT_PIN, GPIO.LOW)
print("Light OFF")
elif "exit" in text:
break
if __name__ == "__main__":
try:
listen()
finally:
GPIO.cleanup()
This script does three things:
- Listens to the microphone continuously.
- Sends audio to Vosk, which returns plain text.
- Looks for the phrases “turn on the light” or “turn off the light” and toggles the GPIO pin accordingly.
Run it with python3 smart_switch.py. You should be able to command your lamp by voice.
Step 5 – Make It Run on Boot
You probably don’t want to SSH in every night to start the script. Add a systemd service:
sudo nano /etc/systemd/system/smartlight.service
Paste:
[Unit]
Description=Voice controlled smart light
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/pi/smart_switch.py
WorkingDirectory=/home/pi
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
Enable and start:
sudo systemctl daemon-reload
sudo systemctl enable smartlight.service
sudo systemctl start smartlight.service
Now the Pi will launch the voice listener automatically after power‑up.
Tuning Tips and Personal Tweaks
- Mic placement: I taped the USB mic inside a small 3D‑printed housing mounted near the ceiling. The higher you place it, the less it picks up kitchen chatter.
- Noise filtering: Adding a simple high‑pass filter in the code (
if len(text) < 3: continue) reduces false triggers from background hum. - Multiple lights: Use the second relay channel and add a second GPIO pin. Extend the phrase list to “turn on the desk lamp” and map it accordingly.
- Power savings: The Pi Zero draws about 120 mA at idle. If you want a truly low‑power switch, consider a microcontroller like the ESP‑32, but you’ll lose the easy Vosk integration.
Why This Project Rocks
- Cost: Most commercial smart switches start at $50 and lock you into a cloud service. Here you own the hardware and the code.
- Privacy: All voice processing stays on the Pi, so no data is sent to the internet.
- Learning: You get hands‑on with Linux, GPIO, and speech recognition – skills that pay off in any maker project.
I built this for the hallway light in my apartment. The first time I said “turn on the light” and the bulb flickered to life, I felt like a sci‑fi director shouting commands at a set. The best part? The whole thing fits inside a tiny project box that I printed on my Prusa. It’s a modest upgrade, but it turned a dull hallway into a little showcase of what a few dollars and a bit of curiosity can do.
If you run into hiccups, the Tech DIY Hub community on Discord is a great place to swap snippets and troubleshoot. Remember, the magic isn’t in the price tag – it’s in the joy of making something that actually works for you.
- → How to Build a Raspberry Pi-Powered Smart Home Hub from Scratch @techcraftinsights
- → How to Build a Smart Home Hub with a Raspberry Pi for Under $50 @techandtinker
- → Step-by-Step Guide: Build a Raspberry Pi Smart Light Switch for Under $30 @techandtinker
- → DIY Home Automation: Using Raspberry Pi to Control Your Lights and Locks @smarthomeliving
- → How to Build a Smart Automatic Feeder for Your Dog Using a Raspberry Pi @petfeederpro