ESP32 Smart Home Automation System With Alexa

by Rau7han in Circuits > Electronics

7912 Views, 5 Favorites, 0 Comments

ESP32 Smart Home Automation System With Alexa

demo_alexa_esp32_all_4_relays_on_alexa_active.jpg
alexa_esp32_topdown_flatlay_setup_01.jpg
alexa_esp32_setup_standby_all_relays_off_03.jpg
FEV86RXMTBYJY92.jpg

In this project, I will show you how I connected an ESP32 4-channel relay board with Amazon Alexa and controlled all four relay channels using voice commands.

I am using a ready-made relay controller that has the ESP32, four relays, relay driver circuits, optocouplers, status LEDs, power circuit, and USB Type-C programming interface on the same PCB.

Once the ESP32 connects to WiFi, Alexa can discover the four relay channels as separate devices. After that, each relay can be switched ON or OFF using an Echo Dot or directly from the Alexa app.

For example:

"Alexa, turn on Relay 1."

"Alexa, turn off Relay 2."

For the Alexa communication, I am using the Espalexa library. This lets the ESP32 work with Alexa on the local WiFi network without using another IoT platform such as Blynk or Arduino IoT Cloud.

Supplies

esp32_relay_board_angle_view_black_bg.jpg
esp32_4ch_relay_board_pinout_top_view.jpg
11_alexa_echo_dot_voice_command_listening.jpg
esp32_4ch_relay_board_terminals_view.jpg

For this project, you will need:

  1. ESP32 4-channel relay controller board
  2. Amazon Echo Dot
  3. Smartphone with the Amazon Alexa app
  4. WiFi network
  5. USB Type-C cable for programming
  6. Suitable DC power supply for the relay board
  7. Electrical loads for testing
  8. Connecting wires

Circuit Diagram & Hardware Design

output_page-0001.jpg
3d-modell.png
Screenshot from 2026-08-26 22-50-54.png

or this project, I used a ready-made ESP32 4-channel relay board that I purchased online.

The board is based on the ESP32-WROOM-32E and has the ESP32, four relays, relay driver circuits, optocouplers, status LEDs, and USB programming circuit on one PCB.

Each relay has three output terminals:

  1. COM: Common
  2. NO: Normally Open
  3. NC: Normally Closed

For a normal switching application, COM and NO can be used so that the load remains disconnected until the relay is activated.

The board also has optocoupler isolation between the ESP32 control section and the relay driver section.

If you already have this type of ESP32 4-channel relay board, you can use it directly without building a separate relay circuit.

I have also attached the schematic and Gerber files for a similar ESP32 4-channel relay board. The schematic includes the ESP32, USB-to-serial circuit, 5V to 3.3V regulator, auto-reset circuit, optocouplers, transistor relay drivers, flyback diodes, and four 5V relays.

You can use these files as a reference or send the Gerber files to a PCB manufacturer if you want to make your own version.

Note: Different ESP32 4-channel relay boards can use different GPIO pins. The board I am using controls its four relays through:

Relay 1: GPIO 26
Relay 2: GPIO 25
Relay 3: GPIO 33
Relay 4: GPIO 32

So check your board pinout before uploading the program.

Downloads

PCB Manufacturing With NextPCB

9.png
IMG_5587.JPG

I have also attached the schematic and Gerber files for the ESP32 4-channel relay board. If you want to make your own version, you can send the Gerber files to NextPCB for PCB manufacturing.

NextPCB provides PCB fabrication and assembly services for prototypes as well as small production runs.

Before ordering, you can also use HQDFM, NextPCB's free Gerber viewer and DFM analysis tool. It can help you check the PCB layers and find possible manufacturing issues before sending the board for production.

If you are planning to manufacture your own PCB, you can check the latest PCB and PCBA offers directly on the NextPCB website.

How the Alexa Home Automation System Works

ChatGPT Image Aug 28, 2026, 11_31_42 PM.png
demo_alexa_esp32_relay_1_on.jpg
FOI1RTGMTBYJXRK.jpg

The ESP32 connects to the same WiFi network as the Amazon Echo Dot. To make the ESP32 understandable to Alexa, this project uses the Espalexa library.

Espalexa makes the ESP32 appear to Alexa as a compatible smart-home device. It handles device discovery and receives the ON/OFF commands sent by Alexa, so we do not need to create our own Alexa skill or use a separate IoT platform.


In the code, each relay is added to Espalexa as a separate device, such as Relay 1, Relay 2, Relay 3, and Relay 4.

When you say:

"Alexa, turn on Relay 1"

Alexa sends the command through the WiFi network. Espalexa receives the command on the ESP32, and the ESP32 switches the GPIO connected to Relay 1.

The same process works for all four relays.


So the basic communication is:

Alexa / Echo Dot → WiFi Router → ESP32 + Espalexa → Relay → Appliance

Install ESP32 Board and Espalexa Library

Before uploading the code, we need ESP32 support and the Espalexa library in Arduino IDE.

First, make sure the ESP32 board package is installed.

Then open:

Sketch > Include Library > Manage Libraries

Search for:

Espalexa

and install it.

You can also get the library from the official Espalexa GitHub repository.

After installing everything, connect the ESP32 relay board to your computer using a USB Type-C cable.

This board already has an onboard USB-to-serial interface, so no separate FTDI programmer is required.

In Arduino IDE, select your ESP32 board from:

Tools > Board

Then select the correct port from:

Tools > Port

That is all we need for the software setup.

I shortened this section because Step 2 already explains what Espalexa does. Your current Step 3 repeats that explanation almost word for word

Program and Upload the ESP32 Alexa Code

The program creates four Alexa devices and connects each one to a relay output on the ESP32.

