How to Build a Voice-Activated IoT Light Switch With Raspberry Pi for Under

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:

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:

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:

  1. Install Raspberry Pi OS on the micro SD card. Use the Raspberry Pi Imager tool.

  2. Insert SD card into Pi and connect keyboard, mouse, monitor.

  3. Boot up the Pi, run sudo raspi-config, and set:

  4. Update timezone

  5. Change user password
  6. Enable SSH

  7. Update the system with sudo apt update && sudo apt full-upgrade.

  8. 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:

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:

Here is a circuit diagram showing how to connect the components:

Follow these steps to construct the circuit:

  1. Insert the relay onto the breadboard.

  2. Use a male-to-female jumper wire to connect GPIO 17 to the relay IN pin.

  3. Connect the 5V and GND rails on the breadboard to the Pi's 5V and GND pins.

  4. Attach jumper wires from the relay VCC to 5V, and GND to ground.

  5. 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:

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:

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:

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:

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!