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...

J-M-L:
This is how I see this - may be we will get everyone to agree

I reckon we had got there in Reply #75 and my summary in Reply #79.

Here's to the next 30 replies :slight_smile:

...R

Robin2:
I reckon we had got there in Reply #75 and my summary in Reply #79.

Here's to the next 30 replies :slight_smile:

...R

Not exactly expressed the same way, right? was a different attempt

How long since anything NEW was added that isn't a new rehashing of what was posted before.

Yeah OP, lay the "final verdict" down on us like you would have it. Now, Build That Wall!

J-M-L:
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

The "statistical minimum" is obtained when the value is assumed to be in the middle of the bucket: VEST = (x + 0.5) * VREF / 1024

Yes that could be another formula :slight_smile:

The whole idea that people might have uses for hardware that don't completely match the hardware to the full set of not just significant digits but the insignificant digits as well simply never occurs to datasheet fetishists.

Robin2:
I agree with that, and we established that way back when.

And, assuming that is also your view I don't understand the purpose of your Reply #102

...R

Thread TL:DR?

J-M-L:

The figure is wrong because the datasheet says that 1023 is obtained for a value that is not Vref (Vcc in your figure) but Vref-Vref/1024, so the last segment, the one that has Vcc as upper limit, doesn't exist.

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

Same reason, the ADC range never reach Vcc if Vcc=Vref, Vref is ever outside ADC range.

Give the ADC 5V and it cannot measure all the way to 5V.

In some people's heads the world is perfect and only one answer can right.

In the real world I can read the same signal with the same ADC several times and average the yes-they-are different values to arrive at a closer to real value than one read is likely to give. It's called oversampling and it works because the real world is not as diagram-simple as people's notions.

The truest you can get on a single read is "center of the bucket" and to realize that and this has been noted here since YEARS ago that the error margin is more than a "bucket" wide.

In the real world we build to tolerances. That's what the band on the resistors is there to tell you. That's what tolerances in specs are there for. The measure and the cut are never perfect, only ever within acceptable range (what will be tolerated by the design, hence the term "tolerance") that since you CAN'T measure perfectly, you CAN'T tell if anything happens to be so.

In the real world, when someone takes a procedural convenience that is within tolerance, you have to get UNREAL to object.

zoomx:
The figure is wrong because the datasheet says that 1023 is obtained for a value that is not Vref (Vcc in your figure) but Vref-Vref/1024, so the last segment, the one that has Vcc as upper limit, doesn't exist.

The figure is correct - that’s the start point, the left side of the last (top) horizontal red line - what do you get if you feed 5V ? (Answer is 1023 too - and I think the pin will safely take up to 5.5V if the ref is 5V - returning 1023 as you saturated the ADC)

This way the last segment has not the same length of the others, for sure is longer, and you don't know how is long because you can get the same value, 1023, also for 5.5V, maybe more, maybe less.
In other words 1023 mean a value that is bigger than Vref-Vref/1024 (not Vref!) but you don't know how many is bigger.

zoomx:
This way the last segment has not the same length of the others, for sure is longer, and you don't know how is long because you can get the same value, 1023, also for 5.5V, maybe more, maybe less.
In other words 1023 mean a value that is bigger than Vref-Vref/1024 (not Vref!) but you don't know how many is bigger.

Yes... this does not mean the estimated voltage you get by dividing by 1023 or 1024 is not a possible value, right?

you could even argue that given it's not at the beginning of the interval, if your setup allows going beyond 5V (not recommended though) then you'll be closer to the truth by saying 5 versus (5 - 5/1024) and statistically in the case lower the max potential error.

but as said above as well it does not matter compared to all the other imperfections of the hardware and likely your Vref

