How to setup relay for Home automation using Google Home, ESP32 and Arduino Cloud

Hardware Required:

ESP32 (I used ESP-WROOM-32)

5V relay

prerequisites:

Install Arduino Cloud agent by downflowing it from https://cloud.arduino.cc/download-agent/ and installing the downloaded file.

Navigate to https://app.arduino.cc/ and register an account, if already registered login with your details.

Arduino Cloud Setup:

Click on CREATE NEW -> Thing

Rename ‘thing’ with whatever name you want to. (even if you don’t do this, it is not going to affect the functionality, but I would recommend doing this for organizing things correctly)

On the right pane, under Associated Device click Select Device,

On the new window click on SET UP NEW DEVICE, (if you already have an ESP32 to choose from you can select that and skip SET UP NEW DEVICE steps below)

Select third party device,

Select ESP32 and Filter with ESP32 Dev Module and click Continue

Name the device appropriately and click Next (you can name it however you want, it is not going to affect functionality)

From the next screen, copy Device ID and Secret Key you can also choose to download it as PDF. Select I saved my Device ID and Secret Key and click CONTINUE (It is important save Device ID and Secret Key for future usage)

Click on DONE on next screen,

On the right pane, under Smart Home Integration, click Configure,

From the next screen select Connect to Google Home

click on GOT IT and finish the wizard.

Click the ADD under Cloud Variables in the main screen

Name it switch1 (my program going to refer it as “switch1”. If you name it different remember to use the same in your program)

From the drop down, select Google and scroll down and select Switch (it it important to do this as exactly, else you will not be able to add the Arduino in Google home Devices)

Rest of the settings, leave default and click ADD VARIABLE

Note: We have to configure Network tab (but we will do that later after flashing the program to esp32. Also if you have not added any device this might be grayed out for you to select)

At this point, screen should look like below,

From the left pane menu button click on Dashboards

Click on + DASHBOARD to create new one

Rename the Dashboard Appropriately.

Click EDIT button on top right

Click on ADD and filter with switch and Select the first toggle Switch from the list of items

Click Linked variable from the right pane

From the next screen, select the thing you created earlier and select switch1 variable and click LINK VARIABLE

Then click DONE

And click DONE again on dashboard screen top right

Now we will flash below program to ESP32 by connecting it your computer and Arduino IDE

Relay to be wired as below to ESP32, (Here I am assuming you are powering the ESP32 using micros USB and hence VIN will have 5V available to power the relay.

If you are using external power for ESP32, you can power it with VIN and be sure not provide more than 5V. Same can be used to power 5v relay.

Make sure to update DEVICE_LOGIN_NAME, SSID, PASS and DEVICE_KEY

DEVICE_LOGIN_NAME and DEVICE_KEY you saved earlier when adding ESP32 to Arduino Cloud. Here I am using GPIO 23 to wire the relay. (if you are using a different GPIO be sure change RelayPin1

Also be sure install ArduinoIoTCloud and Arduino_ConnectionHandlerlibrabries in Arduino IDE

// Relay logic: LOW = ON, HIGH = OFF (Active-Low Relay)

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
 
const char DEVICE_LOGIN_NAME[]  = "YOUR DEVICE ID";   //Enter de
const char SSID[]               = "YOUR HOME WIFI SSID";    //Enter WiFi SSID (name)
const char PASS[]               = "YOUR HOME WIFI PASSWORD";    //Enter WiFi password
const char DEVICE_KEY[]         = "YOUR SECRET KEY";    //Enter Secret device password (Secret Key)

#define RelayPin1 23  //D23

int toggleState_1 = 0; //Define integer to remember the toggle state for relay 1

void onLight1Change();

CloudSwitch switch1;

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  initProperties();
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, doThisOnConnect);
  ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, doThisOnSync);
  ArduinoCloud.addCallback(ArduinoIoTCloudEvent::DISCONNECT, doThisOnDisconnect);

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

  pinMode(RelayPin1, OUTPUT);

  //During Starting all Relays should TURN OFF
  digitalWrite(RelayPin1, LOW);

}

void loop() {
  ArduinoCloud.update();  
}

void onLight1Change() {
  if (switch1 == 1) {
    digitalWrite(RelayPin1, HIGH); // OFF (invert logic)
    Serial.println("Device1 OFF");
    toggleState_1 = 0;
  } else {
    digitalWrite(RelayPin1, LOW); // ON (invert logic)
    Serial.println("Device1 ON");
    toggleState_1 = 1;
  }
}

void initProperties(){
  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.addProperty(switch1, READWRITE, ON_CHANGE, onLight1Change);
}

void doThisOnConnect(){
  Serial.println("Board successfully connected to Arduino IoT Cloud");
}
void doThisOnSync(){
  Serial.println("Thing Properties synchronised");
}

void doThisOnDisconnect(){
  Serial.println("Board disconnected from Arduino IoT Cloud");
}

Once the program is flashed successfully, if you navigate to Arduino Cloud Menu – Devices you should see you ESP32 listed as online (ignore my esp32-testing device is offline, as I have not flashed the program on it)

At this point we will go back to the thing and configure the earlier skipped Network

in Arduino Cloud click on the Menu and select Things click on the thing you created earlier (in my case, it is testing)

On the next screen, on right pane, under Network Click Configure

Fill in your wifi network ssd, wifi password and secret key (which you earlier got when you added your ESP32 to Arduino Cloud devices) and click SAVE

Now the things screen will look like below.

Now can test it by going to Menu->Dashboards-> and click on the dashboard you created earlier and try toggling the switch on and off. If everything done correctly. You can hear the relay connected to ESP32 turning on and off. Congrats!

We will now add this ESP32 to google home. So that we can control this switch via google home app, as well using google voice.

Install Google Home app from playstore/apple store.

Launch the app and follow the prompts to link to a gmail address. Once the initial setup is done, From the home screen->Devices

Click on Add device

Click on Works with Google Home

Type in Arduino and click Arduino

Click Continue on the screen where it says “Link an account to control Arduino on any device”

On the next screen login with your Arduino Cloud credentials,

You should see “Linking your Arduino account” message

If all went well, on the next screen it should show the switch which you added in the Arduino Cloud Dashboard. Select the switch and click Add to a room

Select Family room (or whichever room for you) and click Next

Finally you can see switch1 on Devices screen. if all went well, you can toggle that see relay get turned on and off. Congrats!

You can also turn the relay on and off using voice command,

“hey google, turn on switch 1”

“hey google, turn off switch 1”

Note: I could not make hey google getting detected on Iphone though.

Note: you can follow https://raspiwrt.freeddns.org/2025/03/25/how-to-use-a-relay-with-raspberry-pi/ to understand how to wire the relay to an actual AC device.

Best of luck!

Leave a Reply

Your email address will not be published. Required fields are marked *