DIY smart home project. Build your smart relay with ESP32 and Arduino Cloud
Welcome to our DIY guide on building a Smart Relay for home automation using an ESP32 and a relay module board! In this post, we’ll walk you through each step—from gathering components to setting up the Arduino Cloud and uploading your code. Whether you’re new to microcontrollers or an experienced maker, this tutorial is tailored to make home automation simple. With this project, you can control appliances or lighting with your voice using Alexa or Google Home. If you’re interested in boards that integrate both ESP32 and relay or want to design your own custom board, this is the perfect starting point. Let’s empower your home with smart technology!
What is a Smart Relay?
Before we start with our DIY project, let’s begin win the basics. A smart relay combines the basic functionality of a traditional relay (an electrically operated switch) with added «smart» features, such as:
- Remote Control:
It can be controlled remotely through wireless communication protocols such as WiFi, Bluetooth, or even through IoT platforms. This means you can switch devices on or off using a smartphone app, voice command (via Alexa or Google Home), or web-based interfaces. - Automation and Scheduling:
Integrated with microcontrollers, smart relays can execute pre-programmed schedules or respond to sensor inputs. For example, you can program them to turn lights on at sunset or activate appliances when certain conditions are met. - Monitoring and Feedback:
Some smart relays provide feedback on the device’s status, energy consumption, or even potential faults. This feature is especially useful in energy management and preventive maintenance. - Integration in Smart Home Systems:
They are often designed to be easily integrated with home automation systems, allowing them to communicate with other smart devices. This helps create coordinated and automated environments where multiple devices work in sync.
1. What you need for this DIY project
Before you begin, make sure you have the following components:
- ESP32 microcontroller board: the heart of the project that processes and transmits data.
- Relay module board: allows you to switch high-power appliances using low-power signals.
- Arduino IDE: your coding environment to write, compile, and upload code.
- Arduino cloud account: For device management and remote control.
- Code: The program that integrates all functionalities.
- (Optional) A combined board (ESP32 + Relay + Power Supply) – available on online stores.
For detailed product options and comparisons, you can explore the official Arduino IoT cloud documentation and various ESP32 resources.
2. Procedure
Setting up Arduino Cloud
- Create or log in to your Arduino cloud account:
Start by signing up or logging in at the Arduino Cloud portal. Once logged in, navigate to the Devices section from the left sidebar.- Reference: For more on Arduino Cloud account setup, visit the Arduino IoT Cloud Documentation.
- Add a new device:
Click the +device button and select “Third party device”. Choose the ESP32 option and then select your specific board type.
Name your device (this is important, as it must match the identifier you use in your code later). A unique identifier will be generated—download the PDF containing your device ID and key for backup. - Configure your “thing”:
In the left sidebar, select Things, then click +thing. Choose your board from the taskbar on the right, and add the variables (relays in this case) you want to control.- Note: Each variable corresponds to a relay. Make sure the names in the Arduino Cloud and your Arduino code are identical.
- Create your dashboard:
Now, go to Dashboards and click +dashboard. Add virtual switches to control each output by linking them to the corresponding variable you created.
The code
Next, open the Arduino IDE and paste the code snippet below. Be sure to replace the placeholders with your WiFi credentials, device ID, and key. You can also customize the GPIO ports to suit your hardware configuration.
For a complete walkthrough of the code, including detailed explanations of each function and how the Arduino IoT Cloud libraries work, refer to the Arduino IoT Cloud GitHub repository or check out the official Arduino IoT Cloud reference.
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
const char DEVICE_LOGIN_NAME[] = "YOUR_DEVICE_ID"; // Replace with your device ID
const char SSID[] = "YOUR_WIFI_SSID"; // Replace with your WiFi name
const char PASS[] = "YOUR_WIFI_PASSWORD";// Replace with your WiFi password
const char DEVICE_KEY[] = "YOUR_DEVICE_KEY"; // Replace with your secret device key
// Define GPIO connections for relays
#define RelayPin1 23
#define RelayPin2 19
#define RelayPin3 18
#define RelayPin4 5
// Define GPIO connections for manual switches
#define SwitchPin1 13
#define SwitchPin2 12
#define SwitchPin3 14
#define SwitchPin4 27
#define wifiLed 2 // Indicator LED pin for WiFi connection
// Variables to store relay states
int toggleState_1 = 0;
int toggleState_2 = 0;
int toggleState_3 = 0;
int toggleState_4 = 0;
// Cloud Switch States
bool SwitchState_1 = LOW;
bool SwitchState_2 = LOW;
bool SwitchState_3 = LOW;
bool SwitchState_4 = LOW;
// Function prototypes for relay control on cloud events
void onLight1Change();
void onLight2Change();
void onLight3Change();
void onLight4Change();
CloudSwitch light1;
CloudSwitch light2;
CloudSwitch light3;
CloudSwitch light4;
void initProperties() {
ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
ArduinoCloud.addProperty(light1, READWRITE, ON_CHANGE, onLight1Change);
ArduinoCloud.addProperty(light2, READWRITE, ON_CHANGE, onLight2Change);
ArduinoCloud.addProperty(light3, READWRITE, ON_CHANGE, onLight3Change);
ArduinoCloud.addProperty(light4, READWRITE, ON_CHANGE, onLight4Change);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
void setup() {
Serial.begin(9600);
delay(1500);
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
pinMode(wifiLed, OUTPUT);
pinMode(SwitchPin1, INPUT_PULLUP);
pinMode(SwitchPin2, INPUT_PULLUP);
pinMode(SwitchPin3, INPUT_PULLUP);
pinMode(SwitchPin4, INPUT_PULLUP);
// Turn all relays OFF at startup
digitalWrite(RelayPin1, HIGH);
digitalWrite(RelayPin2, HIGH);
digitalWrite(RelayPin3, HIGH);
digitalWrite(RelayPin4, HIGH);
}
void loop() {
ArduinoCloud.update();
// Call your manual control function to check the physical switches (not shown fully here)
}
// Cloud switch callback functions:
void onLight1Change() {
if (light1) {
digitalWrite(RelayPin1, LOW);
toggleState_1 = 1;
Serial.println("Device1 ON");
} else {
digitalWrite(RelayPin1, HIGH);
toggleState_1 = 0;
Serial.println("Device1 OFF");
}
}
void onLight2Change() {
if (light2) {
digitalWrite(RelayPin2, LOW);
toggleState_2 = 1;
Serial.println("Device2 ON");
} else {
digitalWrite(RelayPin2, HIGH);
toggleState_2 = 0;
Serial.println("Device2 OFF");
}
}
void onLight3Change() {
if (light3) {
digitalWrite(RelayPin3, LOW);
toggleState_3 = 1;
Serial.println("Device3 ON");
} else {
digitalWrite(RelayPin3, HIGH);
toggleState_3 = 0;
Serial.println("Device3 OFF");
}
}
void onLight4Change() {
if (light4) {
digitalWrite(RelayPin4, LOW);
toggleState_4 = 1;
Serial.println("Device4 ON");
} else {
digitalWrite(RelayPin4, HIGH);
toggleState_4 = 0;
Serial.println("Device4 OFF");
}
}
Note: For further details on code structure, check this Arduino IoT Cloud code reference.
Connecting to Alexa/Google Home
To integrate voice control, navigate back to the Things tab in the Arduino Cloud. Under the Smart Home Integration option, configure the connection for either Alexa or Google Home.
- On your smartphone, open the respective app (Google Home or Alexa).
- Tap + Add Device and choose “Works with Google Home” (or the equivalent option for Alexa).
- Search for “Arduino” and follow the linking instructions.
- Once linked, your devices will appear in the app, ready for voice commands and automation setups.
Conclusion
If everything went as planned, your ESP32 should now be online and controllable via both the Arduino Cloud dashboard and your voice assistant. We hope you enjoyed this step-by-step journey into building a smart relay!
Thank you for reading until the end. We invite you to explore more detailed DIY projects in our DIY Projects category and subscribe to our YouTube channel for more tutorials and tech insights. Let’s continue to make our homes smarter, one project at a time!
A very interesting and informative read!
This site consistently shares high-quality and useful articles.
Just wish to say your article is as amazing. The clearness in your post is simply great and i
can assume you are an expert on this subject. Well with your permission allow me to grab your feed to
keep up to date with forthcoming post. Thanks a million and please keep up the rewarding work.