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
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
For this project, you will need:
- ESP32 4-channel relay controller board
- Amazon Echo Dot
- Smartphone with the Amazon Alexa app
- WiFi network
- USB Type-C cable for programming
- Suitable DC power supply for the relay board
- Electrical loads for testing
- Connecting wires
Circuit Diagram & Hardware Design
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:
- COM: Common
- NO: Normally Open
- 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:
So check your board pinout before uploading the program.
Downloads
PCB Manufacturing With NextPCB
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
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:
Here is a cleaner version of the program:
Before uploading, replace:
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:
- Connect the ESP32 board with USB Type-C.
- Select the correct ESP32 board.
- Select the correct COM port.
- Click Upload.
- Open the Serial Monitor at 115200 baud.
After the ESP32 connects, you should see something similar to:
Now the ESP32 is ready to be added to Alexa.
Configure Alexa
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:
- Relay 1
- Relay 2
- Relay 3
- 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
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:
- "Alexa, turn on Relay 2."
- "Alexa, turn on Relay 3."
- "Alexa, turn on Relay 4."
- "Alexa, turn off Relay 1."
- "Alexa, turn off Relay 2."
- "Alexa, turn off Relay 3."
- "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.