Measure DC Voltage and Current with Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we'll be measuring dc voltage and current using an arduino we'll go over the principles of performing these measurements and we'll look at some sensors that can make the job easier then we'll put our knowledge to the test with some practical demonstrations we're taking the direct approach today so welcome to the workshop well hello and welcome to the workshop and welcome to the first of two videos on the measurement of voltage and current using a microcontroller and in today's video we are going to be working with direct current or dc now there are many reasons why you might want to measure dc voltage or current using a microcontroller you might want to build an instrumentation panel or perhaps a monitoring circuit so you can measure voltage levels or current consumption over a period of time maybe you have an automotive application in mind or perhaps you have a device that produces dc voltage such as a solar cell whose output you want to measure you could also use these techniques in order to measure the charging and discharging rates of a battery such as a lipo and some of these batteries require very specific voltage levels whatever the case you're certain to find an answer here today now i'm going to be using an arduino uno for our experiments but you could use any other microcontroller in fact some microcontrollers such as the esp32 or c duino xiao may indeed be a better choice because they have higher resolution analog to digital converters and that is going to come into play in several of our experiments but whatever microcontroller you use the techniques are essentially the same so let's start off by learning how we can use a microcontroller to measure dc voltage in order to measure dc voltage with the microcontroller we make use of an analog to digital converter or adc this is a device that has an analog input and provides a digital representation of that input on its output the number of output bits on the analog to digital converter determines its resolution the adc uses a reference voltage and that determines what the maximum input voltage level will be the arduino uno that we'll be using in our experiments has six 10 bit analog to digital converters it also accommodates an external voltage reference on its a ref pin the arduino mega 2560 has 16 10 bit analog to digital converters the esp32 has 18 12-bit analog to digital converters the esp8266 has one 10-bit adc the arduino nano avr series and the pro mini each have eight 10-bit adcs the cduino xiao has 11 12-bit analog-to-digital converters the arduino analog read command is used to get the adc value the voltage reference used determines the value per increment arduino's analog reference command determines the voltage reference used the default value for the analog reference uses the arduino's vcc on our arduino uno that will be 5 volts when the analog reference specifies the external value it uses the voltage on the aref pin this cannot exceed the vcc some arduino processors also have additional internal references you can see the article accompanying this video for a link to determine what the internal references for your processor would be in order to increase the voltage range that we can measure with an adc you can place a resistive array in front of it this also has the advantage of increasing the input impedance of the adc so that it will not load down the circuit that it is measuring you can also buy a prefabricated resistive array and this is what we'll be using in our experiments so as we saw in the presentation you'll need to use a resistive voltage divider in front of your analog to digital converter both to create a higher impedance input and to also reduce the voltage so that you can measure voltages higher than what the a to d converter is capable of now you can make a voltage divider out of two precision resistors or you can just pick up one of these little modules and these are very inexpensive and all they basically are are a 7.5 k and a 30k precision resistor mounted on a module but they're very easy to use you've got the inputs over here we've got ground on one side and bcc on the other side and over here is the output to your a to d converter the s lead over here and the negative lead on the other side is the ground lead the plus lead on this is actually kind of a dummy connection it really doesn't connect anything so for the next experiment you can use one of these modules or you could use your own resistors it's really your choice but i'm going to use the module for our first dc voltage measurement experiment we're going to require an arduino uno a resistive array module like the one i showed you earlier now you could use two discrete resistors if you don't have one of these but i would recommend the module as it makes it much easier to hook everything up you'll also need a source of dc voltage no higher than 25 volts and this is the voltage that we are going to measure you could use batteries or you could use a power supply we'll begin by connecting the s pin or signal pin on the module to the a0 input on the arduino uno if you're using a couple of discrete resistors you will connect the junction of the two resistors to this point we'll connect the minus output of the module to the ground connection on the arduino if you're using discrete resistors this will be one side of your resistor array we'll connect our dc voltage to the vcc and ground connections on the module observing polarity if you're using discrete resistors the vcc is connected to the other side of your resistor array and the ground connection is connected through to the arduino's ground and now let's take a look at a sketch we can use to measure dc voltage with this arrangement and so here's the sketch that we're going to be using to measure dc voltage with the arduino's built-in analog to digital converter and it's a fairly simple sketch we start off by defining the analog input pin that we're using and i'm using pin a0 of course you could move to one of the other pins and just change that value if you wish then we define a couple of floats and they both represent voltages the first one adc voltage is the voltage that we have at the analog input pin and the second voltage in voltage is the voltage that we have in front of the resistive divider and that's the actual voltage that we're trying to measure and then we have a couple of floats that define the resistors inside that divider now we have a 30k and a 7.5 k resistor and these figures are in ohms and again because they're floats they have a decimal point after them and then we define another float and this is our reference voltage now we're going to be using the internal reference voltage in the arduino and that will be 5 volts although it may not be exactly 5 volts it really depends upon your power supply and it also depends on what else the arduino is doing if you're driving something else that voltage could indeed be a little less than 5 volts but we're going to call it 5 volts then we have an integer that we define and that integer represents the value that we're getting on the analog to digital converter now the arduino has a 10 bit a to d converter so this value is going to fluctuate between 0 and 10 23. we'll initialize it with a value of zero then we're going to go into the setup and in the setup all we are doing is setting up a serial monitor so we'll do a serial begin and then we're going to print out to the monitor this dc voltage test so we know what we're doing now into the loop now the first thing we're going to do is we're going to get that adc value that one can that can go between 0 and 1023 and we'll do that by doing an analog read on the analog in pin that we defined earlier then we're going to determine the voltage that we're getting right at that adc input and that's pretty simple because we know what our reference voltage is and we know what the adc value is and so we'll multiply the two of them and divide it by 10 24 because it is a 10 bit a to d converter now notice we use the decimal point because these are floats and then we're going to calculate the voltage at the divider input this is actually the voltage we're trying to get in the first place and so we take that adc voltage and we divide it by the resistive array over here and you can see how we've done that over here in order to get the input voltage and then finally we're just going to print that to the serial monitor so we print input voltage equals and then we print the input voltage and notice that we have a two after here and what that will do is that will restrict the printout to two decimal points then we put a short delay in and we go back into the loop and do it again so it's a fairly simple sketch let's compile it load it up to the arduino and see how it works now here's our dc voltage demonstration and it's a pretty simple demonstration really here is my voltage source which is these uh 4aa cells and i've got the arduino of course and here's my little resistive divider now you could use discrete resistors if you wanted to because this really is just a 30k and a 7.5 k resistor so if you don't have a divider you could do it that way and as you can see i'm measuring about 5.35 5.32 volts let's go and measure this on the multimeter and see what that is i've got 5.41 volts over here so there is a bit of a difference but that difference can be attributed to the fact that i am using the arduino's internal power supply as my voltage reference and that reference is only as good as the power source which in this particular case is the usb connection to my computer so let's go and see if we can improve upon this now in order to improve the accuracy of our analog to digital converter we're going to be using an external voltage reference source and this is the one that i'm going to be using this is the lm4040 module from adafruit and it actually has two reference voltages on it it's got 2.048 volts and 4.096 volts now those may initially seem to be very oddball voltages until you look at them in millivolts and realize that they're 2048 and 4096 which is going to work perfectly with analog to digital converters now this can be supplied on the v in pin over here with any voltage from 3 to 6 volts so it's workable with both 3.3 and 5 volt logic now if you only provide 3.3 volts in you're not going to get the 4.096 volt output just the 2.048 volt output but these are not meant to power anything they're just simply references they have no current capabilities at all but they are extremely precision references and we're going to hook one up and see if it can improve the performance of our arduino voltage measurement for our second dc voltage measurement experiment we will start with the same circuit that we used for the first experiment we're going to add the lm4040 voltage reference module from adafruit we'll connect the ground of the module to the arduino's ground the 4.096 volt output will be connected to the a ref or analog reference pin on the arduino and the v in pin on the module will be connected to the arduino's 5 volt output and this completes the connections now let's take a look at the sketch we'll use to use the voltage reference with our dc voltage measurements and so here we have our new and hopefully improved circuit for measuring dc voltage and we've added the voltage reference onto it so here's the little adafruit voltage reference module and we've got the 4.096 volts fed into the aref pin on the arduino the rest of the circuit is the same with my weak batteries and the resistive divider connected to the analog input on the arduino now before we go and take our final voltage measurement i wanted to see how accurate my meter was because this voltage reference is supposed to be very accurate 0.1 percent so let's see if we can measure that 4.096 volts it's a little tough to get in here but there we go 4.094 4.095 that's fairly accurate for an inexpensive multimeter i'm actually rather impressed and so let's see what the voltage is with my weak batteries here and the meter is reading 4.5 excuse me 5.442 and if you look on the serial monitor we've got 5.4 5.44 5.42 so this is quite accurate so by adding the external voltage reference to the arduino we have definitely improved the accuracy of our voltage measurements so if you're looking for an accurate way to measure voltage with an arduino this is definitely the way to go so now that we've seen how we can measure dc voltage using a microcontroller let's focus our attention on the measurement of dc current now there are actually a couple of different methods that we can use in order to accomplish this so let's go and take a look at two of those dc current is measured in amperes or milliamperes or for that matter micro amperes or pico amperes when measuring dc current the measurement device is inserted in series with the load when performing a digital measurement of dc current the current is turned into voltage which is then sampled by an analog to digital converter there are two types of dc current measurement invasive and non-invasive with the invasive method a low value resistor is used and is inserted in series with the circuit the non-invasive method uses a hall effect sensor with the invasive method the voltage drop across the resistor is measured the non-invasive method uses the magnetic field created in the wire to measure the current because the invasive method is inserted in series with the circuit it can slightly affect the circuit's voltage as there is no electrical contact with the circuit the non-invasive method has no effect on circuit voltage due to its nature the invasive method requires a direct connection to the circuit as the non-invasive method measures the magnetic field around the wire it is possible to use an indirect connection although the sensors we'll use today will require a direct connection the invasive method is a simple passive design that has been used for well over a century the non-invasive method is a complex act of design that requires more components and requires power itself we will be using both methods today with the arduino now the acs-712 hall effect sensor is a non-intrusive method of measuring current it can actually be used with both dc and ac current but in our application today of course we are going to be using it for dc current so let's take a look at the acs-712 and then we'll see how we can use it with an arduino the acs-712 is a hall effect based linear current sensor it uses a low resistance 1.2 milliohm current conductor essentially a piece of wire this device works with both dc or ac current the acs-712 is powered by 5 volts dc there are three different models of this sensor the 5 amp 20 amp and 30 amp version when no current is detected the output of the device is 2.5 volts dc in order to calculate the current you need to know the scale factor of your device and this differs for the three different models as shown here here's the formula for calculating the current being measured the current in amperes is equal to the output voltage in millivolts minus 2500 as you recall 2.5 volts is the zero point this is divided by the scale factor to give the current so let's take a look at an acs-712 sensor and then we'll see how we can use it and so here's a peek at the acs-712 hall effect sensor module and again these are very popular and common modules and you can pretty well get them anywhere i picked this one up on amazon but you can also get them on ebay or at your local electronics store they're very common now they're also very easy to use you've got the input on this side in which you connect this in series with the circuit whose current you want to measure and you'll notice that the input doesn't have any labeling on it and that's because this module can be used with both ac or dc and therefore polarity is really not that much of an issue over here on the other side we have the output that's going to go to our a to d converter and it's just got a ground and a vcc and an output and in this case you do need to use the vcc because this tip needs to be powered but otherwise it's a very easy module to use and so let's go and see how we can use it with an arduino to measure dc current for our experiment with the acs-712 you'll require an arduino uno the acs-712 hall effect ac dc current sensor a dc source this can be something like a battery or a power supply and a dc load suitable for use with your dc source we'll begin by connecting the acs-712 ground to the arduino ground the acs-712 output is connected to arduino analog input a0 and the acs-712 vcc is connected to the arduino's 5 volt output the dc source negative connection is connected to the dc loads negative connection the dc source positive connection is connected to one of the terminals of the acs-712 and the dc load positive is connected to the other acs-712 terminal now let's take a look at the code that we'll be using to measure dc current with the acs-712 now before we run our sketch we need to know which model of acs-712 that we have because as you recall there are three different ones with three different current levels and the best way to do that is just to actually look at the markings on the tip on the module itself on the acs-712 chip and so i've got mine over here and if you take a look at the part number at the very end there you can see a dash 2 0 a that means i've got a 20 ampere version of the acs-712 so you're going to want to check yours before we run the next sketch and adjust the sketch accordingly now here's the sketch that we're going to use to demonstrate the operation of the acs-712 hall effect current sensor and if you recall how the current sensor works you shouldn't have any problem understanding the sketch now first of all we define a couple of variables v out is the voltage that we are measuring from the current sensor itself and current of course is the current that we are trying to measure then we need to put in the scale factor now as you recall the scale factor will differ depending upon which model of acs-712 you have as i have the 20 amp one i'm going to use this line over here next we have to deal with the analog to digital conversion now our analog to digital converter is going to be using this the basic built-in voltage the one that comes from the power supply so i'm setting my vref to 5 volts but if you wanted to modify this circuit you could use the external aref pin and put that voltage at the are voltage that you're putting in of course you'd have to modify the rest of the code over here in order to let the arduino know it's using the external one in a similar fashion the resolution of the converter is 1024 and that's because the arduino has a 10 bit analog to digital converter if you were using another microcontroller with this sketch let's say for example a c duino shower esp32 you could increase this value for more resolution again you'd have to modify the sketch to tell it to use the full resolution of the analog the digital converter now the res adc is how much voltage does one point of this conversion represent and so it's basically this divided by this over here and then the zero point now the zero point is the output of the sensor which represents zero volts and that's usually two and a half volts and so right now it's v ref divided in half and that is accurate now one thing i should point out is if you are going to modify this to use an external analog reference then this line is not correct because this is actually half of the supply voltage to the sensor not half of the reference voltage and so this would actually have to be modified or you could just put a value of 2.5 into there and that would be fine now in the setup all we do is we set up our serial monitor and then we go into the loop now in the loop we are going to read everything 1000 times and we do that to get an average to get some precision out of this and so here's the line in which we actually read the analog to digital converter and we put that in a loop that happens 1000 times and then we end up with a value for v out but we need it in millivolts it's going to be in volts up over here so we just simply divide it by 1000 and then if you recall the formula you saw in order to determine the current that's exactly what we have over here where we factor in the scale factor and then we go and print everything out and so we're going to print the v out and we're going to print it at a resolution of two decimal points and that's what the 2 is over here and then we're going to say that's in volts and then on this line we're going to print the current notice this at the beginning this just adds a tab character to make everything look a little prettier on a display and we print the current also down to two decimal points and that's an amperes we'll delay for a second then we'll go through the loop again so it's a relatively simple sketch if you understand the operation of the sensor let's load it up and see how it looks so here we have our acs-712 demo and here's my arduino of course here's the current sensor down over here and i'm using this six volt lantern battery and i'm using a light over here an incandescent light as my load and i've got my meter in series with everything as well and as you can see the meter says that we are pulling 0.427 of an amp here and it says we're pulling 0.44 of an amp here up on our serial monitor which is fairly close but not exact now if i pull this over here i can give no current going through it as the meter indicates but if you look at the serial monitor you'll see it actually says i have .01 of an amp here and that is probably because the zero point although it says 2.5 volts is probably not exactly 2.5 it's probably something like 2.504 or something like that and so that small inaccuracy is causing an inaccuracy in the current measurement now another thing i want to show you is i'm going to reverse the polarity over here of the current going through and now let's let it stabilize you'll see we're getting a negative measurement and the negative measurement is about negative point four two now if you factor in that point zero one or zero two amperes that we're offset by it is actually pretty accurate and as you can see my meter has dropped over here to 0.3 sorry to 0.436 so it's reasonably accurate i think the source of the inaccuracy is probably the fact that i'm not using a precision analog reference over here and that the supply voltage to this is likely not exactly 5.00 volts but all in all it's a good demonstration as to the operation of the acs-712 hall effect current sensor now the final sensor that we're going to be looking at today is quite unique when compared to the other ones we've seen this is a sensor module by adafruit and it uses the ina 219 chip now this is an intrusive sensor in that it measures dc current by measuring the voltage drop of a resistor inserted in series with the circuit so it does produce a little bit of resistance however this module can also simultaneously measure dc voltage as well as dc current and it measures things on the high side in other words on the positive side of the circuit as opposed to the negative side which is the way that you'll find a lot of those voltage and current measurement modules work and that has a real advantage in that it doesn't create any ground loops in your circuit another unique thing about this sensor is that its output doesn't use the analog to digital converter input instead this is an i2c device so as such it can be used not only with just about any microcontroller that supports i2c but as well as a number of microcomputers so let's go and take a look at the adafruit ina 219 sensor the adafruit ina-219 is an intrusive sensor that measures the voltage drop across a 0.1 ohm one percent precision resistor the device has an internal 12 bit analog to digital converter the ina 219 also measures dc voltage to a maximum of 26 volts dc it can handle a maximum current of 3.2 amperes this device can be powered by 3.3 or 5 volts dc the adafruit ina-219 is an i2c device with selectable addresses the current sensing resistor can be replaced to increase the current capability now let's take a look at an adafruit ina-219 so here's a look at the adafruit ina 219 module and it actually comes in two different formats which i have over here this one on this side is for the feather wing line of processors and this is just a standalone module and that's the one we're going to be using today but they'll both operate in the same fashion now you'll notice that it has input terminals over here that we're going to be connecting to our voltage and current source and i'll show you the wiring for that in a moment and on this module we've got the same thing i just haven't soldered them on over here and then on the output here we have the ground the bcc the connections for i2c because remember this is an i2c module and they've also duplicated the two pins over here at the bottom in case you just want one area to connect it to one thing to note is that both of them have these sets of solder pads over here and these solder pads are used if you want to change the i2c address so in case you have an address conflict or if you want to use more than one of these modules in your circuit you can do it that way so it's a very versatile little module so let's go and take a look how we would use this with our arduino for our dc current experiment we're going to require an arduino uno and an ina 219 dc current sensor module we'll also need a source of dc voltage this can be anything up to 26 volts and we'll need an appropriate load to drive with that voltage now this could be an led or a resistor or another load this makes certain it does not consume more than 3 amperes we'll begin by connecting the ina-219vcc to the arduino's 5-volt output the ground on the ina-219 will be connected to the arduino ground the ina-219scl is connected to arduino pin a5 the sda pin on the ina-219 is connected to arduino pin a4 we'll take the negative side of our dc source and connect it directly to the negative side of our dc load the positive side of our dc source will be connected to the v in plus side of the ina-219 the v-n minus side of the ina-219 will be connected to the positive side of our dc load and finally we'll connect the negative side of our dc source to the ground connection on the arduino and this completes the wiring now working with the adafruit ina 219 has been made quite simple because adafruit as usual has provided an excellent library for it so in order to use let's go into sketch and go into include libraries and manage libraries to bring up our library manager in our search we're going to type in adafruit ina 219 and there it is the library now i already have mine installed but if you don't have it installed which you probably won't there will be an install button over here that you can click and when you click it you'll also be prompted to install some dependent libraries go ahead and do that after you've installed the library we can simply run the example sketch that comes with it so go into an examples and then scroll down until you see adafruit ina 219 and do get current and here's the get current sketch and it's actually a fairly simple sketch because the library does all the heavy lifting we start off by defining an object called ina 219 and in the setup we set up our serial monitor note the baud rate that we're using it's a much higher baud rate than you might be used to so you'll have to set your serial down to that then we initialize the ina-219 over here and then in order to use it it's very very simple we've defined over here a couple of floats for different parameters that we're going to be measuring because we're going to measure more than just current and voltage we'll also be measuring power as well and then in order to get those values it's very simple because as you look there's a method for each one of these things so we don't have to do any calculations the library has done everything for us which is really neat and then we'll simply print all of that out to our serial monitor and go through it again after a two second delay and so the sketch has been made very simple because of the library let's load it up and take a look at the results now here's the demonstration that i've hooked up to show the operation of the adafruit ina 219 sensor and i'm using 12 volts from my workbench power supply to power this and for a load i've got these rgb leds they're still on the spool and these are 12 volt ones and i've got a dip switch arranged over here so they can turn on the individual elements by the way if you're ever using a dip switch on a breadboard i've got a handy hint for you put it in an ic socket first and then it'll stay a lot more secure in the breadboard than just by itself because by itself whenever you hit the switch you'll probably pull it off the breadboard just a handy little tip for you and here of course is my ina 219 sensor and my arduino so let's look at the serial monitor right now and you can see i'm getting a number of parameters what's interesting is how much my bus voltage falls whenever i put these leds on so let's put the red one on first and we can see the voltage is falling and we can also see i'm getting a current of 881.3 881.6 milliamperes just for the red let's turn off the red put on the green ones and they take a little less current than the red ones did i'll put the blue ones on right now and the blue ones also take a little less current than the red now let's try a couple i'll put them all on right now here's the maximum current i'm drawing notice how much my power supply voltage has fallen but i am pulling about 1.38 amperes right now so it is a sizable amount of current that i'm pulling and of course i can turn one of these leds off and drop the current by a little bit etc etc so this really is a very handy sensor in that it can measure both the voltage and the current at the same time and so this brings us to the conclusion of this first of two videos on measuring voltage and current using a microcontroller and as we discussed dc voltage and current today i think you can probably figure out that the next time we'll be working with ac now the best way to find out when that video is released is to be a subscriber to the youtube channel so if you haven't subscribed already please do i would very much appreciate that it's very easy to do all you have to do is click on the subscribe button and then go ahead and click on that little bell notification and as long as you have notifications enabled on your youtube you will be able to be notified every time i make a new video now if you'd like some more information about what we discussed today or perhaps you'd like to grab the sketches that i used you'll find an article that accompanies this video on the dronebotworkshop.com website and there's a link to that article right below the video while you're on the website please consider subscribing to my newsletter it's not a sales letter it's just my way of keeping in touch with you and letting you know what's going on here in the workshop and finally if you'd like to discuss this video in depth the dronebot workshop forum is the best place to do that we now have a section exclusively for discussing all of the videos so there will be a post just for this video where you can discuss the video with other users you can submit code samples or any problems you're having in measuring voltage or current using a microcontroller and people will be glad to help you out so until the next time we meet please take care of yourself please stay safe and i will see you again very soon here in the dronebot workshop goodbye for now [Music] you
Info
Channel: DroneBot Workshop
Views: 70,002
Rating: 4.969429 out of 5
Keywords:
Id: psNAeHoZv0A
Channel Id: undefined
Length: 37min 28sec (2248 seconds)
Published: Sat Jan 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.