Dividing by 1023 or 1024? The final verdict on analogRead

This is how I see this - may be we will get everyone to agree

Let's assume everything is perfect from a hardware perspective and no bias is introduced

  • you input a voltage [color=blue]V[/color] between GND and V[sub]cc[/sub]
  • analogRead() gives you a value of x

As documented in the datasheet, x represents any voltage between x * V[sub]cc[/sub] / 1024 and (x+1)* V[sub]cc[/sub] / 1024

That means that if you take [color=blue]V[sub]est[/sub][/color] = x * V[sub]cc[/sub] / 1024 as the measure, you are saying that you always take the lowest value of the interval as your estimated voltage read so you are always undershooting. It means that your max absolute error can be as big as V[sub]cc[/sub] / 1024 for every possible value of x. You have a constant distribution of the error

Now if you were to take the value divided by 1023: [color=blue]V[sub]est[/sub][/color] = x * V[sub]cc[/sub] / 1023 to represent a fair possible value, what does that mean?

There are two key questions:

#1/ Is this [color=blue]V[sub]est[/sub][/color] a fair possible original voltage value, ie are you within the right interval for every possible value of x?

#2/ what is the distribution of the max error for every possible value of x

for #1 it's easy to prove that for every possible value of x in [0, 1023]

x * V[sub]cc[/sub] / 1024 <= x * V[sub]cc[/sub] / 1023 <= (x+1)* V[sub]cc[/sub] / 1024

--> So [color=blue]V[sub]est[/sub][/color] = x * V[sub]cc[/sub] / 1023 is a possible value and thus mathematically as correct as any other value in the interval

for #2, it's best to see how [color=blue]V[sub]est[/sub][/color] moves across the possible interval depending on x


With this approach one sees (the blue arrow) that [color=blue]V[sub]est[/sub][/color] will vary between the min of the interval and the max when x varies between 0 and 1023

--> thus whilst it means that your max absolute error can be as big as V[sub]cc[/sub] / 1024 for x = 0 or x = 1023, for the other values of x, the max absolute error will be less and at x~511 you have divided by 2 the max absoute error compared to the other approach.

Conclusions:

  1. dividing by 1023 or 1024 gives in both cases a likely estimate of the original input value [color=blue]V[/color] and thus are both correct. everyone wins :slight_smile:

  2. dividing by 1023 statistically minimizes the maximum absolute error and thus it's fair then to assume that you get statistically a better approximation of your true original input value [color=blue]V[/color] if you use 1023

----> THIS IS WHY I THINK 1023 IS BETTER THAN 1024 (and you get a nice 5V for 1023)

That being said, the assumption we made initially that everything is perfect is never true...
error.png
so in practice you can do what you want, it does not matter much or if this level of precision is important for your project, then you probably need to invest in better hardware...