Dividing by 1023 or 1024? The final verdict on analogRead

Smajdalf:
This just shows how incompetent you are for this discussion.

Either that or you grossly misunderstood the message.

Smajdalf:
It does not say that because the whole idea of converting ADC result to voltage is silly

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

It's almost like most of the conversation never happened. I've even seen the same lame strawman raised again to a slightly different tune with the small number of bins.

It's almost like people can only interpret ADC data to hardware purity standards. You can get an answer that rounds up to 5V, just whatever you do, don't use shortcuts that profane the holy 1024!

There is no holy in 1024. The datasheet says that the ADC range is not 0-Vref but 0-(Vref-Vref/1024). The difference is very little because we have 10 bits but if, for example, we have only 2 bit it mean that the ADC range, if Vref is 5V, is only 0-4V because the ADC in Atmega328 works in this way.

I know. I just think that the purpose of the coder does not always require the most probable voltage (measured mid-step) nor does it follow the least approximation of Vref x ADC/1024.

0 - ( Vref - Vref / 1024 )

There's really 10 steps that get compared and 10 bits only ever adds up to 1023, 0 is the 1024th. I think there may need to be a voltage difference to read a step at all in which case the ADC can never measure Vref.

You could round up to the nearest 8 or 10 mV. If that's acceptable, you could divide by 1023 and be well within tolerance.

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.