reference-en icon indicating copy to clipboard operation
reference-en copied to clipboard

1024 used instead of correct 1023 in `analogRead` reference

Open StefanT83 opened this issue 6 years ago • 1 comments

Dear reader,

I believe there is an error (typo) in the online documentation of https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/ related to the number 1024 being used wrongly, instead of 1023 which is the correct one.

In my opinion the resolution of Arduino Uno is 5 Volt / 1023 = 4.8875855 mV per unit. This value will allow us to match the reading of 1023 into 5 Volt (not more, nor less).

Let's check the consequences of what is mentioned on the online site: Arduino "will map input voltages between 0 and the operating voltage(5V or 3.3V) into integer values between 0 and 1023. On an Arduino UNO, for example, this yields a resolution between readings of: 5 volts / 1024 units or, 0.0049 volts (4.9 mV) per unit. ". So if we do the maths, it means that if one gets a reading from the Arduino of 1023, if we multiply that by the resolution of 4.9 mV, we end up with 5.0127 Volt (i.e. more than 5 Volt). This contradicts the first statement.

Can we conclude there is a typo on Arduino's online documentation of arduinoread() function?

StefanT83 avatar Oct 23 '19 15:10 StefanT83

So if we do the maths, it means that if one gets a reading from the Arduino of 1023, if we multiply that by the resolution of 4.9 mV, we end up with 5.0127 Volt (i.e. more than 5 Volt). This contradicts the first statement.

This seems like a rounding error. If the resolution is 5/1024 and you multiply that by 1023, there is no way that will end up above 5.

It will end up slightly below 5, which can be unexpected. However, I this is a matter of interpreting the results. I haven't checked the datasheet, but I think that a reading of "0" might actually mean any voltage between 0 and 4.9mV, whereas 1023 means anything between 4995.1mV and 5V. So just doing "ADC reading * resolution", will get you the lower limit on the measured value, which is then obviously not 5V for 1023, but a bit lower.

The datasheet might offer some info to confirm how voltage map to analog readings exactly, I'm not 100% sure my above interpretation is exactly accurate.

Also, in practice the accuracy of an ADC is such that the measurement error is typically up to a few LSB's (i.e. a few times the 4.9mV resolution), so any offset of less than one LSB might not really matter much.

matthijskooijman avatar Oct 28 '19 13:10 matthijskooijman