Voice-Controlled LED Using ESP32 and Arduino Cloud with Google Assistant

I made a voice controlled LED with an ESP32 that I can turn on from Google Assistant.
Aug 06, 2024 — 5 mins read — Remote Control

Voice-Controlled LED Using ESP32 and Arduino Cloud with Google Assistant

Picture a smart home from the movies. You come in from the front door and are immediately greeted by a voice telling you your home's latest events and conditions. You then reply to set a certain mood and all the lights and music adjust accordingly.

While this can be achieved, voice control is still far off from everyday use in such scenarios. However, a step in that direction is to create a device that you can control with your voice.

In the video below, I created a DIY example of how you can use an ESP32, the Arduino Cloud, and Google Assistant to create a device where you can control an LED with your voice.




Tools and Materials

By buying through the links below you support my channel and my work, without any additional cost for you!

Tools and materials used in the video:

Other Smart Home Gear


Setting Up Arduino Cloud

To start, you will need an Arduino Cloud account. The Arduino Cloud is a paid service, but you can have two devices for free so that you can test it out and make sure that you like it before you commit to buying.

This is handy for anyone willing to have just one or two devices without needing to set up a more complicated system like Home Assistant.

Once you create your account, you will need to create a new "thing" where you first need to set up the device that you will use. You have multiple options here and I selected ESP32 as that is what I will be using.

Next, you need to set your WiFi details and enable the Google Home integration for the device.


Creating the LED Control in Arduino Cloud

A device without any controls won't be of much use so to make it helpful, we need to add a switch control to our thing. This is done in the Cloud Variables section of our thing where I selected a "CloudSwitch", making sure to select the variable that had the Google Home logo on the right so that it can be recognized by Google Home.

You need to set a name for this variable, and I set it to LED so that I can later refer to it by this name.

With that, we are now ready to modify the generated Arduino sketch so that we can add the control to our LED based on the Cloud Switch that we just added.


Coding the ESP32

Adding a cloud variable to our device does not automatically control any of the pins of our microcontroller. As the name implies, this is just a front-facing variable that we can then use in the rest of the code to act upon and control whatever we want.

With the variable, we automatically get a function that is called each time the variable value changes so this is the perfect spot to add our logic.

I've connected my LED to pin 15 so in the 'onLEDChange' method, I added code to check the variable value and turn on or off pin 15 based on the state of the cloud variable.

The full sketch is available below.

/* 
 Sketch generated by the Arduino IoT Cloud Thing "LED Example"

 Arduino IoT Cloud Variables description

 The following variables are automatically generated and updated when changes are made to the Thing

 CloudSwitch lED;

 Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
 which are called when their values are changed from the Dashboard.
 These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#define LEDPIN 15
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); 

 pinMode(LEDPIN, OUTPUT);

 // Defined in thingProperties.h
 initProperties();

 // Connect to Arduino IoT Cloud
 ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
 /*
   The following function allows you to obtain more information
   related to the state of network and IoT Cloud connection and errors
   the higher number the more granular information you’ll get.
   The default is 0 (only errors).
   Maximum is 4
 */
 setDebugMessageLevel(2);
 ArduinoCloud.printDebugInfo();
}

void loop() {
 ArduinoCloud.update();
 // Your code here 
  
  
}



/*
 Since LED is READ_WRITE variable, onLEDChange() is
 executed every time a new value is received from IoT Cloud.
*/
void onLEDChange() {
 // Add your code here to act upon LED change
 if(lED) {
  digitalWrite(LEDPIN, HIGH);
 } else {
  digitalWrite(LEDPIN, LOW);
 }
}


The final step is to upload this sketch to our device and this is easily done through the web interface in the same fashion as with the Arduino IDE.


Integrating with Google Home

If you don't have it already, you can go ahead and install the Google Home app. Here you have the option to add new devices and when asked, you need to select adding a device that works with Google Home and select Arduino from the list.

After adding your account details, you will need to select the device we just created and after adding it to a room in your home, the integration will be done and you should see the switch control in your Google Home app.

Pressing on that should now turn on the LED and at the same time, we should have Google Assistant being able to control the LED through voice commands.


Next Steps

With the basic LED control implemented, it is now up to you to modify the setup and expand it with multiple devices, additional lights, extra sensors, or anything else that you might think of.

Since you are writing your own code in Arduino Cloud, the possibilities are endless and you can combine them all with scenes, automation, and schedules in Google Home.

While this only works with a working Internet connection, you still have great flexibility and power to create an intricate setup and customize it to your needs, without investing too much into running an offline smart home system.

If you do create a device, feel free to share it with me in the comments.

Make sure to check out my YouTube channel for more electronics and coding videos and make sure to subscribe so you don't miss any of the future ones.

esp32 google home assistant voice smart home
Read next

DIY Sound Reactive LED Accent Wall

As someone who loves technology, I've always wanted to make a centerpiece for my home to highlight my love of electronics. In this project,...

You might also enojy this

Trail mapping with GPS, ESP32 and microSD card

Where I live, hiking is a big thing as we have plenty of mountains around and there are a ton of trails that people can explore and enjoy.Ho...