ESP32 Tutorial 49 - Control DC Motor Over The internet using Adafruit IoT | SunFounder's ESP32 kit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to internet of thing tutorial from  Robojax in this video we are going to control   the speed direction and start and stop of a  DC motor with esp32 over Wii using MQTT from   a cloud provider in this case Ada fruit we're  going to control the speed and direction of the   motor and start or stop of the motor like this  so from the screen and as you can see we can   see on the serial monitor the speed direction  and we can change the direction of rotation we   can speed the [Music] top slow it down and  stop it or start it the latency is very low   it depends on your internet speed and also I  set it half a second let's get started with this ESP 32 starter kit from SunFounder  this is the best esp32 learning kit from   SunFounder it has this esp32 microcontroller  which has built in Wi-Fi and Bluetooth this   board can do everything Arduino Uno  can do or many other Arduinos can do   plus extra more features because we have  Wi-Fi and Bluetooth the possibilities are   endless you can get connected to the cloud  or do the control or read in information or   values via your mobile device or your  desktop or over the cloud from a far location it comes with a camera extension  kit where you can stream the video over   Wi-Fi either through the cloud or to your  network and also it comes with micro SD   card where you can save images on the micro  SD card or you can write from the device any   information log the information on the device  and read it back you can power the board using   this included 18650 lithium battery and it has  built-in charger where you can connect micro USB   and charge the battery the kit comes with 320  pieces of component that you can learn tons of projects in the previous lessons in this  course we learned how to use l293d motor   driver with ESP 32 and ESP 32 extension board  from SunFounder all these components are part   of the kit that comes with the learning kit  and now we are going to control the uh speed   of this motor by increasing or decreasing the  speed we are changing the direction of rotation   and start or stop uh using MQTT protocol MQTT  protocol is such that we are subscribing to a   topic that we create and in this case we are  creating a motor control topic and this uh   board using Wi-Fi will connect to the service  that is provided by Adafruit this is Adafruit   IO and we are uh subscribing and uh this motor can  be turned on and off or speed change on direction   we are using here as a publisher so we need two  elements one is publishing we are publishing uh   information about the motor in terms of direction  of rotation speed or start and stop and this goes   to a broker QT broker and then this motor can be  uh turned on and off based on the information that   has subscribed so the this uh MQTT the Ada fruit  service is free we can use it for Limited amount   of control or uh uh dashboard that we create  so you can test and learn it we have already   explained the motor the motor driver and how  to control I'm not going to explain that please   watch that the link will be below this video or  it will just appear somewhere on the screen and   we are just going directly showing you the wiring  and how to set up MQTT and the other fruit uh IO service understand how to turn on and off  the motor and also control the speed of a   motor we have to understand that this cannot  be connected directly to esp32 or any other   microcontroller like Arduino or r stm32 or any  other microcontroller for that we need a special   circuitry but before that let's see how we can  control a motor now we have a battery when we have   a motor one terminal of the battery is connected  to one terminal of the motor and the other wire   is connected to the other terminal and as a result  this motor will rotate and if we disconnect this   wire here at this point this is a switch so  we can connect it and the motor will rotate   and disconnected and the motor will turn off  this switch also can be moved to the other line   other wire the same way we can disconnect it the  motor will turn off and connect it and the motor   will turn on now if we pay attention here if for  example in this particular motor if the positive   terminal is connected to one of the wire that is  labeled as positive or negative doesn't matter   for example in this wire and the other side is  connected to the this particular motor is rotating   in counterclockwise Direction and this way now if  we switch the wire this positive which was in here   if you move it to this location and the negative  which was here move it there then the positive   is coming here the the other side is connected to  the negative and as a result the motor will rotate   in clockwise Direction before it was rotating in  counterclockwise now it is in clockwise Direction   so we learn that if we change the polarity of the  power supply to the motor the motor direction of   rotation in DC motor will change if we put some  kind of mechanical switch that can do this all   at once so we can have this motor controlled in  both Direction now let's see what happens from   positive we have this gray shows one switch which  has three Terminals and the other gray is also   another switch which has three terminals these  two switches are connected together so uh you   can move them uh together when you push it both  of these contacts will move so the positive is   now connected via this switch and and in here it's  connected to the negative and the positive of the   motor is connected via this contact and it goes  to the negative and as a result the motor is for   example uh rotating in counterclockwise Direction  now if we move this up now let's see this was   like this now it's moving it up the positive  before which was going to the negative terminal   now this negative is not connected from here it  gets connected to the positive and the negative   of the motor which before was to the positive  now it is connected to this terminal and it is   connected to the negative and as a result the  motor is rotating in clockwise Direction before   it was counterclockwise now clockwise so the other  principle that we just learned is if we have some   kind of switch that can connect or change the  polarity for us we can control the direction of   rotation of the motor how to control the DC motor  with Arduino board with esp32 any other board you   cnot connected directly like that because this  motor needs a lot of current and a microcontroller   cannot Supply that amount of current so we  need something between the microcontroller   we need something between the microcontroller or  esp32 and the motor which is called driver and   because this is an inductive load there is a lot  of winding usually for protection we put a diode   this is just for illustration most drivers  that we use will have their own diode built in we need this l293d motor driver l293d the d  means diode it has a diode it has four drivers   and each driver has two pins we 1 a for example  and then enable pin and then it has output two   input and one output here when we use l293d it  has because I just mentioned it has four drivers   one half bridge if we use one half of the driver  the motor can be rotated only in One Direction and   it has uh one input pin and enable pin so you  enable it and then from here you can send for   example pass modulation signal and the motor will  rotate in One Direction and other wi is connected   to the ground and if we use two half bridge then  it will become a full Bridge uh the other side   which was connected to the ground is now connected  to the same another driver to another driver and   then the positive will come and the negative  will go to other driver and both of these are   connected to the same enable so we enable them  together and as a result this motor will rotate   for example in counterclockwise Direction pay  attention that the lower driver is high and the   upper is low now if we set the lower to low and  the upper High because it was low now it's high   then the enable is high then the current will  change the direction and the motor which before   was running in counterclockwise Direction now  it will run in clockwise Direction and here is   a data sheet for l293 and there is X so it's D  we are using D version it it has quadruple half   H driver what is the meaning of half H each of  this is half Bridge driver two of this together   two half will become one bridge and two of this  will become another Bridge so we have two Bridge   uh in one package however just remember that  operating voltage is 4.5 to 36 volts and the   current per channel is 600 mA it means that from  this driver 600 mA will pass either this way or   that way so this is half Bridge another half  Bridge so they become full Bridge so we can   control one motor but if you connect one motor  to one half Bridge it will be controlled only in   One Direction not both in this case it shows  three motor but only one is controlled fully   clockwise and counterclockwise but these two  are just rotating in One Direction so with L1   l293d we can control only two Motors fully because  these two will become one full Bridge another full Bridge let's get introduced to MQTT MQTT stands  for massage queuing Telemetry transport uh why we   need it because it's lightweight and efficient  MQTT is bidirectional MQTT can be scaled to   millions of things and it uses reliable messaging  delivery system it is secure and it is persistent   over unreliable Network so first we have to  understand the elements of MQTT we always   need to have a broker and we have MQTT subscriber  we have MQTT TT publisher and the most important   thing in the broker is the topic uh for which  the messaging is being published or subscribed   now this is internet of thing from mt.org and we  have client broker and another client which is   subscriber in this case we have a temperature  sensor sends information to the broker and   the temperature is listed or queued on the cloud  and our subscriber can read or fish a particular   Topic in this case temperature the topic is  temperature and we read that temperature and   if we use a microcontroller such as Arduino  or esp32 this uh in this case we use it as a   publisher and we have MQTT Broker in this case  we are using Ada fruit IO and then we have MQTT   client uh this case a subscriber a lap uh mobile  device or a computer so the temperature from a   temperature sensor is Red by esp32 and because  this acts as a publisher we are publishing it   as a topic of temperature to the broker and it is  being list and queued there it just Waits until   the request comes or uh the device is connected  so we can read the temperature on the screen   that's what how it is used as a publisher now  let's see how we can use it as a subscriber we   have ADD fruit uh MQTT broker service and then  esp32 is used now here as a subscriber and we   have subscribed to a topic called LED and then  uh MQTT mqt client is publisher is publishing   on off uh to the MQTT broker so when they touch  or click on the icon on or off the information   is cued here for the LED topic remember this  is a topic and we can have millions of topic   so one topic is called LED and because we have  subscribed to that topic of led the information   comes either that topic says high or low or on  or off depending on that we interpret and read   the information and we turn on and off the LED  based on the uh action that is taking on the screen now let's click on Ada fruit IO we're  going to create an account click here on the sign in let's create an account here by C sign up so once you open an account log in now once you logged in you  can click on dashboard here   create a and new dashboard and let's call it motor control you can if you have multiple motors  motor control one or um whatever motor with   some word just type this is done let's  go on feeds I'm going to create a group   so we can have all the elements inside  the group and inside the feed click on   groups let's call it DC motor and then  create now this has been created it has   new feed click here and we are creating  three feeds speed and then um Direction and start so this is the DC motor we have created  three feeds and you see DC motor Direction DC   motor speed I could have typed just motor and  it will be motor. Direction motor. speed so   it's up to you now after creating this go back  to the dashboard and click on motor control uh   dashboard it's empty click on this icon and  then create new block for the motor speed we   need slider click on slider and then under the  motor select the speed because the slider is for   the speed click next we type here a text the text  appear on top of the slider let's call it speed   and then the value is zero to 100% that's correct  this 10 is the step let's set it to one so we can   set even uh with a resolution of 1% so 0 to 100%  And then here you can say the label like percent   this percent will appear 0% now create another one  new block and this time we create a toggle switch   and then go under the motor and then this will  be Direction and click next type here Direction and then on text and then on value on text  will be CW and then the value would be one   and then off would be CCW and the value would  be zero so pay attention to the value we want   one and zero inside our code so we can read  it and this is just a text that appears here   click and this is done now let's create  another block new and this time again   toggle switch and then go under the motor  start select the start next and then here start so we can say on and off and here on value  should be one and off value should be zero if   you don't put the value this on and off will be  passed it says uses on text F blank so make sure   to type one and zero now we have set all this  and it's ready we can get the key by clicking   here and the username and key which I'm going  to show you again so we need this inside the code when you use esp32 with Sounders esp32 camera  extension module like this it comes also with a   battery on the package and it comes a built-in  charger so you can connect it and charge it and   disconnect and later on you can use it with a lot  of power so you can power up your application very   easily now let me show you the wiring this is the  wiring diagram that I have followed and prepared   this um because this is very simple I have created  this blue is my negative on both side and then the   red is my positive and I've got a ground wire and  positive the positive has not been used but I've   connected the red to this side using this wire so  both side has 5 volts and I'm using external power   then black is connected to the negative line and  this is connected to external 5 volts because the   DC motor needs a lot of current and USB will be  overloaded so I will not suggest con connecting   it directly to 5 Vols of this module so I'm going  to remove these two wires first first this this   chip has a DOT here this dot is pin one 1 to8 and  then 9 to 16 now the pins are shown here pin one   pin 16 and pin 8 are connected to 5 volts now pin  one is connected using red wire to 5 volts to this   red line as I said this is my 5 volts which is  connected here and then pin 8 is connected to 5   volts and pin 16 is connected also to 5 Vols to  the red line that's done only one wire from pin   4 is connected to the ground so 1 2 3 4 pin 4 is  connected using this black wire to the ground then   the signal is connected pin 2 is connected to 13  pin 7 is connected to pin 14 and here that's 13   and that is pin 14 you can connect it directly in  here or if you don't use this extension board you   can use directly pin pen 13 and 14 which are  labeled here and I'm connecting pin 13 and 14   to pin two and pin 7even pin two is here and  pin 7even is the last second so this is done   and it is connected to here and then ground is  connected from this line using black wire to   the ground and I've connected it to 5 volts but  the 5 volt is not connected because I'm using   external power now the motor will be connected  this is my motor it has been connected to pin   three and the other side is connected to pin six  so one wire doesn't matter which one because if   you connect it in different ways the direction  of rotation changes so uh it's up to you which   your where you connect one wire is connected  to pin three and the other is connected to pen six like that and the wiring is completed now let me explain the code I have  written this code here the first line it says   Boolean debug false this is the value that if you  want to see the information on the serial monitor   and let me connect it the information that is  displayed in here is coming from the serial   Monitor and it will be printed if I open the  serial monitor this information if if it is set to   false it will not display if you want to display  it make it true at the moment the code that has   been uploaded is true so if you want to disable  this just change this to false p uh the m a a   motor 1 a motor 2 a so this is pin one and pin two  for the motor a I've def Find pin 13 and 14 this   is the same as the introduction to the motor pin  and then this is a frequency for pulses modulation   the resolution is 8 Bits And then we are using  Channel A and B of pulsive modulation of the esp32   these are just a text CW CCW so it is displayed  in here this CW the CCW is coming from here please   do not change this and then motor speed motor  Direction motor start or stop and duty cycle these   are variables that are being updated by the code  do not change them this is for Wi-Fi and then Wii   secure client connection and then we need Ada  fruit MQTT this ad fruit MQTT Library must be   installed if you have not installed it let me uh  right click and copy this text go to library right   click here and then paste and you will see add  fruit in qtt library which shows installed in your   case you will see the install button like that  just click and install it in my case it's already   installed and then this is for USS ID for Wi-Fi  and this is for the password make sure to respect   the case for example if the SSID is uh uppercase  and you type small B it will not work so make sure   to type it exactly as it is this is for the path  for uh Adafruit IO service and this is the port do   not change them this is the username and this is  the key which we get from Adafruit IO uh dashboard   and um you must copy and paste it as shown there  here let me go there go to Ada fruit IO service in   here you will see the key click and you will  see the key in here the username and you can   generate and change the key as you wish because I  don't uh I'm not worried if you see it for example   if I change this and then uh the code will not  work I have to update it with a new key right   click copy and then come back and paste it here  so that's very important this is for Wi-Fi client   and this is creating MQTT client we are creating  an an object of MQTT with all the client service   port all the information that are at the top of  the code to get connected to MQTT service this   is the certificate add a fruit root certificate  Authority that's the certificate and this will   expire by the time that you watch this video so  we have to update it because we just installed   the library let's get the latest certificate from  the code that is provided click on file example   you see we don't see other fruit all the other  examples let's just close this and then open it again now after installing the library this will  be updated so click file examples and now you   see we see a lot of other examples and under the  example from Custom Library you will see a lot of   ADA fruit one is Adafruit MQTT library and under  that you will see Adafruit secure esp32 click to   open this sketch and in here from line 50 it the  certificate is in here select it from here go down until let me increase the size so from this  bottom to and here so select it up to here   right click and then copy close this come to  the sketch that we are working on select up to   here and then right click and paste now now the  certificate have been updated and you will not   get error otherwise if this is expired you will  not be able to able to get connected now we are   subscribing creating an subscription uh object  I call it the first one I we need three object   motor speed motor Direction motor start and for  each we are using MQTT subscribe object instance   and then we pass MQTT uh Library as a reference  and then username which is Robojax for/ feed and   then motor. speed again Robojax this is username  feed motor. Direction motor. start these are the   values that you get from the uh at the fruit IO  service if I click here at the feet and then under   the motor uh group you see motor. Direction motor  speed motor. start so the value must m exactly as   it is otherwise you will not be able to read  it so we are creating these three object for   subscription inside the setup we are initializing  the ledc for the passive modulation channel   frequency and resolution Channel A and B because  we need two of them and then for pin one and pin   two of the motor we are ass attaching this channel  two channels this is initializing the C monitor   printing this text this is printing just the SS ID  on the screen after 1 second delay we initialize   the SS ID and password for the Wi-Fi to begin we  have one 2 seconds delay this y Loop checks if the   connection is not true or not happened then after  500 millisecond or half a second it prints one dot   continuously until the connection is established  then the loop will exit and this will print this   value and if I open the serial monitor the value  and uh and if I press the reset button you will   see that we are getting connecting and you can  see let me you can see in Wi-Fi connected this   is the text that is coming from here and then it  will print the Wi-Fi connected IP address let me   stop this Wi-Fi connected IP address and it will  print the IP so that text is coming from here and   this is initializing the certificate for the  secure connection and I've created these two   lines first line is registering the call back for  the feed I call it motor speed from this object   the same object I use this motor speed so motor  speed do uh set col back and here I've created   this function motor speed call back which is at  the bottom of the code and then we pass this uh   by reference to this MQTT subscription so this is  initializing that and then we have done the same   thing for motor direction as you can see that is  here and motor Direction call back which is at the   bottom of the code and then the third one is motor  start which is used for start and stop which is   I've created a function at the bottom of the code  with that name inside the loop we are running this   function to connect to MQTT with the credentials  and then every half a second or 500 millisecond   we check the MQTT uh pocket processing so we are  reading it and this function is run motor which   is at the bottom of the code which I'm going to  explain now motor speed call back is this function   which ends here we are reading the message that  is coming from librar and we are printing it on   the serial monitor when the information comes  and then we get that message and we store it   in a variable of type string and we call it is  in string this line is converting it to integer   because the speed of the motor is an integer  value so we are storing it in a variable called   motor speed this variable is global variable  at the top of the code in here motor speed and in this section we are checking the bound uh  to see if the value is greater than 100 or less   than zero we set it back to zero this prevents it  in case if you enter incorrect information on Ada   fruit IO page and after that we get the speed  using the map we map this this value which is   from 0 to 100 we are mapping it and converting it  to a value of 0 to 255 the map is a function from   uino so the result of the calculation which is a  value between 0 to 255 is stored in the variable   called Dy cycle this is global variable at the  top of the code so this will go to the motor and   now the same way motor direction we are getting  the value converting into integer this will be   zero or one and stored in this variable which is  defined at the top of the code which is a global   variable and then start stop is also a Boolean  or a variable of zero and one the same way we   get the value converted to string and then from  string we convert it to integer now when we run   through the loop run motor we check this if  start is equal one then we run all this code   else we just s send LC right to the channel  we send 0 To A and B for for both of those   and the motor will stop now if it is equal one  it means the motor should start this portion is   for debug if the debug is equal true it will  print the motor is running with the direction   CW or CCW with the motor of speed with percent  now let's see if the direction is equal one or   it's equal true then we use LC right channel  one we set the duty cycle and then for Chan   a and for B we set zero it will rotate in One  Direction else means the direction is CCW we   just change this pay attention for a we had  duty cycle here for a we have zero for B we   had zero now for B we have duty cycle we just  change that the position of the value and the   motor will rotate in the other direction and this  is MQTT MQTT function will uh start a connection   and it will try three times and using this while  R it tries to connect every 5 seconds and if it   is uh connect once twice three times every time  it decrements this value until it becomes equal   zero after three times if it is equal zero then  it stays while one which means stay here forever   and the code will stuck if the connection  happens then it will print MQTT connection which and here is a demonstration I've connected  this uh using external power because this needs   five volts and it needs a lot of current so  I've connected external 5 volts here and make   sure if you when you connect external 5 volts  make sure to connect the common to the common   and just Supply the positive and right now the  serial monitor is opened uh let me increase the   screen a little on this side so you can see  better all the values at the moment the value   is uh zero and when you run the this dots as  you can see it changes the direction but you   don't see anything and the speed because the  motor is stopped when I turn it on the motor   is started it shows the direction CCW and at  40% at the moment on the serial Monitor and   if I reduce it you can see it's 20 30% so at  the moment the value is uh step the step of is 10% and when I click on CCW or CW The Direction  Changed we can stop it from here we can uh from this gear you can click and  edit layout and then go to this speed   click and edit block this is  a block that we are going to control this under the motor speed click next and  as you can see here the step is 10 we can set it   to one save the block save layout and let's  start now we can go with a step of no still   not reflecting maybe we have to reboot restart  it yes right now we can go with a increment of one let me put this fan that comes with a kit this  will make a lot of noise but let's let's try it right now the current that  this draws at 5 volts 80 uh milliamp but if I change the  direction the wind will blow to this side 30% I'm going to connect the meter from the power  supply so we can see the current and here let   me set it to zero now the current is  zero let's see if I can show you as well no this is not good oh in here now now we can see it better because  I set it at a proper range let's go at the maximum as you can see it shows 9  870 milliamp but if I change the direction that's 830 93% let's go 100% 970 so 100% at 5 volts the voltage is exactly 5 volts
Info
Channel: Robojax
Views: 1,274
Rating: undefined out of 5
Keywords: robojax, sun, founder, sunfounder, course, tutorial, kit, learn, learning, step by step, esp32, arduino, iot, wifi, mqtt, start, stop, speed, direction, rotation, cw, ccw, motor, dc, rotate, protocol, adafruit
Id: OUgyPXNYg3g
Channel Id: undefined
Length: 40min 51sec (2451 seconds)
Published: Tue Feb 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.