ESP32 Tutorial 55 - How to measure 12V, 24V or 100V DC voltage | SunFounder's ESP32 IoT Learning kit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to a video tutorial from Robojax  in this video we are going to learn how to   use esp32 to measure any DC voltage as you can  see we are measuring DC voltage of 20 volts and   here it's shown I can reduce the voltage and  we can see it in here on the serial monitor   I'm increasing it now to 30 volts almost and  we can read it in here let's get started with this esp32 starter kit from Sounder 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   uino 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 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 uh there are many   instances that we need to measure DC voltage we  had a in a previous lesson we introduced how to   measure the voltage from potentiometer which  we connected it to 3.3 volts of this pin and   we are able to read the voltage and but sometimes  you need to measure higher voltages for that I've   have made this video so we can use two voltage  two resistors as voltage divider and we make   sure that one resistor voltage will be measured  and the rest the remainder of the voltage will   be dropped on the second resistor so this way we  can virtually measure any voltage but of course   you cannot go thousands of volts because there  are some insulation and even with um plastic   insulation there will be Sparks so you cannot go  beyond certain voltages for that uh please refer   to the Safety Code of the region or school or  organization that you living but we can go for   example 30 volts 24 volts for the voltages of  battery or something that you need to measure it   will be easy to use with these resistors with some  uh uh precautions so we are using two resistors   one resistor that I'm using is 10 kilohm these are  all 1% tolerance metal film resistors the blue one   so get uh 1% tolerance resistor and then the other  one will be changed accordingly so this way we can   measure the voltage and also we will reduce  the amount of current that is consumed by the   circuit so keep one resistor at 10 Kil and then  the rest we can change it for in order to find   what type what resistor you need for what voltage  I have created a function in the code where you   can you can calculate resistor just uncommon  this and run the code and this is R1 which is   10 Kil at the top and just enter your voltage for  example uh 24 volts and run the code once you run   the code automatically it will tell you uh What  uh kind of resistor you need for 24 volts just wait so this was the previous inform information  let me press a button here it says for our   calculating R2 when R1 is 10 kilohm the maximum in  input voltage is 24 volts you R2 should be 67 kilo   ohm this number is not critical so whatever uh  higher closest Value that you have you can use or   to be safe just use 82 or 72 kilm 82 kilm or maybe  93 or maybe 100 Kilo ohm it's uh very acceptable   if you use those values but regardless you can  have a proper closes value if you want to make   it uh once you're done just comment this out and  you don't need it and also in the code I have set   back feature so when you run it it constantly uh  if I set this for example to to true uh right now   it's uploading it will give you the input voltage  that we are reading and the resistor value uh so now to understand the circuit we are having  from the battery or source that we have we want   to measure the voltage of B1 vb1 and we are  using two resistor R2 and R1 so the voltage   comes and it is divided on two resistors for R1  we call it VR1 and for R2 will be VR2 and we are   measuring the voltage across R1 which is 10 kilo  ohm so ground and V and this is connected to our   microcontroller and this voltage to calculate  this voltage this is the formula we can drive   it very easily so vb1 is equal VN this VN 1 + r2/  r r 1 so this is the formula and from this formula   if you need to know R2 we can just dve it R2 is  equal vb1 minus VN over VN / R1 so you subtract   this and divide it you will get the amount of R2  for certain voltage for vb1 let's say you want to   find the voltage for the battery which is 200  volts or whatever maximum you you want to work   with 24 volt for Simplicity and for safety uh V VN  I've set it to 3.1 so we will not allow anything   above 3.1 to be safe and when we put this in the  formula the calculation will be very accurate and   because we are selecting R1 to be 10 kilo ohm  we have let's say a voltage of 100 volts VN is   3.1 uh so just plug in the values and you will  get the R2 R2 and it will work fine with your calculation when you use esp32 with SunFounder  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 I have connected a 10 K  resistor to a point where it is connected using   this wire to this blue line and this will be my  ground and then from this point I have a 100 Kil   resistor you can change it I will show you the  calculation to another Point our voltage will   be connected our source for example my source  voltage will be connected like this so this is   connect connected now I need to connect from the  point where the two resistor meet we will measure   this voltage it is connected to pin 35 and if  you are using just is p32 without this extension   board you can connect the wire here to 35 which  is labeled here and for the ground we must bring   a wire here from the second pin either or from  this second pin to this point so we can have ground uh now let me explain the code I've  defined here an integer constant I called it   R1 and I have defined another variable R2 of  type integer constant so this is 10 Kil this   is 100 Kil we can change it uh this should be  not changed I suggest we just play with this   and this is another constant VN pin where we  connect across the R1 to measure the voltage   in this case 35 this is a frequency for the puls  modulation the resolution is 12 bits because we   want to measure the higher resolution that is  offered by esp32 and we using Channel Zero this   vb1 and uh vb1 is the voltage uh that we are  measuring the battery voltage one and integer   Mill volt this is the input voltage in terms  of Mill volts of type integer do not change   them they are used by the code and inside the  setup we initialize the serial monitor with   115,200 this is initializing the ledc setup  with channel frequency and resolution that   that is coming from these three information  this calculate resistor is a function that   I've have written at the bottom of the code at  the bottom of the code this should not run only   unless you need to calculate for example certain  voltage you want to uh find out uh uh what kind   of resistor is needed for the amount of R2 so  this is calculating R2 just uncommon this and   it will calculate and printed for you inside the  loop this function which is at the bottom of the   code reads the voltage and these three lines  are just printing this text and actual vb1 and   the uh this symbol V and we have 500 millisecond  delay it will run continuously Now read voltage   we we when we run this uh read voltage it  comes to this function and the function and we use analog read Mill volts this will  directly read the mill volts and we pass the   VN pin so this will give us the voltage in terms  of Mill volt and then the back which is at the   top of the code if it is enabled then we print the  maximum voltage which is another function based on   these two resistors it will tell you what is the  maximum voltage so you can take precaution not   to damage your microcontroller so I'm going to  explain that and then it will print the voltage   the M volt so this is just printing it and then  the M volt is now is stored in the MV which was at   the top of the code and this line is calculating  the voltage uh this is exactly the so this is   exactly this calculation that you see here uh we  get the molt the input voltage this one divided by   by th000 makes it volt so this doesn't matter and  then we multiply so this is inside the parenthesis   and then we say 1 plus R2 over R1 this is R2  over R1 we just put float this is called casting   we are changing the type because R1 and R2 are um  integer and we want to get the result in terms of   float uh vb1 that's why so this is a calculation  and the vb1 is a variable at the top of the code   so this will update it now maximum voltage  is a function that does the same thing so for precaution so maximum voltage is a function  that does the same thing it just calculate   the maximum voltage and it will print it on the  serial monitor calculate resistor is a function   that when you uncomment it and from the setup it  will receive the value of R1 and it will receive   a float voltage value and it will print all these  text up to here and it does a calculation to give   you the R2 and R2 is calculated using this formula  and I've implemented in here so this is exactly   this and we will divide it by, and we will get  in terms of kilo and sorry we will divide it by,   and we'll get it in terms of Kil while one while  one means stay here and never go forward so it   will stuck forever and as long as we have  the power that's why uh when you run this function uh make sure to comment  it after that so you just need to   print once the information and then comment it out and if you need to use or work  with a voltage this is a voltage   you can do whatever you want it's a  float value just do your uh task in here now let's see how we can select the esp32  board we can click here under the select board   and type here if pp32 d as soon as you type Dev  you see Dev board you can select it and click   okay so the board have been selected now we have  to select the port the other way to select the   board is Click On Tools board esp32 and select  the esp32 dev module now we have to select the   port if I click here it shows two port and I don't  know which one belongs to my device sometimes you   will see you will not see the number properly  so the best way to be sure the right click on   the start menu go to the device manager and you  will see here the ports if I click on this Arrow   it will show me the ports one is USB serial  ch340 one the other is USB serial device and   here now it's connected if I disconnect this one  of them disappear the one that disappeared is my board so sex stays and it  the disappeared if I connect it so it is my comport ch340 now it is my  comport and I can select it or I can click   on tool port and here you will see it you can  select whichever you want ours is come eight now   we have successfully selected the board and the  port and this is very important it must be done first I've connected my power supply at this  point one wire is connected and here uh   on this side of this R1 resistor and this is  connected to the input of uh gpiu 35 to pin   35 and this is a 100 Kil resistor connected  to my main power supply so we have used use   this as a divider and from the middle we are  just reading the voltage this is at the moment   the voltage is that is coming from my power  supply here if I disconnect it you will see   that the voltage is now zero I'm connecting it  back this is the voltage and as you can see we   are reading the voltage here on the serial  monitor I'm increasing it here 10.24 or 10.23 now I increase it again and we  are reading the voltage on the serial monitor as you can see the voltage is fluctuating  but here the voltage is fluctuating and this   voltage is uh also fluctuating but if you if you  want a steady more steady voltage then we can do   the average just create a loop and measure  1020 measurement and then take the average   and display it that way the fluctuation  will be reduced and it will be very stable voltage now if we turn on the debugger  debagging here this line if I say true   and upload the code you will see that it shows  all the information including the resistor the   uh input voltage at this point because  this is a voltage divider and we see all   the values which will help you understand  better and may also maybe for the bagging   purpose here let me stop this so it shows the  actual voltage 2.33 and maximum voltage with   the current configuration of these resistors  we can measure 34.1 volt and the pin voltage   that is coming to this point is 232 MTS  or 2.03 volts at the moment uh from this   resistor but the total voltage is properly  displayed which is this voltage this is now fluctuating and if you need to know for example uh let me if  you need to know uh for certain voltage uh the   amount of resistor I have set this function here  inside the setup calculate resistor just uncommon   this and here we have the R1 we want to use 10  km resistor but we don't know the R2 and your   maximum voltage let's say you want to measure a  100 volts let's upload it it will calculate R2 for you let me clear the screen I'm now pressing  this reset button to get the value here   it says calculating R1 R2 when R1 is  1,000 10,000 ohm maximum input voltage   is 100 volts and R2 should be 312 kilm so  you don't need to have 322 Kil maybe 330   kiloohm uh resistor which is a standard  one you can use that and the calculation   will be accordingly adjusted and you will  get very accurate uh reading of Maximum of   100 volts so very useful once you're done  just comment this line out and it will not   be displayed and now as you can see we are  reading 22.9 and here 22.8 the discrepancy   of few 100 MTS is due to the 1% tolerance  of the resistor with 100 Kil that's still acceptable and here I've have replaced the  R2 with 330 K resistor and I've entered here   330,000 and if you can see input voltage is  30 volts and we are reading 30 volts let me   reduce the voltage for some random value 23.3  and here 23.1 or 23.0 we are reading 300 MTS   discrepancy for such large amount of resistor  with 1% tolerance that's acceptable let's go lower 13.6 Vols or 13.5 this is  13.3 so 300 Mt discrepancy still acceptable now if I reduce the voltage even with the minimum we are reading  1.6 so minimum voltage is 1.5 uh at the device   and this is due to the internal reference  voltage so we cannot go be below this and   this is a voltage is zero it shows 1.56 so  that is the reference internal reference   voltage and if I increase it as soon as  it reach reaches 1.6 after that we can read so 1.5 still we are not reading anything as soon as I increase it the reading started
Info
Channel: Robojax
Views: 2,730
Rating: undefined out of 5
Keywords: robojax, sun, founder, sunfounder, course, tutorial, kit, learn, learning, step by step, esp32, arduino, iot, measure, any, DC, voltage, 12V, 24V, 100V, 36V, 200V, using, resistor, calculate, find, how to
Id: znBiVlgV9JI
Channel Id: undefined
Length: 23min 49sec (1429 seconds)
Published: Wed Feb 21 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.