Go Down

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

GoForSmoke

The whole idea of converting a range of values to a single number has an inherent known error we design around, same as with tolerance on resistors. Nothing is perfect, tolerances is a way to make things fit by being close enough. All of manufacturing works this way.

So please don't get purist to the hardware when an abstraction gets applied to the data that causes less error than you can measure. Please get the pebble away from your eye and see that it is not bigger than the moon. I spread the range by less than 1/1000th because scale and existing error margins erase the cost by magnitudes and get 0V to 5V where purely it measures 0V to 4.995V even when the REALITY is 5V. Why does that difference not seem important to the hardware 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

That is not how the AVR analog-to-digital converter works.  A one bit successive approximation converter with a reference of 5 volts makes just a single comparison: Is the voltage less than 2.5 volts?  If the answer is "yes" then the converted value is zero.  If the answer is "no" then the converted value is one.


So you could get 0V or 5V, OFF or ON with just 1 bit?

If the reality is less than half, it's 0 otherwise it's 1, for every 1 one of 10 bits. That's what the datasheet says too.

Proofreading fail.
Have you addressed me any better? Ignored anything?

Wait... do you just convert all ADC reads and work with that? That would be work with floats but shriek about  /1023?
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.

TolpuddleSartre

Quote
So you could get 0V or 5V, OFF or ON with just 1 bit?
No, that would be "less than or equal to 2.5V, or greater than 2.5V (including greater than 5V)". Or vice versa.
Do try to keep up.

Coding Badly

#48
Mar 29, 2018, 08:56 pm Last Edit: Mar 29, 2018, 08:57 pm by Coding Badly
So you could get 0V or 5V, OFF or ON with just 1 bit?
I see your failure to correctly quote / proofread your post and my failure to notice your failure lead us to this point.  I would ask you to correct your mistake but I have a strong suspicion that you would simply ignore my request.

In any case, I apologize for my mistake.  My reply should have been direct to @Budvar10.


GoForSmoke

No, that would be "less than or equal to 2.5V, or greater than 2.5V (including greater than 5V)". Or vice versa.
Do try to keep up.
I already wrote about the difference between the data and the conversion to a single number of volts.

I do not advocate for converting ADC data for work purposes. That conforms with your description above.

However I note that the process of turning ranges (less than or equal to 2.5V, or greater than 2.5V) into a single value to be labeled Volts is a different thing altogether.

You seem to imply that I indiscriminately apply the conversion everywhere or perhaps the difference is beyond you.

It's a math trick, not a statement about the precious hardware. Hardware should serve software should serve people.

CB: but I have a strong suspicion that you would simply ignore my request.

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

#50
Mar 29, 2018, 09:44 pm Last Edit: Mar 29, 2018, 09:46 pm by Coding Badly

Nice.  Thank you.


1/1024 = 0.000976562
1/1023 = 0.000977517
Difference when Vref is 5V is less than 5 microvolts.
That certainly seems insignificant. 

But, I wonder what happens when considered in the context of PID.  More specifically what happens when we...

Add a small error then add a small error then add a small error then add a small error...  You know.  Integrate.

I wonder if that would be considered a systematic error.  You know.  The kind of error no self respecting engineer would ever make.   ;)

Quote
Dividing by 1023 or 1024? The final verdict on analogRead
Neither.  The correct divisor is determined by calibrating.  On a regular schedule.  Using statistical methods.

In lieu of calibration the correct divisor for nearly everyone is the value specified by the hardware manufacturer.  By now we all know what that is.


GoForSmoke

I see your failure to correctly quote / proofread your post and my failure to notice your failure lead us to this point.  I would ask you to correct your mistake but I have a strong suspicion that you would simply ignore my request.

In any case, I apologize for my mistake.  My reply should have been direct to @Budvar10.


I hope that's fixed now.

It illustrates the difference behind what the 0/1 data means and the assignment of a single value to each outcome.

When we work at the millivolt level, the error is small. That final step covers less than 4.883mV in which the real voltage may be and the first step covers the same. Every step may be half or less, or more than half, of less than 1mV.

Keep the data as 10-bit and work with it that way is my choice. It minimizes error and Arduino floats suck.

But when converting to volts for display... not covering both ends of the scale is a flaw in itself.

What /1023 does is to interpret the ADC range as from 0V to 5.0049V with the last step starting at 5V.
It's an act of data interpretation, not operating code or nefarious disinformation. How dare I perform cheap scaling!
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

Nice.  Thank you.


That certainly seems insignificant. 

