Build a Voice‑Controlled Light Switch with ESP32 for Under $15

Ever walked into a room, fumbled for the switch, and thought “there’s got to be a smarter way”? You’re not alone. With a tiny ESP32 board and a few pennies worth of parts, you can turn any lamp into a voice‑controlled fixture. No fancy hub, no monthly fees—just a little code, a bit of solder, and a lot of fun.

Why Now?

Smart speakers are everywhere, but most of them lock you into a brand’s ecosystem. If you want a light that listens to your voice without paying for a cloud subscription, the ESP32 is the perfect middle‑man. It’s cheap, it has Wi‑Fi, and it can run simple voice‑trigger code right on the chip. That means you can keep the control local, stay private, and still enjoy the magic of “Hey, turn on the light”.

What You’ll Need (All Under $15)

PartApprox. Cost
ESP32‑DevKitC (or any ESP32 board)$6
5 V Relay Module (single channel)$3
Mini electret microphone breakout$2
Small breadboard or perf board$1
Jumper wires, a bit of solder$1
120 V lamp and socket (already in your home)

Total: about $13. Prices vary, but you can usually find a combo pack of ESP32 + relay for under $10 on sites like AliExpress or eBay.

Safety First

We’re dealing with mains voltage (120 V or 230 V). If you’re not comfortable working with live wires, ask a qualified electrician to help you with the relay wiring. The ESP32 side stays low voltage, but the relay contacts will switch the lamp’s power. Keep the high‑voltage side isolated from the low‑voltage side—use a proper relay board that has opto‑isolation.

Wiring the Switch

  1. Power the ESP32 – Connect the board’s 5 V pin to the breadboard’s power rail and GND to ground. You can power it via USB or a 5 V wall adapter.
  2. Hook up the Relay
    • VCC → ESP32 5 V
    • GND → ESP32 GND
    • IN (control pin) → ESP32 GPIO 23 (you can pick any free pin).
      The relay’s COM (common) and NO (normally open) terminals will sit in series with the lamp’s hot wire. When the ESP32 drives the IN pin HIGH, the relay closes and the lamp gets power.
  3. Add the Microphone
    • VCC → ESP32 3.3 V (most mic modules run at 3.3 V)
    • GND → ESP32 GND
    • OUT → ESP32 GPIO 34 (an analog‑only pin).
      The mic will feed raw audio into the ESP32 for simple keyword detection.
  4. Secure the Connections – If you’re using a perf board, solder the wires for a permanent build. Otherwise, a breadboard works fine for testing.

Getting the Code onto the ESP32

We’ll use the Arduino IDE because it’s easy to set up. If you haven’t installed it yet, grab the latest version from arduino.cc, add the ESP32 board URL (https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json) in the preferences, and install the ESP32 package.

Libraries You’ll Need

  • ESPAsyncWebServer – to handle a tiny web endpoint for IFTTT.
  • ArduinoFFT – for a very basic voice trigger (detecting a clap or a short “on” shout).
  • WiFi – built‑in, for connecting to your home network.

You can install these via the Library Manager.

Sketch Overview

#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoFFT.h>

const char* ssid     = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

const int relayPin = 23;
const int micPin   = 34;

WiFiClient client;
AsyncWebServer server(80);
ArduinoFFT FFT = ArduinoFFT();

void setup() {
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW); // start with light off

  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected");

  // Simple web hook for IFTTT (optional)
  server.on("/toggle", HTTP_GET, [](AsyncWebServerRequest *request){
    toggleLight();
    request->send(200, "text/plain", "OK");
  });
  server.begin();
}

void loop() {
  // Very simple voice detection: look for a loud sound > threshold
  int sample = analogRead(micPin);
  if (sample > 2000) {          // adjust based on your mic
    toggleLight();
    delay(1000);                // debounce
  }
}

// Helper to flip the relay
void toggleLight() {
  bool state = digitalRead(relayPin);
  digitalWrite(relayPin, !state);
  Serial.println(state ? "Light OFF" : "Light ON");
}

What’s happening?
The ESP32 connects to Wi‑Fi, starts a tiny web server, and constantly watches the microphone’s analog output. When the sound level crosses a set threshold (you can test by shouting “on” or clapping), the toggleLight() function flips the relay. You can also call the /toggle URL from any phone or from IFTTT, letting you pair the switch with Google Assistant or Alexa without a dedicated skill.

Tuning the Mic

Every room sounds different. Open the Serial Monitor and watch the numbers that come from analogRead(micPin). Speak your trigger phrase and note the peak value. Raise the 2000 threshold if you get false triggers, or lower it if the ESP misses your voice.

Making It Voice‑Friendly

If you want a proper “Hey ESP, turn on the lamp” phrase, you can add the EasyVR library or use TensorFlow Lite for Microcontrollers. Those options need a bit more RAM and a slightly bigger board, but the basic clap‑or‑shout method works for most hobbyists and stays under $15.

For a cloud‑free solution, you can also pair the ESP32 with Home Assistant running on a Raspberry Pi. Add the ESP32 as a MQTT client, and let Home Assistant handle the voice intent. That way you keep the heavy lifting off the ESP32 and still enjoy natural‑language commands.

Testing the Finished Switch

  1. Plug the lamp into the relay’s COM‑NO circuit.
  2. Power the ESP32.
  3. Say “on” (or clap) loudly enough for the mic to pick up.
  4. The lamp should flick on. Say it again, and it should turn off.

If it doesn’t work, double‑check the relay wiring and make sure the mic’s VCC matches the board’s voltage. Also verify that the ESP32 is actually receiving Wi‑Fi – the Serial Monitor will tell you.

A Few Tips from My Workshop

  • Mount the board in a small project box – it keeps the electronics safe from dust and accidental touches.
  • Add a pull‑down resistor on the relay control pin (10 kΩ) to avoid the relay floating when the ESP32 boots.
  • Label the power side of the relay so you don’t mix up the hot and neutral wires later.
  • Play with the threshold – a quiet house needs a lower value, a noisy kitchen needs a higher one.

That’s it. You’ve turned a regular lamp into a voice‑controlled fixture for less than the cost of a coffee. The best part? You built it yourself, so you know exactly how it works and can tweak it any way you like.

Happy hacking, and may your rooms always light up when you say the word.

Reactions