For my board, the relay GPIO pins are:


#define RELAY1 26
#define RELAY2 25
#define RELAY3 33
#define RELAY4 32

Here is a cleaner version of the program:


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

// Relay GPIO pins
#define RELAY1 26
#define RELAY2 25
#define RELAY3 33
#define RELAY4 32

// WiFi details
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

Espalexa espalexa;


// Relay 1
void relay1Changed(uint8_t brightness)
{
if (brightness > 0)
{
digitalWrite(RELAY1, HIGH);
Serial.println("Relay 1 ON");
}
else
{
digitalWrite(RELAY1, LOW);
Serial.println("Relay 1 OFF");
}
}


// Relay 2
void relay2Changed(uint8_t brightness)
{
if (brightness > 0)
{
digitalWrite(RELAY2, HIGH);
Serial.println("Relay 2 ON");
}
else
{
digitalWrite(RELAY2, LOW);
Serial.println("Relay 2 OFF");
}
}


// Relay 3
void relay3Changed(uint8_t brightness)
{
if (brightness > 0)
{
digitalWrite(RELAY3, HIGH);
Serial.println("Relay 3 ON");
}
else
{
digitalWrite(RELAY3, LOW);
Serial.println("Relay 3 OFF");
}
}


// Relay 4
void relay4Changed(uint8_t brightness)
{
if (brightness > 0)
{
digitalWrite(RELAY4, HIGH);
Serial.println("Relay 4 ON");
}
else
{
digitalWrite(RELAY4, LOW);
Serial.println("Relay 4 OFF");
}
}


void setup()
{
Serial.begin(115200);

pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);

// Keep all relays OFF at startup
digitalWrite(RELAY1, LOW);
digitalWrite(RELAY2, LOW);
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY4, LOW);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

Serial.print("Connecting to WiFi");

while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}

Serial.println();
Serial.println("WiFi connected");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());

// Add relay devices to Alexa
espalexa.addDevice("Relay 1", relay1Changed);
espalexa.addDevice("Relay 2", relay2Changed);
espalexa.addDevice("Relay 3", relay3Changed);
espalexa.addDevice("Relay 4", relay4Changed);

espalexa.begin();

Serial.println("Espalexa started");
Serial.println("Ready for Alexa discovery");
}


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

Before uploading, replace:


const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

with your own WiFi name and password.

I strongly recommend replacing the WiFi credentials currently visible in your draft before publishing it. Your uploaded draft contains an actual SSID and password in the code.

After editing the WiFi details:

  1. Connect the ESP32 board with USB Type-C.
  2. Select the correct ESP32 board.
  3. Select the correct COM port.
  4. Click Upload.
  5. Open the Serial Monitor at 115200 baud.

After the ESP32 connects, you should see something similar to:

Connecting to WiFi...
WiFi connected
IP Address: 192.168.x.x
Espalexa started
Ready for Alexa discovery

Now the ESP32 is ready to be added to Alexa.

Configure Alexa

01_alexa_app_launch_screen.png
02_alexa_app_devices_tab.png
03_alexa_app_add_device_button.png
04_alexa_app_add_device_menu.png
05_alexa_app_select_device_type.png
06_alexa_app_select_brand_or_other.png
07_alexa_app_discover_devices_prompt.png
11_alexa_echo_dot_voice_command_listening.jpg
09_alexa_app_devices_discovered_success.png
10_alexa_app_all_relays_status_on.png
08_alexa_app_discovering_devices_progress.png

Make sure the ESP32 and Echo Dot are connected to the same WiFi network.

Since the ESP32 uses 2.4 GHz WiFi, I used the same 2.4 GHz network during setup.

Open the Amazon Alexa app and go to:

Devices > + > Add Device

Select:

Light > Other

Then tap:

Discover Devices

Alexa will search the local network for the devices created by Espalexa.

After discovery, you should see:

  1. Relay 1
  2. Relay 2
  3. Relay 3
  4. Relay 4

You can keep these names for testing or rename them later to something more natural, such as Light, Fan, Lamp, or Room Light.

At this point, you can control the relays from the Alexa app or with voice commands.

This step in your current post is already good. I mainly shortened the wording and removed phrases such as "For best results" that were not really necessary.

Test the Relays With Alexa

alexa_esp32_topdown_flatlay_setup_02.jpg
demo_alexa_esp32_all_4_relays_on_alexa_active.jpg
demo_alexa_esp32_relay_1_2_3_on_alexa_listening.jpg
demo_alexa_esp32_relay_1_and_2_on.jpg
demo_alexa_esp32_relay_1_on.jpg
alexa_esp32_setup_standby_all_relays_off_02.jpg
alexa_esp32_setup_standby_all_relays_off_01.jpg

After Alexa discovers all four devices, we can test the complete setup.

Say:

"Alexa, turn on Relay 1."

Relay 1 will switch ON and its status LED will light up, showing that the command was received successfully.

Then say:

"Alexa, turn off Relay 1."

The relay and its status LED will turn OFF.

In the same way, test the other relay channels:

  1. "Alexa, turn on Relay 2."
  2. "Alexa, turn on Relay 3."
  3. "Alexa, turn on Relay 4."
  4. "Alexa, turn off Relay 1."
  5. "Alexa, turn off Relay 2."
  6. "Alexa, turn off Relay 3."
  7. "Alexa, turn off Relay 4."

You can also switch the relays ON and OFF directly from the Alexa app. The reference project demonstrates the same voice-command and Alexa-app control method.

For this demonstration, I am not connecting any AC appliance or external load. The onboard relay LEDs are enough to confirm that Alexa, WiFi, Espalexa, and all four relay channels are working correctly.