But, I wonder what happens when considered in the context of PID.  More specifically what happens when we...

Add a small error then add a small error then add a small error then add a small error...  You know.  Integrate.

I wonder if that would be considered a systematic error.  You know.  The kind of error no self respecting engineer would ever make.   ;)

Here I have to stress yet again to WORK with the ADC data.

DISPLAY converted data but WORK with read values. It's faster and it makes tabling much easier. :)



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.

TolpuddleSartre

Quote
What /1023 does is to interpret the ADC range as from 0V to 5.0049V with the last step starting at 5V.
That would be "What /1023 does is to interpret the ADC range as from 0V to any voltage over five volts (that doesn't actually break the comparator), with the last step starting at 5V."

Robin2

What /1023 does is to interpret the ADC range as from 0V to 5.0049V with the last step starting at 5V.
That makes no sense to me.

If Vref is 5v the ADC will treat give the highest possible value for anything greater than (5v - 5v / Nsteps)  - and it is probably irrelevant for this comment whether N is 1023 or 1024.

If your program then gets a reading of the highest value (let's say it gets 1023) your program must consider the real value to be between  (5 - 5/N)volts and 5 volts.

I remain of the view that is is easier to understand what is happening if you imagine an ADC that produces the values 0, 1, 2, 3, 4 and 5 for the range 0v to 5v because the errors are much more obvious.

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

lastchancename

#55
Mar 29, 2018, 11:42 pm Last Edit: Mar 29, 2018, 11:43 pm by lastchancename
OK, so this is moving to the INSOMNIAC section?

I was only going to use the 12-bit ADC to plot my tpreturn flightpath from the moon. 

Surely 1-bit error in 12 isn't 'that serious'  :)
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

GoForSmoke

That would be "What /1023 does is to interpret the ADC range as from 0V to any voltage over five volts (that doesn't actually break the comparator), with the last step starting at 5V."
What voltage you feed the pin is up to you. If you give if > Vref then don't blame a formula for bad results.

But if you behave and give it no more than 5V, it will report actual 4.995V to 5.0V as 5 volts.

If I give the pin 5.05V and divide by 1024, will it tell me the true voltage? No. Conversion by /1024 won't tell me 5V either, not without being unable to tell me 0V or some division of one stage is left uncovered.




 
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.

Robin2

Surely 1-bit error in 12 isn't 'that serious'  :)
Of course not.

But neither is a crossword-puzzle - that does not prevent it from being a puzzle for which one would like to find the correct answer.

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

GoForSmoke

That makes no sense to me.

If Vref is 5v the ADC will treat give the highest possible value for anything greater than (5v - 5v / Nsteps)  - and it is probably irrelevant for this comment whether N is 1023 or 1024.

If your program then gets a reading of the highest value (let's say it gets 1023) your program must consider the real value to be between  (5 - 5/N)volts and 5 volts.

I remain of the view that is is easier to understand what is happening if you imagine an ADC that produces the values 0, 1, 2, 3, 4 and 5 for the range 0v to 5v because the errors are much more obvious.

...R
In reality, stage 1023 measures the same range, the interpretation makes it "look like" the data shifts up.  Consider how the 4.995V to 4.999V stage gets dropped when converting the read by /1024.

Each stage gets a slice. A 1/1023 slice is wider than a 1/1024 slice by 1/(1024^2). The stretch is there's 1024 wider slices.  

Divide by 1024:
Highest read is 1023. 5000mV * 1023/1024 = 4995 in data maximum regardless of input when minimum may be 0V.

I present that the /1024 conversion does not represent the ADC data or the input voltage so well as have room to point and preach as if the true meaning of the ADC is the whole of the process. If you can't be substantially more precise, you got no room to pick.  

Divide by 1023:
If the read is 1023 then /1023 gives 5000mV input * 1023/1023 = 5000 in data. 1022 gives 4995, 511 gives 2497 and 512 gives 2502, 0 gives 0.

5000 * 1000/1023 gives 4887.
5000 * 1000/1024 gives 4882 where the ADC step is 4.882 to 4.887mV.  

I don't tell you to not use /1024 conversions.
I give reasons why you shouldn't but I don't condemn the use of /1024 outright.

People should have a choice as to which way to be ever so slightly wrong in at least some cases. Don't pre-judge.
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

Of course not.

But neither is a crossword-puzzle - that does not prevent it from being a puzzle for which one would like to find the correct answer.

...R
Turning a range of values into a single value leaves correct behind. Say hello to best fit.
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.

Go Up