Full width home advertisement

Post Page Advertisement [Top]

 I resisted smart home stuff for years. It always felt like paying $40 for a smart plug just to turn a lamp on with your phone instead of, you know, your hand. But then I built my own voice-controlled relay using an ESP32, and I get it now — there's something genuinely satisfying about saying "turn on the workshop lights" while your hands are covered in solder flux, and having it actually happen.

This guide walks through wiring an ESP32 to control a relay, then hooking it up to Alexa or Google Home so you can control it by voice — no expensive smart home hub required.

ESP32 Voice Control with Alexa

How This Actually Works (No Fluff)

Here's the honest version of what's happening under the hood: your ESP32 doesn't "understand" your voice. Alexa or Google Assistant does the speech recognition on their servers, then sends a simple on/off command to your ESP32 over your home network. The ESP32 just needs to expose itself in a way that Alexa or Google Home can "see" as a smart device — and there are a couple of ways to do that.

The two most beginner-friendly approaches are:

  1. Fauxmo / ESPAlexa library — makes your ESP32 pretend to be a Philips Hue-compatible device, which Alexa already knows how to discover natively. No account setup, no cloud service, works entirely on your local network.
  2. IFTTT with Google Home or Alexa — a bit more setup, but works well if you want more flexibility (custom phrases, multiple actions, etc.).

I'll walk through the first method here since it's genuinely the fastest way to get from zero to "it works" — and it doesn't rely on a third-party server being online.

What You'll Need

  • 1x ESP32 development board
  • 1x Relay module (1 or 2 channel, depending on how many devices you want to control)
  • Jumper wires
  • The device you're controlling (a lamp, fan, etc. — plugged into the relay-controlled outlet)
  • An Amazon Echo or Google Home device already set up on the same Wi-Fi network

A safety note before we go further: if you're wiring the relay to control anything plugged into mains power (110V/220V), be careful. The relay's low-voltage side (control side) is safe to handle, but the high-voltage side switching your actual appliance is not something to get sloppy with. If you're not confident with mains wiring, use a pre-made smart plug with a relay module built in, or control something battery-powered/low-voltage instead.

Step 1: Wiring the Relay

  • Relay VCC → ESP32 5V (or VIN)
  • Relay GND → ESP32 GND
  • Relay IN → ESP32 GPIO 4 (or any free digital pin)

The relay's other side connects to whatever you're switching — a lamp's live wire routed through the relay's NO (normally open) terminal is the classic setup.

Step 2: Installing the ESPAlexa Library

In the Arduino IDE, go to Sketch > Include Library > Manage Libraries, and search for "Espalexa." Install it — it's actively maintained and handles all the annoying discovery-protocol stuff that Alexa needs, so you don't have to build that from scratch.

Here's a working example to get your first light switch talking to Alexa:

cpp
#include <Espalexa.h>
#include <WiFi.h>

const char* ssid = "YourWiFiName";
const char* password = "YourWiFiPassword";

#define RELAY_PIN 4

Espalexa espalexa;

void lightChanged(uint8_t brightness) {
  if (brightness > 0) {
    digitalWrite(RELAY_PIN, HIGH);
  } else {
    digitalWrite(RELAY_PIN, LOW);
  }
}

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  espalexa.addDevice("Workshop Light", lightChanged);
  espalexa.begin();
}

void loop() {
  espalexa.loop();
}

Upload this, and once your ESP32 connects to Wi-Fi, open your Alexa app, go to Devices > Discover Devices, and it should find "Workshop Light" within about 30 seconds. If it doesn't show up right away, don't panic — Alexa's discovery scan can be a little slow on the first try. Run it again.

ESP32 Voice Control with Alexa  2

Step 3: Testing It

Once it's discovered, just say "Alexa, turn on Workshop Light." You'll hear the little confirmation chime, and the relay should click almost instantly. The first time this actually worked for me, I said it out loud like three more times just because it felt like magic that I'd built myself.

If you want to use Google Home instead of Alexa, the setup is nearly identical — ESPAlexa actually works with Google Home too in many cases, since Google Home added support for discovering Hue-style devices. If it doesn't discover automatically, the IFTTT route (Google Assistant trigger → webhook to your ESP32's IP address) is the reliable fallback.

Common Problems (And How to Fix Them)

Alexa can't discover the device. Make sure your ESP32 and your Echo device are on the same Wi-Fi network — not a guest network, and not a separate 5GHz/2.4GHz band that's isolated from the main network on some routers. This trips up more people than anything else.

The relay clicks, but the light doesn't turn on. Double-check you've wired the relay's high-voltage side correctly — it needs to interrupt the live wire specifically, not the neutral. Wiring it on the neutral side means the relay clicks, but the circuit never actually breaks.

It worked yesterday, but Alexa says the device isn't responding today. Your ESP32 probably got a new local IP address after a router reboot. Setting a static IP for your ESP32 (either in your router's DHCP settings or in your Arduino code) will fix this permanently.

Multiple relays, but Alexa only ever controls one of them. You need to call espalexa.addDevice() once for each relay, each with a unique name and its own callback function. It's a common copy-paste mistake to leave two devices pointing at the same GPIO pin.

Taking It Further

Once you've got one light working, it's a short jump to controlling multiple outlets, dimming LED strips with PWM instead of a simple on/off relay, or combining this with a PIR motion sensor so lights turn on automatically and you can still override them by voice. That's actually where I ended up after a couple of weekends — my workshop now turns lights on when I walk in, but I can still yell at Alexa to turn them off if I'm heading out the door with my hands full.

ESP32 Voice Control with Alexa  3

Final Thoughts

This project has one of the best "wow, I actually built that" payoffs of anything on this blog. It's not complicated once you understand what's actually happening between your voice and the relay click, and it turns a $5 board into something that legitimately competes with commercial smart plugs — minus the subscription and the "cloud service is down" excuse when it randomly stops working.

Bottom Ad [Post Page]