# arduino aref

In the last video I used a trick to enhance the accuracy of the analog to digital converter in the Arduino. I thought this is worth a small video for all the guys which just want to solve this particular issue. Arduino have a built in 10, bit analog to digital converter, abbreviated ADC. They can be used to read analog values on pin a 0 to a 7. A DC’s need an exact reference voltage to be precise. Unfortunately, the Arduino s use the supply voltage as their reference, because supply voltage voltage can vary. The reading of the ADC also varies to show this. I supply an Arduino with a variable supply voltage and measure a fixed voltage of 3 volts. The measured value is displayed on the TFT screen. You see the supply voltage on the yellow multimeter and the measured voltage on the red one. I start with a supply voltage of exactly 5 volts as foreseen. The reading of the Arduino is correct. If I change the supply voltage to 4.8 volt, the Arduino measures a higher value, it is imprecise. The same applies if I change the supply voltage to 5.2 volt. It is quite clear that this is not acceptable if you need precise measurements. This is, for example, true. If you want to measure any sensors with analog outputs as the ampere meter in my last video, fortunately, a trick exists to correct this value. Arduino have a right, precise, built in voltage reference of 1.

1 volt. If we measure this voltage with our imprecise ADC, we get, of course, a wrong value. We saw this in our last test, but because we know that the internal reference is really 1 point 1 volt. We can use the following formula to calculate the real supply voltage. Vcc equals one point one times, one thousand 23 times one thousand divided by the measured result. We see VCC gets bigger when the measured result get smaller, and this is exactly what we need by the way. The factors 1023 and 1000 are because the ADC has 10 bits, which means 1024. Distinctive values can be measured, let’s check in reality. Again i supply 5 volts arduino measures also 5 volts supply voltage. If i go to 5 point 3 volts, the reading of the arduino is quite correct. Of course, a 10 bit ADC is not completely accurate. If i go down to 4.5 volts, the reading is still correct, even with 4 volts, it stays correct. Now let’s use the measured VCC to correct our initial measurement. The formula is: corrected: measurement equals VCC divided by 5, multiplied times the value measured let’s check this. In reality, I can change the supply voltage between 4 and 5 point 3 volt and the reading stays correct at 3 volts much better than the original. The original value changes between two point: eight volts and 3.7 volts, which is a measuring error of minus 72 plus 23. The code to measure supply voltage is enclosed in the comment it uses some bit panning and works on most of the Arduino.

# arduino aref Social

official.arduino
2019-10-02T19:32:52+0000

👋 Want to bring Tony Stark-like gesture control to your projects? Learn how with the BLE-enabled MKR WiFi 1010 and Nano 33 BLE Sense boards using the ArduinoBLE library.
official.arduino
2019-10-02T19:08:01+0000

Arduin-yo ho ho! A fairground favorite, the pirate ship is a fun way to explore the oscillation of a pendulum. How much fun, you ask? Access our Science Kit Physics Lab preview and see for yourself: http://bit.ly/2oC6L0Q

🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today: http://bit.ly/2MnQ7fr

Array

zener

Originally posted 2016-10-02 09:00:48.

(Visited 140 times, 1 visits today)

## Comment (48)

1. Christopher Malau says:

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…

1. Andreas Spiess says:

English is my third language, so I probably mixed the words. So, you are absolutely right and I hope, the video itself is clear.

2. Christopher Malau says:

Wow, third language! I’m currently studying German, it’s great

3. Andreas Spiess says:

But for sure not easy. Keep on!

2. mbaksa says:

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

1. Andreas Spiess says:

You are right

2. mbaksa says:

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.

3. Eric Kitchen says:

Great video! Also thanks for the code. Worked great!

1. Andreas Spiess says:

You’re welcome!

4. Fernando Marengo Rodriguez, Dr. Eng. says:

Thank you for this video, Andreas! Could you please explain me the formula
Vcc = 1.1*1023*1000/measured result ?
God bless you!

1. Andreas Spiess says:

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…

