Go Down

Topic: Dividing by 1023 or 1024? The final verdict on analogRead (Read 7062 times) previous topic - next topic

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

GoForSmoke

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!


1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

zoomx

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.

GoForSmoke

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.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

J-M-L

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 V between GND and Vcc
- analogRead() gives you a value of x

As documented in the datasheet, x represents any voltage between x * Vcc / 1024  and (x+1)* Vcc / 1024

That means that if you take Vest  =  x * Vcc / 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 Vcc / 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: Vest  = x * Vcc / 1023 to represent a fair possible value, what does that mean?

There are two key questions:

#1/ Is this Vest 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 * Vcc / 1024   <=    x * Vcc / 1023   <=    (x+1)* Vcc / 1024

--> So Vest  = x * Vcc / 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 Vest moves across the possible interval depending on x

With this approach one sees (the blue arrow) that Vest 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 Vcc / 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 V and thus are both correct. everyone wins :)

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

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


Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Robin2

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 :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

J-M-L

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

Here's to the next 30 replies :)

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

Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

GoForSmoke

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!
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

Coding Badly

#113
Apr 15, 2018, 09:10 pm Last Edit: Apr 15, 2018, 09:20 pm by Coding Badly
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 V 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


J-M-L

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 :)
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

GoForSmoke

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.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

GoForSmoke

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?
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

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.


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.

GoForSmoke

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.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

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.
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)
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Go Up