Error in ReadAnalogVoltage example?

Isn't the code in the example ReadAnalogVoltage wrong for an Arduino UNO Rev3, given the information in the ATmega328P datasheet (24.7 ADC Conversion Result)? If I get it right the ADC value should be divided by 1024 instead of 1023. I'm aware that this is not that important as the accuracy is only ±2LSB. I'm also aware that an ADC value of 1023 would not map to V_ref in this case but this is expected as written in the documentation

0x000 represents analog ground, and 0x3FF represents the selected reference voltage minus one LSB.

Here we go again! Yes, there are 1024 possible values. They range from 0 to 1023. Count them, there are 1024 possible numbers in the set.

2 Likes

This gives a good explanation:

It seems that the ADC can return 1024 "slots" of values (from 0 to 1023) where each slot represents 1/1024 of the reference voltage.

So for a 5V reference voltage the slot width is:

5 / 1024 = 0.0048828125V

Thus a result of 0 could be: 0V to 0.00488V
A result of 1 could be: 0.00488V to 0.009766V
...
A result of 1023 could be: 4.99511V to 5V

... and eventually suggests

float voltage = ((float) rawADC  + 0.5 ) / 1024.0 * Vref;

...which names each of the 1024 0.0048828125V-wide intervals with the midpoint of the interval. The plain analogRead(pin)/1024.0 represents each interval by the lower edge of the interval. The /1023.0 scheme inconsistently represents the intervals by low, high, or somewhere in-between in order to make up for the lost interval.

Another nice thing about using the midpoints of the intervals is that it helps avoid divide-by-zero complications.

Hint, "0" is a real number, is a saying I had posted on my computer for many years. It depends on the base as to how it fits in. If you count base 10 0.1.2...15 marbles or hex 1.2.3...D,E,F 16 marbles each pile will be the same, the only difference is your number base. Most people live with bast 10. Spend some time with this link: Number Systems and Bases – BetterExplained

Correct, but may be other way around...

I also like this approach the best although it is not the formula proposed in the datasheet.
Is there a possibility to change the built in examples? I think the current example is misleading a lot of people.