5. SashNone says:

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.
https://www.arduino.cc/en/Reference/AnalogReference
You made a huge job with register tweaking… but is it worth it?

1. Andreas Spiess says:

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 😉

6. Alexander Sandberg says:

HEEDS UP. All arduous have a small divination on the internal 1.1 voltage reference. If you want a perfect reference number, you can use this code here(see link below) to find your chips internal voltage ref ( needs to be done for all the chips you will use because it is not a fixed 1.1 on even the same batch of chips)

Edit: Could not link directly to it, so you need to scroll down and find the topic:
“Detecting low voltage”

Quote from nick gammon writer: “For example, I got 1.062 volts when I tried it”

Hope this helps someone in getting more precise measurements /alleckz 🙂

1. Andreas Spiess says:

Thanks for this link. It contains valuable information.

2. Alexander Sandberg says:

Andreas Spiess Nice that I could share!

I was just going to share directly to Gammons microcontroller section (http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123).

But then I saw this link on there, which is exactly what we want in this case 🙂

In reference to that I wrote earlier in the first post scroll down too the topic “Reference voltage”.

Have another quote that will help us. “In all cases Atmel advises putting a capacitor between AREF and ground to smooth out the voltage on that pin. The datasheet doesn’t seem to say, but probably 0.1 µF would do the job.”

I like you videos between! Keep it up

/alleckz 🙂

7. brandon says:

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?

1. Andreas Spiess says:

If it is 98-99% accurate it seems to be ok for me.

8. Beacon of Wierd says:

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? 🙂

1. Andreas Spiess says:

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.

9. Shanmuganathan Srirahulan Srirahulan says:

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?

1. Andreas Spiess says:

you get level shifters from 5 to 3.3 volts

10. Remigius Bachmann says:

Hello Mr. Spiess. i’am trying to build a wood moisture meter. I’m searching a method to measure the resistance in the Wood. The Values to measure reach from 10MOhm up to 100000MOhm. Some more information about the relation between the moisture and the resistance in the wood here: https://www.vtt.fi/inf/pdf/publications/2000/P420.pdf

1. Andreas Spiess says:

I never thought about such meters. So I cannot help. Maybe google know more.

11. Alphonse Synrem says:

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.

1. Andreas Spiess says:

You are welcome. I hope it will do the job for you.

12. Taran72 says:

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!

1. Andreas Spiess says:

You are welcome!

13. Javier Camacho says:

I used you code and I’m still getting wrong readings. The fluctuation is from 0.5v up to 1.5v

1. Andreas Spiess says:

Then something is really screwed up. I would first try to get stable values without the code.

2. Javier Camacho says:

@Andreas Spiess I’ll keep searching then. Thanks for your time

3. Javier Camacho says:

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

14. Erik van der Leer says:

Does the ESP8266 have an internal reference voltage which would enable it to apply this method to the analog in readings too?

1. Andreas Spiess says:

I never heard of something like in the Arduino.

15. Vivek Venkat Subramaniam says:

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?

1. Andreas Spiess says:

This is a video for an Arduino, not for an ESP8266.

16. Andrew Johnson says:

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.

17. tinkerBOY says:

Will this work with atmega32u4?

1. Andreas Spiess says:

I do not know

18. zedBel4ever says:

Thanks for the post. Is there any such kind of trick for MKR family mine is 1010? I have tested the analogRead(ADC_BATTERY) .

1. Andreas Spiess says:

I do not know. Maybe not. They use much different chips

2. zedBel4ever says:

@Andreas Spiess Thanks for the fast feedback. Yes the MKR 1010 is based on SAMD21 Cortex-M0+ 32bit chip.

3. Andreas Spiess says:

I know

19. Arthurk346 says:

your accent remindes me Toto Wolff (principal of mercedes f1 team). he is austrian

1. Andreas Spiess says:

I was already compared with Arnold Schwarzenegger 😉 He is also Austrian and our native languages are similar, so our accents.

20. sean allen says:

Hi! Does this work the same for the ESP32?

1. Andreas Spiess says:

No