Introduction
In this guide, I will show you how to build your own voice-activated IoT light switch using a Raspberry Pi. With just a few cheap components, you can create a custom voice control system that turns lights on and off in your home.
The total cost to build this project is under $50. The end result is a smart home device that responds to voice commands. You'll be able to control lights with your voice using Amazon Alexa or Google Assistant.
Overview of the Project
Here's a quick overview of what we'll cover in this guide:
- Raspberry Pi - The brains of our voice-controlled light switch. It handles voice recognition and controls the switch.
- Relay - An electronic component that will switch our lights on and off.
- Microphone - To capture voice commands. We'll use a USB microphone plugged into the Pi.
- Breadboard - For easily connecting components with jumper wires.
- Alexa or Google Assistant - For voice control and natural language processing.
By the end, we'll have a fully functional voice-controlled light switch built with the Raspberry Pi.
Components Needed
To complete this project, you'll need the following components:
- Raspberry Pi - Any Pi model will work, but I recommend Raspberry Pi 4.
- Micro SD Card - To store the OS and software. Get at least 16GB.
- Jumper wires - For connecting components to the Pi. Get male-to-female and male-to-male.
- Breadboard - To build circuits without soldering.
- 5V relay - For switching higher voltage/current devices.
- Power supply - A 5V 2.5A supply is recommended.
- Micro USB cable - For powering the Pi.
- USB microphone - Any USB mic will work for voice control.
- Light bulb - An LED or CFL bulb to control with relay.
Optional:
- Raspberry Pi case - For protecting the Pi.
That's everything you'll need! With all these parts, you can build an IoT voice light switch for around $40-$50.
Set Up the Raspberry Pi
The first step is to set up the Raspberry Pi.
Here's how to get your Pi ready:
-
Install Raspberry Pi OS on the micro SD card. Use the Raspberry Pi Imager tool.
-
Insert SD card into Pi and connect keyboard, mouse, monitor.
-
Boot up the Pi, run
sudo raspi-config
, and set: -
Update timezone
- Change user password
-
Enable SSH
-
Update the system with
sudo apt update && sudo apt full-upgrade
. -
Reboot Pi.
Now your Raspberry Pi is set up and ready to build projects on!
Next we need to install a few packages to enable voice control and GPIO access.
Run these commands on your Pi:
sudo apt install portaudio19-dev libffi-dev libssl-dev
sudo pip3 install RPI.GPIO gpiozero
That will install the needed GPIO and audio packages.
Connect the Voice Assistant
Next up is connecting a voice assistant to the Raspberry Pi. This will handle voice commands.
You have two options here:
-
Amazon Alexa - The Alexa Voice Service API allows you to integrate Alexa voice capabilities into your own products. We'll use this to add voice control to the Pi.
-
Google Assistant - The Google Assistant SDK lets you build Assistant into devices. Similar to Alexa, it enables voice interaction.
Both Alexa and Google Assistant work well for this project. I recommend Alexa as it's simpler to set up.
Follow one of these guides to enable voice control on your Pi:
Once complete, you'll be able to issue voice commands like "Alexa, turn on the light" or "Hey Google, turn off the light".
Hardware Circuit
Now for the hardware portion. We need to:
- Connect a relay to the Raspberry Pi
- Connect the relay to a light bulb
- Wire it up so voice commands activate the relay
Here is a circuit diagram showing how to connect the components:
Follow these steps to construct the circuit:
-
Insert the relay onto the breadboard.
-
Use a male-to-female jumper wire to connect GPIO 17 to the relay IN pin.
-
Connect the 5V and GND rails on the breadboard to the Pi's 5V and GND pins.
-
Attach jumper wires from the relay VCC to 5V, and GND to ground.
-
Screw in a light bulb to the NO/COM terminals on the relay.
That completes the hardware portion of this project. With Alexa/Assistant set up and the circuit wired, we're ready for software and coding next.
Writing the Python Code
Now we need to write a Python script that:
- Listens for voice commands
- Activates the relay based on those commands
This code will interface with Alexa/Assistant and control the GPIO pins.
Here is the full code:
```python
import pygame
import time
import gpiozero
relay = gpiozero.OutputDevice(17)
def listen():
pygame.mixer.init()
pygame.mixer.music.load("alexa_prompt.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
def turnOnLight():
print("Light turned on")
relay.on()
def turnOffLight():
print("Light turned off")
relay.off()
while True:
listen()
if check_for_on_command():
turnOnLight()
if check_for_off_command():
turnOffLight()
pygame.mixer.quit()
```
Let's break this down:
-
We initialize the relay as an output on GPIO pin 17.
-
The
listen()
function plays an Alexa prompt MP3. -
It waits for the prompt to finish before continuing.
-
We have functions to turn the relay on and off.
-
The main loop repeatedly listens for commands and toggles the relay.
-
The
check_for_on_command()
andcheck_for_off_command()
functions (not shown) would process the voice commands using Alexa/Assistant APIs.
So in essence, this Python script is listening for voice commands and controlling the relay based on those commands!
Save this code as a Python file and run it via:
python voice_switch.py
Once running, try issuing voice commands like "Alexa, turn on the light" to control your light.
Finishing Touches
Almost there! To complete the project:
-
Mount the Pi, breadboard, and relay in a case or enclosure.
-
Make sure all wires and connections are secure.
-
Position the hardware near an outlet and lamp you want to control.
-
Run the Python code on startup so it's always listening.
Now you've got a fully functioning voice-controlled IoT light switch built with Raspberry Pi!
Conclusion
Building your own DIY smart home devices is fun and inexpensive with Raspberry Pi. This project shows how you can use a Pi, relay, and voice control to construct a voice-activated light switch.
Some ways you could expand on this:
- Control multiple lights or appliances
- Add schedules, timers, and wireless control
- Integrate it into a home automation system
As you can see, the possibilities with Raspberry Pi are endless! With just a few cheap components, you can construct useful IoT and automation devices.
I hope you enjoyed this guide on constructing a voice-controlled light switch. Let me know if you have any other questions!