The difference is not only near Vref. It changes also the angular coefficient of the straight line that you use as transfer function between the ADC values and the voltage values, if you use a linear fit. If you use a range of 0-5V you have an angular coefficient of 1/1023 but if you use a range of 0-Vref-Vref/1024 you get an angular coefficient of 1/1024
Also this values, using 10 bit, have very small difference. If bits are lesser (maybe 8, I didn't tested it) the difference is not negligible.
As I wrote before, if you have only 2 bits the range is only 0-4V ( if Vref is 5V) and the difference is a lot.

Your reasoning is "flawed" because there is no angle and there is no continuous transfer function from voltage to digital value

The math problem to solve is that you are looking at the reverse function of a surjective but non bijective piecewise affine function. (the steps)

You really lost information when the ADC converted the input Voltage into a digital representation, and the amount of information you lot is directly related to the number of bits (and hardware quality) of the representation and there is no way to get back to the original value.

All you can do is take an estimate, based on simple principle that it has to be a likely original value (ie within the interval). There is no good or bad estimate as long as they sit there in the interval. You can add extra personal constraints like "I want to see a nice 5V when the ADC tells me 1023" or "I want to minimize the max average error" or "I want a constant max error"... Those are arbitrary choices you can impose to yourself.

that means basically that any of the affine function crossing every "step" is legit to consider.

line.png

but it does not need to be an affine function either, you might want to compensate specific bias you have at specific steps so this would be a legit curve as well

altview.png

if you have 2 bits --> you have 4 possible values for the ADC. So it's exactly the same thing just with less precision... and in a perfect world, they would map this way:

[color=purple][sub]
0.00V to 1.25 V would map to 0
1.25V to 2.50V would map to 1
2.50V to 3.75V would map to 2
3.75V to 5.00V would map to 3 (and beyond 5V if the hardware does not burn)[/sub][/color]

so when you have a x value 0,1,2 or 3 you need to decide arbitrarily what voltage was the input. The point I make above is that dividing by 4 to get a possible estimate of the original voltage by using x * 5 / 4 always gives you the entry point of the interval (0 - 1.25, 2.50, 3.75) whereas dividing by 3 gives you a point somewhere within the interval (0, 1.66, 3.33, 5) which are not worse than the other values... they are just as likely possible estimates.

Now assuming you have input varying amongst the full scope and you do millions of reading, when the ADC tells you 1 the second approach (dividing by 3) will give you 1.6666 for something that was between 1.25 and 2.50 so the max error you are doing is 0.83 whereas when you divided by 4 the max error is 1.25V..

--> as said above, taking the middle of the interval (x + 0.5) * V[tt]ref / 1023 [/tt]is actually what minimizes this max error. You could decide to take always the min of the interval (divide by 1024), always take the max of the interval (same absolute max error), or shoot anywhere randomly within the interval... All are valid answers for an estimate.

zoomx:
This way the last segment has not the same length of the others, for sure is longer, and you don't know how is long because you can get the same value, 1023, also for 5.5V, maybe more, maybe less.
In other words 1023 mean a value that is bigger than Vref-Vref/1024 (not Vref!) but you don't know how many is bigger.

Same thing goes for > 5V when dividing by 1024. What's your point worth making?

When you start at zero what could be 1/2 you end at Vref - Vref/1024 rather than Vref = Vref/2048 which is More Real.

The hardware does this in 10 stages, not 1024.

Is it worse to play in shadows or to act like there are none?

Nobody that was shown properly successive approximation A/D conversion method will go down the wrong path of 5/1023, right? Do that in the online calculator and see how many decimal places the result has. It keeps going and going. But 5/1024 is different.

The way it works is... 10 bit... 1024 levels. Level 1 to level 1024. Voltage range is 5V for the analog range. 1024 levels (steps) contains 1023 individual gaps..... just as 2 steps has 1 gap. We don't divide the 5V range into 1023 portions. Instead, we divide it by the total number of levels (ie. 1024).

For later... the D/A will have a voltage res of 5/1024 volt. Why D/A? Because a successive approx A/D contains a D/A within it. Zero volt analog will convert to level 1.... all zeros binary... ie. decimal 0. And 2.5V converts to level 512... or binary 1000000000 or decimal 512.... which is 512*[5/1024] V. And level 1024 is all ones binary.... which is decimal 1023, which corresponds to 1023*[5/1024] volt. The highest analog voltage 5V rounds down to 1023*[5/1024]. This just means..... all binary ones 1111111111 (decimal 1023) represents (5V-5/1024) V, or 1023*[5/1024] V.

To avoid bias..... they use noise dithering, right?

It's true that for relatively large bit length.... the 5/1023 or 5/1024 thing give relatively close results. But should always be divide by Vrange/1024 for the voltage resolution. It's definitely not 5/1023.

Southpark:
The 5/1023 or 5/1024 thing give relatively close results. But should always be divide by Vrange/1024 for the voltage resolution. It's definitely not 5/1023.

Seems you have not read the above... see the graphs in post #123 you will see that dividing by 1023 is as legit as dividing by 1024 => it gives you one of the possible value

J-M-L:
Seems you have not read the above... see the graphs in post #123 you will see that dividing by 1023 is as legit as dividing by 1024 => it gives you one of the possible value

Thanks jml.... but the method used in successive approx is the method of halves...or halving. So what you wrote below shows method of halving.... where the input analog voltage is compared with exactly half the voltage range ... ie. compared with 2.5 volt.

J-M-L:
always gives you the entry point of the interval (0, 1.25, 2.50, 3.75) whereas dividing by 3 gives you a point somewhere within the interval (0, 1.66, 3.33, 5) which are not worse than the other values

The 0, 1.25, 2.5, 3.75 system does just that. ie. 2.5 is halfway between 0 and 5. And, 1.25 is exactly halfway between 0 and 2.5.....and 3.75 is halfway between 2.5 and 5. It's the divide and conquer method.... easily implemented. That's what they use, right? And when they apply the D/A, which is required in a A/D process.....the D/A constructs an analog level based on the 2 bit resolution of 5/(2^2) = 5/4 volt, right? It all ties up to the hardware link between the A/D and internal D/A, right?

But I do agree that if we use our own system, then we can define or specify own levels of quantisation, or customise the system to however we want. But if the arduino system is based on say successive approximation, then the calculations/math will be based on their system. So if the documentations/data sheet reckons 5/1024 as the voltage resolution, then that is what should be used.

There is no but...any voltage value in the interval corresponding to the digital value is as good as any other one... that’s it.