When using the built-in analog to digital converter of the Arduino Uno Rev 3's Atmega328P with a reference voltage of 5V, what is the appropriate function to interpret the output? There is conflicting information on the Arduino website and in the datasheet for the Atmega328P (Arduino Uno Rev 3's chipset).
A lot of code examples on the internet indicate that the appropriate conversion factor is 5V/1023 (including the Arduino website: float voltage= sensorValue * (5.0 / 1023.0);
).
Per the Atmega328P's datasheet, it indicates the conversion factor is 5V/1024:
If Vin and Vref are 5V, then the ADC reading would need to be 1024 in order to have a true reading of 5V, which is not possible since 1023 is the max output of the ADC. The closest reading to 5V we can get from this equation is ~4.9951 (5V*1023/1024).
With the Arduino example's conversion factor of 5V/1023, we get 5V for an ADC output of 1023.
Additionally, since the information from both sources is in conflict, it got me thinking a little more: The ADC is inherently sampling, and I am not sure if the ADC rounds up/down to the nearest value, so perhaps the closest approximation of Vin is an equation like the following that splits the difference:
Vin = (ADC+(ADC+1))/2 * 1024/Vref
Using the above equation, an ADC reading of 0 with Vref=5V would give Vin = (0 + (0+1))/2 *1024/5V = ~0.00244
. This equation never gives a reading of 5V or 0V, but rather a value that splits the difference between samples.
Perhaps I am splitting hairs since the absolute accuracy of the ADC is "±2 LSB absolute accuracy", but I would still like to know which is the best equation to convert an output from the ADC into a voltage measurement.