04 Easy IoT project w/ Arduino IoT Cloud - ESP32 | DHT11 Humidity & Temperature | Relay & DC Motor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
for today's lesson you'll learn how to build your first iot project using the Arduino iot Cloud you'll learn how to use electromechanical relays to digitally switch on and off basically and electrical devices like electric fans and lights you'll start by controlling a DC geared motor and learn how to attach an external Supply to your board to control high power devices you'll also learn how to read temperature and humidity data from a dht-11 sensor via the Arduino iot cloud and visualize it in a dashboard at the end of this lesson you'll be able to conduct your own experiment to control an LED's brightness via the Arduino iot Cloud Web and mobile dashboard together with remotely controlling the motor on and off and reading the humidity and temperature data hi my name is joyet go and welcome to lesson 4 of the iot development training course with esp32 for this experiment you'll need the following materials a current limiting resistor an LED a dht11 or dht22 sensor for humidity and temperature sensing a 5 volt SP detail relay or you can also use a 12 volt relay if you have one a three to six volt DC geared motor your esp32 development board a breadboard a couple of connecting wires and a micro USB data cable to start our circuit connection ensure that the ground and the 3.3 volt supply of your esp32 development board are connected to your breadboard's negative and positive bus strips then connect the gpio23 to the current limiting resistor and your LED this is the first device that we will be controlling over the cloud next we are also going to control a DC motor so typically DC motors come into wires and they can usually run with either polarity and for this to work we need to use an external Supply based on the voltage rating of our motor it means that you cannot use directly the 3.3 volts output of your esp32 gpio pins to power devices like this DC motor and other actuators that require higher voltage and current for them to operate in that case I have here a 5 volt 2.5 ampere easy to DC power adapter which is enough to drive this motor in your setup you can use any power adapter that you have a 9 volt battery or even your phone's charger and a USB charging cable will do so I'll plug this into my AC outlet and what I did is I've cut the other end of my USB charging cable to get only the positive and the negative wires and then attach pin headers to it now to illustrate how this works I'll connect this external 5 volt DC supply to the upper bus strip of this breadboard notice that the ground of my external Supply is now connected to the ground of my esp32 board so just to clarify the upper bus trip now uses 5 volts from an external Supply while the lower bus strip uses 3.3 volts from the esp32 and then using these two connecting wires I'll connect them to this 5 Volt Bus strip notice that our DC geared motor is now rotating in a clockwise Direction so reversing the polarity also reverses the direction of the motor rotation for now we're not yet going to talk about how to create a reversible motor control what we only want to do is to make this motor rotate regardless of Direction when we switch it remotely using our phone or a web browser via the Arduino iot cloud so to explain this further I'll connect another black wire here to the ground and then create a simple concept of a switch notice that as I tap these two wires the electricity flows through them and the motor rotates now how do we control this digitally as in without my fingers manually acting as a switch to have these two wires come in contact with each other and one option is to use a relay so I have here a 5 volt DC spdt relay spdt means single pole double throw and the question is how does it work a relay is an electromechanical switch used to control high power devices through a low powered signal from electronic circuits for example a simple logic circuit working under 5 volt DC cannot drive high voltage devices like light bulbs or electric fans so by introducing a relay component we can easily control these devices an sbdt relay contains two coil Terminals and then a common terminal and two switching terminals normally open and normally closed if there is not enough DC Supply in the coil terminals then a relay represents an idle condition that is the common terminal is connected to the normally closed terminal however when the coil gets the required DC Supply in our case it requires only 5 volts then the coil gets magnetically energized and this magnetic force attracts the common terminal level which is basically made of iron to make contact or switch to the normally open Terminal this makes the normally closed terminal becomes open now I've got here a relay module that has a pin out of negative positive and Signal pins respectively on the output side of this relay module is another set of terminal connectors this represents the common normally open and the normally closed terminals it says here that it can drive electrical devices with a rating of up to 10 ampere 250 volts ac so technically this relay is not just designed to switch on and off a 3 to 6 volt DC motor but we can even drive electrical appliances like your electric fan lights and television so long as you don't exceed this specified rating so to use this relay I'll connect the negative and the positive terminals of my external Supply to this strip and this third wire is for the digital signal that will be sent from our esp32 gpio pin for now I'll not connect it yet next we need to connect the three pins of this relay module to the negative positive and Signal pins make sure to align this properly now I'll connect the black wire of our DC motor to the common terminal of this relay and then to test if this is the normally closed terminal I'll tap the ground here and sure enough the current flows and the motor rotates this confirms that these two terminals are normally connected by default so it means that this is the normally open Terminal and this is where I'm going to connect this ground and then I'll tighten the screws later near the end of this video I have a bonus clip I'll show you how to connect an actual AC electric fan to the same circuit now to energize the coil inside this relay this wire must receive a logic High signal from any electronic device like a gpio pin of this esp32 and to test this I'll simply tap this input signal wire to the 5 volt Supply just to simulate the logic High signal that will be coming from our esp32 and the coil gets energized and the switching happens now I'll connect the signal pin to the gpio 22 of my esp32 so far we only have two devices to control an LED and a DC motor let's have one more component the dht-11 sensor the hd11 sensor is a low-cost digital sensor for sensing temperature and humidity and you can easily interface this with any microcontroller such as your esp32 Arduino and Raspberry Pi basically this sensor has four pins and it requires the use of a pull-up resistor to its data pin but since we are using a module it only exposes three pins and the required resistor is already implemented on the board so when using module like this make sure to check the correct pin out it depends on who produces it well the common Arrangement is negative output and positive from right to left however mine here is arranged as negative positive and then output from right to left also note that you can use either 3.3 volts or 5 volts to power this DHT 11 sensor the main difference is that when using 5 ball Supply you can have a much longer wire like 10 feet long and this allows you to place your DHT 11 sensor in a more appropriate location like outside your house to read humidity and temperature data so here I'll just use the 3.3 volts and then the output pin will be connected to the gpio 13 as a digital input to my esp32 similarly I'll place this dht11 module on the breadboard make sure that it is aligned properly and then plug my esp32 into my computer so we can start writing code using the Arduino iot Cloud platform to start using the Arduino iot cloud and for us to upload our program to our esp32 via the web editor first we need to install the Arduino create agent well Arduino Cloud uses its own agent to be the middleman between arduino's web editor the iot cloud in your computer so go to the Arduino create agent installation and click Start download the installation file by the way the Mac OS and Linux versions also exist execute the installation file and this is fairly straightforward once the agent is correctly installed the Arduino create agent tray icon should appear at the taskbar of your system click on the tray icon and click go to Arduino create a web page will launch allowing you to either sign in using an existing account or to create one you can create an account by clicking this link or you can sign in with your existing Google account GitHub Facebook or Apple account so I'll sign in with my Google account and to begin click this iot Cloud link and this iot Cloud page allows you to manage your things dashboards devices Integrations and templates Under The Things tab it offers you to create your first name so a thing is a connected device that can communicate with the cloud and a thing can interact with other things or anything else in the physical world so let's create our first thing I'll give this a title of my first esp3210 now when creating a thing you also need to create Cloud variables and basically Cloud variables are just Cloud representation of the physical component that you want to monitor or control in our case we want to control two components an LED and a motor and we could have two separate variables for those we also want to monitor the temperature and humidity through our dht-11 sensor and we could add two more Cloud variables for those so I'll add my first variable and call it lead underscore switch and just like any other variable it requires a data type well Arduino iot Cloud offers a variety of variable types by default it shows only the basic types such as Boolean character float and integer I can uncheck this and we can now see a whole lot of different variable types to choose from like acceleration data rate dim light you'll be using this one later in your activity Challenge and a lot more so for this first variable I'll just use the basic if we want to Simply turn on and off an LED we can just use a Boolean type and this will automatically create a declaration of bull lead underscore switch in our sketch file for the variable permission if this variable is meant to be linked to an output component like an LED then choose read and write however if a variable is meant to be linked to an input component like a temperature sensor then use read only note that a read and write variable can be updated from the board and from the cloud while a read-only variable can only be updated from the board in this variable update policy the on change means that we can write a code that responds to something when this variable changes its value think of it more like an event listener wherein a callback method gets fired when change is detected for example in a mobile app if I toggle a switch button from on to off we can write a code that responds to this change like turning on an LED or turning on a motor periodically means that your variable synchronizes every hexagons like a time interval so I'll add this led switch variable now this thing which only has one variable at the moment must be Associated to an actual device when I click this it asks me to set up a new device since we don't have an existing device configured previously but if you do have one you should be able to select it from here now we can either set up an Arduino device and as you can see there are several Arduino boards that you can choose from for your iot project but for this thing I'll set up a third-party compatible device so I'll select esp32 and the model is esp32 Dev board note that I am selecting this model because what I have here is a generic 38 pin node MCU Dev board please choose your specific Board model based on what you have then usually the iot cloud automatically suggests a name for your device and you can simply change it to anything I want mine to be esp32 node MCU so I'll click next now later in your program you need this auto-generated device ID and the secret key you'll be needing these credentials for your board to be recognized by the iot cloud also note that the secret key once lost cannot be recovered so keep it safe or else you'll have to delete and set up your device again and before you click this continue button make sure to download this credential in PDF and then accept it and click continue and we're done here so we now have an Associated device but it is currently offline because there is no activity yet and the next thing that we need to configure is this network so provide here the name of your Wi-Fi network wherein your esp32 will connect I'll connect to the same test Wi-Fi in my room jogo Galaxy and the password is one two three four five six seven eight and then the secret key is the one that is provided to us in the previous step so I'll copy this one and paste it here typically you don't want to expose your network and device credentials to the public like this but this is just a demo setup which I will be deleting anyway after I made this video so far we have successfully configured this esp32 thing and we are ready to write some Arduino codes to provide expected functionality so click the sketch Tab and you must know that the Arduino iot Cloud provides automatic sketch generation and every time you add change or remove a variable this sketch gets updated like here this bull LED switch variable can be accessed by your board and the cloud and you no longer have to declare it it is already declared inside this thing properties.h so let's explore the auto-generated code your setup function initializes the serial Port communication and a short delay to give chance for the serial Monitor and the port open then it has this call to indeed properties function which is defined in the things properties as well we'll explore this further later and then the Arduino Cloud that begin function this is to connect to the Arduino iot cloud and some function calls to obtain more information about the network and the cloud and print them to the serial monitor in the loop function we only have one code a call to the Arduino cloud.update function this is the one responsible for synchronizing data between your board and the cloud so for real-time data synchronization it is a good practice not to put delay function calls here unless it's necessary and finally this void unlead switch change function and this is some sort of a callback method that gets executed every time a new value is received from an iot Cloud this is where we are going to put our code that acts when the LED switch change now let's start adding some codes to it to make this work so I'll Define my LED pin and recall that it is connected to gpio pin 23 also I'll set its pin mode as output and then inside this on lead switch change function I'll ask this Cloud variable let's switch if it is switched on or we can simply remove this then I'll turn the LED at gpio 23 to high otherwise I'll turn it off and that's it now double check if you are pointing to the correct board in port number if yes just click this verify and upload button and this is going to take some time a lot slower as compared to the desktop version of the Arduino IDE again once the upload has started don't forget to press that boot Button on your esp32 Dev board so for us to control and monitor our device we need to build the dashboard under this dashboard tab click build dashboard then I'll name it my dashboard and then click on ADD and as you can see a list of several widgets to choose from widgets are the building blocks of your dashboard and can be used to either visualize data or interact with your board widgets are linked to a single Cloud variable and as that variable is updated so as the widget so I'll use this switch and switch widget is great for simply turning something on or off like an LED or a motor and a switch can be linked to a Boolean variable so I'll give it a name Led switch and then link this to my lead switch Cloud variable I want the on off labels to be shown and click done I can toggle this led switch by simply clicking it and now let's test this with our esp32 thing sometimes the response is almost instantaneous but sometimes you'll get a delay especially the first time you uploaded a code it could take few seconds before it responds now if you click on this arrange widget button you'll be presented with a slightly different UI with a grid this allows you to reposition and resize your widgets accordingly also you can provide two layouts not just for web but also for your mobile app here you can have a totally different arrangement of widgets that is different from that of the web layout so I'll test it and it still works Now to control our motor via iot Cloud I'll repeat the same process click on the Things tab select this my first esp32 thing and aside from this led switch variable I'll add another one I'll call this motor switch and then for its type though I can use the same Boolean type but for the sake of exploring other variable types I'll use this Cloud switch which is basically a Boolean type underneath so I leave this as read and write and this update policy don't change now we no longer need to configure the associated device as well as the network and we can go back to our dashboard and select this my dashboard currently we are on View mode so to add a new widget I'll change this to edit mode again I'll add a new Switch and give it a name motor switch and then link this to a cloud variable so here we have two options but I want the switch to control the motor switch and click done then I'll arrange these widgets of course we can attest it yet because we have not yet uploaded our program so I'll go back to the Things tab my first esp32 thing and as you can see in this sketch tab it has three updates I'll click on it and we have a newly created motor switch variable in the things properties.h so I'll Define a motor pin at gpio22 and set its mode as output similarly I'll check if this motor switch variable has been changed if it is switched on then send a logic High signal to the motor pin gpio to turn on the motor otherwise send a low signal to turn off the motor okay let's upload the code now I'll go to my dashboard and I'll give the Arduino Cloud Server some time to sync I'll reset my board just to make sure and wait for a few seconds to connect okay if I toggle this led switch the lead turns on and now if I toggle this motor switch the motor rotates I'll toggle this led switch again and it turns off and finally I'll toggle this motor switch again and the rotation stops now to complete this iot Cloud experiment let's go back to my first esp32 thing I'll be adding two more variables this time these variables are intended to act as input the first one is for temperature so I'll call it temp we can make use of this float data type or you can also use a readily available temperature type which could be in Celsius or in Fahrenheit now we have this new declaration Cloud temperature sensor temp that will be available in your sketch also note that the permission is fixed to read only and then click add variable to confirm I'll add another one for the humidity and then call it humi similarly I can use a basic type of load or I can search for a ready-made type for humidity and there is one relative humidity and the sample value is in terms of percentage this looks good so I'll set the permission to read only as well because the data will be coming from our dht11 sensor as an input signal okay so we now have four Cloud variables and I'll go back to my dashboard again and add a widget to display my temperature readings so we can use a gauge here and a sample value could be anywhere from 0 to 100 and this is a good way to visualize a temperature value so now you can change the name to anything descriptive here I'll simply put temperature now I'll link this to my temp Cloud variable also you must know that the dht11 sensor can only receive temperature readings between 0 to 50 degrees Celsius plus or minus 2 degrees accuracy and if you want a more accurate version of this use the dht22 version instead the dht22 sensor can measure temperature between negative 40 to positive 125 degree celsius plus or minus 0.5 degrees accuracy and so for our humidity we can use this percentage widget since humidity value is viewed as percentage I'll give it a name of humidity and then link this to the humi variable and we're done so now I'll just rearrange this a bit and then to make this work I'll go back to my first esp32 thing and click this sketch tab we now have these two additional variables humi and temp aside from this nothing has changed to the rest of the codes here so to receive data from our dht11 sensor we need to reference a library and to have a fully functioning web editor that is very similar to your Arduino desktop IDE click this open full editor and similar to the desktop version you can see all your sketches example libraries serial Monitor and so on now your project is not only consisting of a single dot Eno file it also has this readme file which is a good document to describe your project this is the associated thing properties that age as you can see this file contains most of the Declarations like your esp32 device login name also here is the secret file containing the SSID pass and device key were in it is being referenced here as well then here are the actual Declaration of your Cloud variables as mentioned in your main sketch and the method definition for your two read and write variables that get executed once change is detected and finally the init properties were in everything gets initialized so to use the dht-11 sensor go to the libraries and search for the HD sensor if it doesn't exist open the library manager and look for DHD sensor library for dhd11 the hd22 temp and humidity sensor now you can simply click include or you can select a specific version of this Library you want to include in your sketch I'll cut this to include files and move it here so it is more organized now to use this I'll Define the DHT sensor pin at gpio 13. and I also need to define the sensor type which is dht11 of course if you're using the hd22 you have to change this and finally I'll create an object for my DHT sensor and pass in two arguments the sensor pin and the sensor type in the setup function I'll set the pin mode of my sensor pin as input and finally I'll initialize and start the DHT sensor object So reading data from your dht11 sensor is very easy and you simply need to store them to this temp and human Cloud variables so to do this inside the loop function call the DHT send Source read humidity function as well as the read temperature function and that's it now to check the received data to our serial monitor I'll add this code I'll check whether the human data received is not a number as well as the temp variable if this is true I'll print a message failed to read from DHD sensor however if there is a successful numerical data receive I'll print the humidity value in percentage as well as the temperature value in degree Celsius by the way if you're looking for this degree symbol in your keyboard simply press alt and then type 0176 now it is also important to note that the dht11 sensor has a sampling frequency of 1 Hertz which means that it is capable of reading data after every one second so just to give time for the sensor to read I'll simply put a delay here of two seconds before it Loops back so it doesn't overlap with its next reading again make sure to select the correct board and Port before uploading otherwise you'll end up with this error message so I'll re-upload the code and once done give a few seconds for the board and the sensor to sync its data and going back to our dashboard we'll immediately see the real-time temperature and humidity through our chosen widgets and if we want to see the actual temperature and humidity readings go back to the Arduino editor and open the serial monitor here we see the additional info about our board network connection and some Cloud information also you see the humidity and temperature data being read about every two seconds but this is not exactly two seconds as there is an ongoing Arduino Cloud update being executed so let's go back to our dashboard and check the previous functionality once again I toggle this led switch and you'll notice the same latency of about two seconds before it responds same with this DC motor that's why it is highly recommended not to use delay function inside the void Loop if you want a real-time response use the meals instead whenever possible and now to practice your skills in Arduino iot Cloud try to implement a similar circuit with a slightly different dashboard you'll be adding another Cloud variable and widget to control your LED here instead of a switch widget I want you to use a dim light widget which you can also use to toggle on and off your LED however this widget allows you to adjust the brightness of your LED which means you'll be implementing pulse with modulation that you've learned from lesson 2 of this course as a hint instead of using a basic Boolean type Cloud variable use a dim light variable type instead please check the Arduino iot Cloud variable documentation on how to work with this type the link is provided in the description below you'll be needing this get brightness function and get switch function for this thing to work and the rest is the same as the previous demonstration toggling the switch turns the motor on or off also you can simply download the Arduino iot Cloud remote app via Play Store or App Store and here's how it looks from a mobile app foreign and here is some bonus clip to replace this three to six volt DC geared motor with an actual 220 volt AC electric fan I still need to use this 5 volt AC to DC power adapter to supply this 5 volt relay however this electric fan requires to be plugged into an AC outlet for it to work Safety First please note that this is not part of your challenge activity and if you are not very familiar in handling electrical devices like this please do not try this at home here I've cut one of the two wires of my electric fan and expose a small portion of the actual copper wire and when these two come in contact with each other so long as this plug is plugged into the AC outlet the fan should rotate now I'll replace this DC motor with this exposed electric fan wire and then plug it in when you do this make sure not to touch the output terminals of your relay as it could get you electrocuted so if I toggle this motor switch the electric fan turns on also notice the change in your temperature and humidity values and this same principle allows you to basically control the simple on and off of most of your electrical appliances again thank you very much for watching and I hope that you've learned something of value here and to support this Channel please click the like And subscribe button and don't forget to hit that Bell icon to get notifications for every new video I uploaded for this course see you again in the next lesson
Info
Channel: Education is Life
Views: 63,154
Rating: undefined out of 5
Keywords: IoT, ESP32, Arduino cloud, iot cloud, arduino iot cloud, dht11, dht22, dht 11, dht 22, temp, humidity, relay, spdt, dc motor, dc geared motor, electric fan, bulb, light, dimmed light, dimmer, dashboard, thing, cloud variable, create agent, arduino account
Id: kcMzBV3XW2E
Channel Id: undefined
Length: 37min 14sec (2234 seconds)
Published: Tue Oct 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.