I know you posted this sometime ago, but I just wanted to thank you for doing so. Has significantly improved the accuracy of my Arduinos readings. Brilliant.
Very nice video! To increase the resolution, you can add up multiple samples, as there is some uncorrelated noise on the A/D and input signal. To get a more stable measurement you can for example add 4 values and divide by two to get an extra bit. Experiment a bit as there is enough sample time for a reading on a display... averaging cancels out noise.
Nice video. Just a tiny correction - in formula one should use 1024 instead of 1023. It's not the highest value that one should be multiplying, it's the number of values (resolution), and the number of 10-bit values is 1024. So the formula should be VCC=1.1*1024/measured result
Actually, I was wrong, and you were right! :) One person explained the issue with my "correction" - it should be 1023 because the maximum value is 1023, and we are basically calculating percentage, not resolution. One should divide by 1024 if one wants to find out resolution - if one wants to know size of the step ADC can resolve.
mbaksa No, you were right from the beginning. See this: forum.arduino.cc/index.php?topic=303189.0 and this: www.electro-tech-online.com/threads/why-adc-1024-is-correct-and-adc-1023-is-just-plain-wrong.132570/
The maximum 10 bit number 1111111111=1023. The math would tell you that the maximum voltage should be 1023. BUT (capitals), the ADC does something you are not expecting. It subtracts one division (LSB) from the reading. To get the full reading you have to use 1024.
The ADC of the Arduino is normally ratiometric, as in referenced to Vcc. You can switch to an internal fixed reference of about 1.1V instead. Most external ADCs have their own built-in fixed reference. Both have different use cases.2
Hi Andreas, My comment seems to have been edited, it is not my original text.? As you know the use of a ratio metric ADC means that the supply to the Arduino can vary but the ADC count should track the changes in the supply and give the correct conversion. BTW: I am no way being critical of your excellent tutorials. Regards Eric.
Hello Sir, brilliant yet simple way of correcting hardware issue with software. My two ways of correcting this issue were: 1) use a higher supply voltage (2 series Lipo) and a 7805 to power the sensitive projects or 2) use a 3.3v regulator to feed the Aref, which limits the input voltage on analog input pins. Now I have an option #3 (or #0)! Thank you !
+thevideoboy87 hello.. I'm having the same problem.. can i use a 12v supply and a 7805 to supply my acs712 current sensors? my reading is pretty bad.. can u give me a sample circuit?
+Renatzu Agura Hi. the ACS712 consume approx 10mA, which means a dissipation of approx 70mW in the 7805. There should be no issue, make sure to install filter caps on the in/out of the linear voltage regulator. It's always a good idea to put a "dummy" load on the 7805 output, like a small LED+resistor to improve ripple.
+Renatzu Agura Thevideoboy87 is right. I could add that you need short wires between the acs712 and the Arduino to avoid noise pick up.In addition, you also could check with a voltmeter if the "bad reading" comes from the acs712 (disconnect everything else exept the current test wires and the supply voltage and measure directly on the module) already or is somehow created by the Arduino. In addition it is always good practice to wire ground as a "star", not a "net".
your videos #9 and #10 were extremely helpful: i am putting together my first data logger and the bits of information are good. i will probably apply the code for the keyboard and the OLED to the next project. :) . thank you!
Try use high external voltage and a linear regulator to power arduino, then don't draw much current from the arduino itself - this works well for bigger applications which already use multiple voltages, also make sure to keep arduino away from magnetic fields such as those made by power supplies, motors, power wires and wireless transmitters.
Did you ever take out the kitchen trash, then get out a new bag, put it in the basket and it just wouldn't fit? So you take your hand and thrash it about against the sides until you give up. That's what my brain felt like with the ADC conversion until I went back and watched video #9 again. It was so simple. Put some air in the bag and squeeze the top while you put it in.
You saved me my last little bit of hair. (Not much left to pull out...lol). The precision you have described here is just what I was looking for. Thanks!!!
Dear sir, again a very good work on reading very precise voltage with fluctuating usb voltages. I had this problem as during testing my uno from the supply of my pc usb, the voltage fluctuats around 4.95 to 5.1 volts according to the pc cpu load etc. After uploading the sketch and when it's built outside the readings are all off. So again to plug to the pc and rewrite the values as per the external 7805 regulator. This video lesson will really help me. I will give it a try. Thank you so much again.
If the Arduino comes with an accurate, built-in voltage reference, you'd think it would use that voltage reference by default (possibly combined with a voltage divider placed in front of the ADC).
@@AndreasSpiess Yes, I know, I've watched the video. I'm only wondering why that isn't the default. There are several things about these ADCs, that have me scratching my head, especially the one in the ESP32. It's a shame such a great little device is let down by such a poor ADC. It would probably cost a few pence (cents/pennies) to get that fixed.
Tried this on a LG ( LightGreen 328p-LQFP48 ) Nano 'equivalent' but this doesn't seem to work. The VCC function returns -1.00 and with the calculations it will end up as 0.00. Is there a chance to make it work with these type of 'clone' MCU's?
Great video, thanks Andreas. In the video the equation is presented as 1.1*1023*1000 but in the code on github it is actually 1.1*1024*1000. Which is correct ? I assume 1023, since the ADC reading will max out at 1023, and dividing that by 1024 will not give exactly 1.1V
Hello Andreas. Nice trick ! Would this also work (or even be needed) on the ESP32 (WROOM) ? Need to measure up to 85 volts and I'm wondering if I would use the build in ADC or an additional AD1115 module ( both with resitor divider). AD1115 is higher resolution ( 15b in place of 12b ) and has a build in voltage reference, but 0.02 Volt resolution is good enough
Does the converted analog to digital value of the internal voltage reference of 1.1V, the digital value, change all the time with variations in Vin and Vcc? It would be interesting to see that digital value with changes in input voltage, if I understand it correctly.
The term was "bit banging", it says "bit panning" on CC. So bit banging is using software to do the job of hardware. That I could look up. Todos los dias apprendo algo nuevo.
Very good video, but I just have to point out that this is making it more accurate not more precise. The number of decimal places is the same each time, so the precision is constant, but the measured value is not close to the actual value, so it is not accurate. I'm sure you know this already, just pointing this out to others out there...
Sir, recently i'm facing a problem when i connect ACS712 20a current sensor to my node mcu 8266 microcontroller.The reading value is not accurate at some points.then i searched on the internet and find out that the current sensor is 5v module while the esp8266 is only giving 3.3v. so how do i fix this?
Andreas thank you. You refer to some video "as seen on the previous AMPÈREMETER VIDEO", but you don't link it, and I can't find it, can I get a link please?
I did not hear this sentence. But I might be wrong. This is video #10. So there are only 9 which were produced before. And none was about current measurement
Thank you for the video. But please help: an Arduino has a 10bit system, so for my logic it can only represent 512 values. How the system can actually represent 1024???
@@AndreasSpiess my Q was why is this temperature reading wrong. You solved that for me. thanks again. So, I condensed the lines of code to make it smaller.
I have gone from 3 volts to 5 volts with 98 percent accuracy. As long as you tie one of the ground ports of the Arduino to whatever you are measuring, its 98-99 percent accurate. Am i wrong in doing this?
Thank you, a very useful and precise description, good job. Is there something special about the Leonardo in relation to excel ? or can you do the same with the Uno or Mega2560?
+Howard Bailey thank you for your comment! Yes, something is special. Only Leonardo, Due, and Pro Micro can act as an USB host. Only these boards know the command keyboard.print() which I used to transfer the data from the arduino via an emulated keyboard to Excel. If you want to do it with an Uno you could use a usb host shield for this purpose. But I never tried it. Just google arduino keyboard.print and you should find more infos.
+Andreas Spiess thanks. I had a look at your other vids (all good) and more on utube, I now realise the Atmega32U4, and some others, have the USB coms built in, i.e. 'Host' USB, I'm learning gradually. It's more difficult 'teaching myself' so videos like yours are a godsend, I look forward to your future posts.
@@AndreasSpiess wt about adding a capacitor ? since making wires short is not possible . if yes , can u tell me wt value of capacitor should be required ??
I do not remember, how I calculated this (it is a long time since then). It has to do that you measure a value which should be 1.1 volts (internal reference voltage), but in reality, is not, because you measuring is wrong. Maybe you can figure it out if you look at the other calculations in the code...
In case you or someone else still wonders: The ADC returns values 0-1023 proportional to the measured voltage with 1023 corresponding to VCC. Proportionality is to say that the ratio voltage/ADC-value is constant for all voltages in its range, specifically this holds for VCC and 1,1 V. We know that VCC is measured as 1023 for any VCC, and measuring our 1,1 reference voltage, the corresponding ratios must be equal: 1,1 / measured result = VCC / 1023 This of course also holds for voltages in milli-volts in which case our reference voltage becomes 1,1*1000 mV, and (after rearranging) we get to Andreas' formula. Looking through the code this conversion seems a bit redundant though, as he immediately converts back to volts in line 29 introducing the 'supply' variable (supply = readVCC()/1000.0;)
I don’t understand… you used the number 1023 in the formula you gave at 3:20 because 2^10=1024. These are not the same number. Why not use 1024 in the formula?
@@AndreasSpiess I watched the video. You're measuring the internal reference and using that deviation as an offset to fix the inaccurate readings. But if you just switched to using the internal reference as the active reference, it would just work properly anyway.
Trying to use an Arduino for a high speed, "high" accuracy application, and getting fluctuation on the first digit, is this fluctuation likely due to the supply voltage from the Arduino fluctuating or is it just inherent noise in the system? Like, would adding a capacitor to smooth out the supply voltage make it more accurate? :)
If the first bit is the one with the highest weight your setup is completely screwed up. If it is the one with the least weight this is what you have to expect. Any source, also the ones you describe can be the reason for that.
Maybe its too late after two years and you figure it youself already, but. if you write analogReference(INTERNAL); // it will limit analogRead to 1.1v maximum input with 1023 value. And you can use simple resistor voltage divider to limit any of your input voltage to 1.1v maximum. www.arduino.cc/en/Reference/AnalogReference You made a huge job with register tweaking... but is it worth it?
I think, it is worth it, because you use the internal connection and do not need a divider for that purpose. BTW: I did not do the work of tweaking. My contribution is just the communication ;-)
I know you posted this sometime ago, but I just wanted to thank you for doing so. Has significantly improved the accuracy of my Arduinos readings. Brilliant.
Very nice video! To increase the resolution, you can add up multiple samples, as there is some uncorrelated noise on the A/D and input signal. To get a more stable measurement you can for example add 4 values and divide by two to get an extra bit. Experiment a bit as there is enough sample time for a reading on a display... averaging cancels out noise.
The man with the Swiss accent and super high quality videos !!
Nice video. Just a tiny correction - in formula one should use 1024 instead of 1023. It's not the highest value that one should be multiplying, it's the number of values (resolution), and the number of 10-bit values is 1024. So the formula should be VCC=1.1*1024/measured result
You are right
Actually, I was wrong, and you were right! :) One person explained the issue with my "correction" - it should be 1023 because the maximum value is 1023, and we are basically calculating percentage, not resolution. One should divide by 1024 if one wants to find out resolution - if one wants to know size of the step ADC can resolve.
mbaksa No, you were right from the beginning. See this: forum.arduino.cc/index.php?topic=303189.0 and this: www.electro-tech-online.com/threads/why-adc-1024-is-correct-and-adc-1023-is-just-plain-wrong.132570/
The maximum 10 bit number 1111111111=1023. The math would tell you that the maximum voltage should be 1023. BUT (capitals), the ADC does something you are not expecting. It subtracts one division (LSB) from the reading. To get the full reading you have to use 1024.
Thank you so much! I'm using a soil moisture capacitive sensor. With your code, my calibration Has significantly improved.
Glad it helped!
The ADC of the Arduino is normally ratiometric, as in referenced to Vcc. You can switch to an internal fixed reference of about 1.1V instead. Most external ADCs have their own built-in fixed reference. Both have different use cases.2
The point of this video is to use the internal reference instead of VCC, I think.
Hi Andreas, My comment seems to have been edited, it is not my original text.?
As you know the use of a ratio metric ADC means that the supply to the Arduino can vary but the ADC count should track the changes in the supply and give the correct conversion.
BTW: I am no way being critical of your excellent tutorials.
Regards Eric.
Hello Sir, brilliant yet simple way of correcting hardware issue with software. My two ways of correcting this issue were: 1) use a higher supply voltage (2 series Lipo) and a 7805 to power the sensitive projects or 2) use a 3.3v regulator to feed the Aref, which limits the input voltage on analog input pins. Now I have an option #3 (or #0)! Thank you !
Thanks! I am not the inventor, I just liked the concept when I saw it.
+thevideoboy87 hello.. I'm having the same problem.. can i use a 12v supply and a 7805 to supply my acs712 current sensors? my reading is pretty bad.. can u give me a sample circuit?
+Renatzu Agura Hi. the ACS712 consume approx 10mA, which means a dissipation of approx 70mW in the 7805. There should be no issue, make sure to install filter caps on the in/out of the linear voltage regulator. It's always a good idea to put a "dummy" load on the 7805 output, like a small LED+resistor to improve ripple.
+Renatzu Agura Thevideoboy87 is right. I could add that you need short wires between the acs712 and the Arduino to avoid noise pick up.In addition, you also could check with a voltmeter if the "bad reading" comes from the acs712 (disconnect everything else exept the current test wires and the supply voltage and measure directly on the module) already or is somehow created by the Arduino.
In addition it is always good practice to wire ground as a "star", not a "net".
Nice explanation. The problem is that 1,1v internal reference is not so precise. It actually varies between 1.0 and 1.2v
your videos #9 and #10 were extremely helpful: i am putting together my first data logger and the bits of information are good. i will probably apply the code for the keyboard and the OLED to the next project. :) . thank you!
You are welcome!
Try use high external voltage and a linear regulator to power arduino, then don't draw much current from the arduino itself - this works well for bigger applications which already use multiple voltages, also make sure to keep arduino away from magnetic fields such as those made by power supplies, motors, power wires and wireless transmitters.
Good idea!
Did you ever take out the kitchen trash, then get out a new bag, put it in the basket and it just wouldn't fit? So you take your hand and thrash it about against the sides until you give up. That's what my brain felt like with the ADC conversion until I went back and watched video #9 again. It was so simple. Put some air in the bag and squeeze the top while you put it in.
Exactly ;-)
You saved me my last little bit of hair. (Not much left to pull out...lol). The precision you have described here is just what I was looking for. Thanks!!!
You are welcome!
Dear sir, again a very good work on reading very precise voltage with fluctuating usb voltages. I had this problem as during testing my uno from the supply of my pc usb, the voltage fluctuats around 4.95 to 5.1 volts according to the pc cpu load etc. After uploading the sketch and when it's built outside the readings are all off. So again to plug to the pc and rewrite the values as per the external 7805 regulator. This video lesson will really help me. I will give it a try. Thank you so much again.
You are welcome. I hope it will do the job for you.
If the Arduino comes with an accurate, built-in voltage reference, you'd think it would use that voltage reference by default (possibly combined with a voltage divider placed in front of the ADC).
I do not understand. We use the built-in voltage reference for this project.
@@AndreasSpiess Yes, I know, I've watched the video. I'm only wondering why that isn't the default. There are several things about these ADCs, that have me scratching my head, especially the one in the ESP32. It's a shame such a great little device is let down by such a poor ADC. It would probably cost a few pence (cents/pennies) to get that fixed.
Tried this on a LG ( LightGreen 328p-LQFP48 ) Nano 'equivalent' but this doesn't seem to work. The VCC function returns -1.00 and with the calculations it will end up as 0.00. Is there a chance to make it work with these type of 'clone' MCU's?
I do not know. You have to read their datasheets and maybe adapt the code.
Great video, thanks Andreas.
In the video the equation is presented as 1.1*1023*1000 but in the code on github it is actually 1.1*1024*1000. Which is correct ? I assume 1023, since the ADC reading will max out at 1023, and dividing that by 1024 will not give exactly 1.1V
There are different opinions about it. I do not care...
Hello Andreas. Nice trick ! Would this also work (or even be needed) on the ESP32 (WROOM) ? Need to measure up to 85 volts and I'm wondering if I would use the build in ADC or an additional AD1115 module ( both with resitor divider). AD1115 is higher resolution ( 15b in place of 12b ) and has a build in voltage reference, but 0.02 Volt resolution is good enough
1. It does not work on the ESP32
2. I made a video about the ADCs inside the ESP32 and the AD1115
Does the converted analog to digital value of the internal voltage reference of 1.1V, the digital value, change all the time with variations in Vin and Vcc? It would be interesting to see that digital value with changes in input voltage, if I understand it correctly.
Yes, it changes as shown in the video.
The term was "bit banging", it says "bit panning" on CC. So bit banging is using software to do the job of hardware. That I could look up. Todos los dias apprendo algo nuevo.
Very good video, but I just have to point out that this is making it more accurate not more precise. The number of decimal places is the same each time, so the precision is constant, but the measured value is not close to the actual value, so it is not accurate.
I'm sure you know this already, just pointing this out to others out there...
English is my third language, so I probably mixed the words. So, you are absolutely right and I hope, the video itself is clear.
Wow, third language! I'm currently studying German, it's great
But for sure not easy. Keep on!
Do you have a similar trick for the PICO as it has a noisy ADC as well.
I do not use the Pico often :-(
Sir, recently i'm facing a problem when i connect ACS712 20a current sensor to my node mcu 8266 microcontroller.The reading value is not accurate at some points.then i searched on the internet and find out that the current sensor is 5v module while the esp8266 is only giving 3.3v. so how do i fix this?
you get level shifters from 5 to 3.3 volts
Andreas thank you.
You refer to some video "as seen on the previous AMPÈREMETER VIDEO", but you don't link it, and I can't find it, can I get a link please?
I did not hear this sentence. But I might be wrong. This is video #10. So there are only 9 which were produced before. And none was about current measurement
How about a tutorial on how to build that screen?
This is quite old technology. So no more relevant today...
Thx!! I was already wondering why my PV Energy meter was not correct! Grüße aus München :D
You are welcome!
Does the ESP8266 have an internal reference voltage which would enable it to apply this method to the analog in readings too?
I never heard of something like in the Arduino.
Thank you for the video. But please help: an Arduino has a 10bit system, so for my logic it can only represent 512 values. How the system can actually represent 1024???
In my world: 2^10=1024
@@AndreasSpiess servus Andreas. Had a brain fart. In the mean time I figured it out.greetings from California. Machs gut
i am using an stm32 12bit adc what formular should i use please ?
This is very Atmel328P specific. So it probably will not work on an STM chip
@@AndreasSpiess ok
Great video! Also thanks for the code. Worked great!
You're welcome!
Thanks a lot, Swiss man. Here's a one-liner using your help:
vIN = ((analogRead(voltageSensor_pin) * (readVcc() / 1000.0)) / 1024.0) / .2;
What was the question?
@@AndreasSpiess my Q was why is this temperature reading wrong. You solved that for me. thanks again. So, I condensed the lines of code to make it smaller.
I have gone from 3 volts to 5 volts with 98 percent accuracy. As long as you tie one of the ground ports of the Arduino to whatever you are measuring, its 98-99 percent accurate. Am i wrong in doing this?
If it is 98-99% accurate it seems to be ok for me.
Thanks for the post. Is there any such kind of trick for MKR family mine is 1010? I have tested the analogRead(ADC_BATTERY) .
I do not know. Maybe not. They use much different chips
@@AndreasSpiess Thanks for the fast feedback. Yes the MKR 1010 is based on SAMD21 Cortex-M0+ 32bit chip.
I know
Thank you, a very useful and precise description, good job. Is there something special about the Leonardo in relation to excel ? or can you do the same with the Uno or Mega2560?
+Howard Bailey thank you for your comment! Yes, something is special. Only Leonardo, Due, and Pro Micro can act as an USB host. Only these boards know the command keyboard.print() which I used to transfer the data from the arduino via an emulated keyboard to Excel. If you want to do it with an Uno you could use a usb host shield for this purpose. But I never tried it.
Just google arduino keyboard.print and you should find more infos.
+Andreas Spiess thanks. I had a look at your other vids (all good) and more on utube, I now realise the Atmega32U4, and some others, have the USB coms built in, i.e. 'Host' USB, I'm learning gradually. It's more difficult 'teaching myself' so videos like yours are a godsend, I look forward to your future posts.
+Howard Bailey I learned it the same way you do now. This is the reason for my videos. I want to give back what I got...
your accent remindes me Toto Wolff (principal of mercedes f1 team). he is austrian
I was already compared with Arnold Schwarzenegger ;-) He is also Austrian and our native languages are similar, so our accents.
hi .. is there any way to reduce analogue value fluctuation while reading from analogue pin
This is a complex topic and I cannot give you a short answer. The simplest thin is: Keep the wires short.
@@AndreasSpiess wt about adding a capacitor ? since making wires short is not possible . if yes , can u tell me wt value of capacitor should be required ??
100u and 100n at the supply input.@@alanvargheseninan1237
Thank you for this video, Andreas! Could you please explain me the formula
Vcc = 1.1*1023*1000/measured result ?
God bless you!
I do not remember, how I calculated this (it is a long time since then). It has to do that you measure a value which should be 1.1 volts (internal reference voltage), but in reality, is not, because you measuring is wrong. Maybe you can figure it out if you look at the other calculations in the code...
In case you or someone else still wonders:
The ADC returns values 0-1023 proportional to the measured voltage with 1023 corresponding to VCC.
Proportionality is to say that the ratio voltage/ADC-value is constant for all voltages in its range, specifically this holds for VCC and 1,1 V. We know that VCC is measured as 1023 for any VCC, and measuring our 1,1 reference voltage, the corresponding ratios must be equal:
1,1 / measured result = VCC / 1023
This of course also holds for voltages in milli-volts in which case our reference voltage becomes 1,1*1000 mV, and (after rearranging) we get to Andreas' formula. Looking through the code this conversion seems a bit redundant though, as he immediately converts back to volts in line 29 introducing the 'supply' variable (supply = readVCC()/1000.0;)
VERY helpful!! thank you!!
You are welcome!
what is the sampling rate of adc
I do not know. Maybe you find it in the datasheet
That was a great tip thank you.
You are welcome!
I don’t understand… you used the number 1023 in the formula you gave at 3:20 because 2^10=1024. These are not the same number. Why not use 1024 in the formula?
Because there are only 1023 "distances" between values. But I am not sure which one is correct :-(
@@AndreasSpiess Gotcha
Hi! Does this work the same for the ESP32?
No
Why not just switch to the internal reference?
This is what this sketch does.
@@AndreasSpiess I watched the video. You're measuring the internal reference and using that deviation as an offset to fix the inaccurate readings. But if you just switched to using the internal reference as the active reference, it would just work properly anyway.
Will this work with atmega32u4?
I do not know
Can i use this for esp32
No.
thank you very much Sir
You are welcome!
Hi, Mr Spiess, I'm trying to use the ADC if the nodemcu, which accepts only a maximum of 1V input, so how would this formula be affected?
This is a video for an Arduino, not for an ESP8266.
I used you code and I'm still getting wrong readings. The fluctuation is from 0.5v up to 1.5v
Then something is really screwed up. I would first try to get stable values without the code.
@@AndreasSpiess I'll keep searching then. Thanks for your time
Andreas Spiess i think i found the problem. I need to use a voltage divider to lower my input V to match the internal reference
Thanks a lot! very usefull
You are welcome!
You are welcome
Trying to use an Arduino for a high speed, "high" accuracy application, and getting fluctuation on the first digit, is this fluctuation likely due to the supply voltage from the Arduino fluctuating or is it just inherent noise in the system? Like, would adding a capacitor to smooth out the supply voltage make it more accurate? :)
If the first bit is the one with the highest weight your setup is completely screwed up. If it is the one with the least weight this is what you have to expect. Any source, also the ones you describe can be the reason for that.
Maybe its too late after two years and you figure it youself already, but. if you write
analogReference(INTERNAL); // it will limit analogRead to 1.1v maximum input with 1023 value.
And you can use simple resistor voltage divider to limit any of your input voltage to 1.1v maximum.
www.arduino.cc/en/Reference/AnalogReference
You made a huge job with register tweaking... but is it worth it?
I think, it is worth it, because you use the internal connection and do not need a divider for that purpose.
BTW: I did not do the work of tweaking. My contribution is just the communication ;-)