Arduino Forum

General Category => General Discussion => Topic started by: Paulnaude01 on Mar 25, 2018, 03:54 pm

Title: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Paulnaude01 on Mar 25, 2018, 03:54 pm
The correct answer is: 1024 (but the error is so small if you use 1023, it actually doesn't matter since the reference volt will vary with a much larger margin of error than your calculation will introduce).

But in case you are still wondering why not 1023, here it is.

After reading many responses from many posts on this exact issue (and some go into very technical detail why it should be one or the other), the bottom line is this:

The confusion boils down to a simple miss interpretation of the facts (and some confusion caused by the math).

NB: The result from analogRead() is not a value (as we would like to think of it when measuring something), its an index. Its an index to a bin number in which the measured value fits, and there are 1024 bins (which has to represents the whole range of values you want to measure).

The first bin number (or bin ID number) is 0, the second bin is 1, the third is 2 etc. until the 1024th bin which has an ID number of 1023.

When you use the equation of (1023/1024)*5 to prove me wrong, since the answer is not 5 (as you want it to be when measuring 5Volt with a 5Volt reference and analogRead()=1023; then you are missing my point!

This is the issue with converting analog values into digital values. With Digital values, there are no in-betweens. A lot of the detail actually gets lost, and more so when we have fewer bins to fit a large range of values into.

Example: If the reference voltage Vref is 5Volt, and the input value is 5Volt, I expect analogRead to provide me with 1023 (the last or highest bin number in the range) so I can interpret that at as 5Volt.
When analogRead gives me 0, I expect the value to be 0 Volt.

BUT now, in which bins would you like to put 0.0001 Volt or 4.999 Volt? When is the value measured too big for the first bin or too small for the last bin?

The fact of the matter is, you only have 1024 bins to put all the values in from 0 to 5 Volt, which is good enough for most things, but it does show a bit of a problem when we want to work back from the digital bin number to the original measured analog number.

With only 1024 bins, and if the range of volt values we want to measure goes from 0 to 5 Volt, it means each bin is 5/1024 = 0.0048828125 Volts "big", which means, measured values from 0 to 0.004882812 will fit in the first bin (bin 0) and values from 4.9951171875 to 5 (and more) will fit in the last bin (bin 1023).

So, the bottom line is that in the analog world, there can be millions of different measured volt values between 0 and 5 volt, but in this 10bit digital system of ours, there can only be 1024.

This means unfortunately that if I read a value of 0 using analogRead(), the value can be anything from 0 to 0.00482...V and if I read a value of 1023, the actual value can be anything from 4.995112... to 5 volt (and more).

So back to the original question: 1023 or 1024?

If you use 1023, your bins are actually larger because you use 1 less bin and 1/1023=0.0009775...V versus 1/1024=0.0009765...V). This means that when analogRead()=1023, the value you calculate may be 5volt using the formula "value/1023*5", but it actually also include smaller values down to 4.995112...V, since those values will also fit in the same bin.

When you use 1024, the bins are smaller, and thus 5*1023/1024=4.995117, which is the smallest value that will fit in the same bin.

In short, 1024 is actually more accurate to use, eventhough the math may let you to believe its wrong!





Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: spycatcher2k on Mar 25, 2018, 04:16 pm
Or you could use basic logic!

You can read 1024 different values, so you are reading 1/1024 of the full range in each value from 0 to 1023!
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: TolpuddleSartre on Mar 25, 2018, 04:48 pm
Or you could simply read the processor's datasheet, where the section on the ADC states:


ADC = (VIN * 1024) / VREF

and

"0x000 represents analog ground, and 0x3FF represents the selected reference voltage minus one LSB."
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 25, 2018, 05:03 pm
And you can divide by 1024 with >>10

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: TolpuddleSartre on Mar 25, 2018, 05:06 pm
...which is probably what the compiler does for you.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: robtillaart on Mar 25, 2018, 05:30 pm
... unless you are using float
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 25, 2018, 06:57 pm
...which is probably what the compiler does for you.
I don't think it converts divisions by powers of 2 into right-shifts

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: robtillaart on Mar 25, 2018, 07:00 pm
I don't think it converts divisions by powers of 2 into right-shifts

...R
Write a test sketch, the proof is in the pudding test ....
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: TolpuddleSartre on Mar 25, 2018, 07:02 pm
Interesting - I doubt the AVR has a barrel shifter.

Smaller powers of two divides will be done with shifts though, unless the compiler is exceptionally dumb.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 25, 2018, 07:35 pm
Write a test sketch, the proof is in the pudding test ....
I believe I did some time ago - hence my comment, but I am not certain.

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GolamMostafa on Mar 26, 2018, 08:53 pm
Quote
NB: The result from analogRead() is not a value (as we would like to think of it when measuring something), its an index. Its an index to a bin number in which the measured value fits, and there are 1024 bins (which has to represents the whole range of values you want to measure).
Because the values returned by analogRead() are the indices, we need to calibrate these indices with respect to two known points in order to figure out the exact value of the unknown input voltage of the ADC. What are these two known points:

1. 0V at the input of the ADC                                                     ----> ADC produces 00 0000 0000
2. 5V (equal to VREF = Full Scale) at the input of ADC ----> ADC produces 11 1111 1111
3. VDC (unknown voltage) at the input of ADC            -----> ADC produces ? (say, ADC)

ADC = ((11 1111 1111)/5)*VDC = (1023/5)*VDC
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: TolpuddleSartre on Mar 26, 2018, 08:56 pm
Quote
2. 5V (equal to VREF = Full Scale) at the input of ADC ----> ADC produces 11 1111 1111
As does 5 - (5/1024) V.

See my earlier quote from manufacturer's datasheet.

Quote
What are these two known points
So then you go and list three points !  :D
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: larryd on Mar 26, 2018, 10:08 pm
1023 or 1024, no significant difference, 1024 would be the # as per manufacture.

Seems like this thread has turned inside out, we are better than this.



Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: TolpuddleSartre on Mar 26, 2018, 10:13 pm
I don't know what "inside out" means in this respect, but I suspect this thread doesn't belong in this section, and I'd be happy for it to    be closed or moved.
I'm pretty sure this must have been discussed before, but yes, it is a bit of a "Angels on a pinhead" sort of thread.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GolamMostafa on Mar 27, 2018, 06:20 am
Quote
So then you go and list three points !
A linear system like the ADC of the ATmega328 requires only two point responses for calibration?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 28, 2018, 04:15 am
If you want your results to read 0 to Vref and are willing to live with less error than you can measure, divide by 1023.

If you want to be a pedantic-retentive obsessed with correctness you can never show or use, divide by 1024 and feel superior.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Whandall on Mar 28, 2018, 09:31 am
If you want your results to read 0 to Vref and are willing to live with less error than you can measure, divide by 1023.

If you want to be a pedantic-retentive obsessed with correctness you can never show or use, divide by 1024 and feel superior.
Doing things deliberately wrong/incorrect would make me feel dumb, how about you?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: TolpuddleSartre on Mar 28, 2018, 10:10 am
Doing things deliberately wrong/incorrect would make me feel dumb, how about you?
Can I give you a hug?
Would that be awkward?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 28, 2018, 10:52 am
Doing things deliberately wrong/incorrect would make me feel dumb, how about you?
First I make sure it's actually "wrong", noting that slavishness to one form of error is not "right".

When the error is below the noise level of somebody's idea of "right", that's how insubstantially small their claim is.

I'd feel dumb claiming either way to be right when both ways yield errors when used to convert ADC to analog values.
I'd feel even more dumb if I couldn't see that as fact and then decide which one serves my app best.

If you want your results to read 0 to Vref and are willing to live with less error than you can measure, divide by 1023.

If you can't stand knowing that the hardware has 1024 steps and you have a need to keep that from abstraction then you should know that you have not improved accuracy and now can't get any result showing signal in either the 1st Vref/1024 or the last Vref/1024 part of the range regardless of the voltage measured, which seems a bit funny for "right" doesn't it?

When I compare the two, I prefer the first since the error is less than noise and at least near 0 gives me 0 and near Vref gives me Vref.

Purity to hardware that is supposed to be approximating something else, it's the something else that I want. If the way I get that does not cause actual as opposed to supposed bad data and that way covers a GAP in the supposed right way at the same time, no I don't feel dumb taking that bit of win at all. I wonder at the ones who don't! Perhaps 3 factors at once confuse them?

PS -- I get a big laugh at people who go on about preserving the "purity" of what is not pure.

So my question for those is: Why does analog oversampling work?

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

THINK
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 28, 2018, 11:47 am
PS -- I get a big laugh at people who go on about preserving the "purity" of what is not pure.
Reminds me of the story about the engineer, the mathematician and the economist who were at the door to a room with an attractive woman (or man, I guess) in the room at the opposite wall. They were told that they could kiss the woman if they could get to her by following a simple rule.

They could start to cross the room with the longest step they could make, but each subsequent step would have to half the distance of the previous step.

The engineer asked how would he know what direction to go.

The mathematician said it was impossible.

The economist said that after 4 steps he would be close enough.

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: TolpuddleSartre on Mar 28, 2018, 12:40 pm
In real life, we all bodge things, but I still think it is better if you know the underlying principles, for those edge cases where it will matter.

I guess I'll just have to live with my feelings of being superior correct and literate.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 28, 2018, 03:17 pm
Now that my interest has been awoken sufficiently to read the Atmega328 datasheet ...

The data sheet clearly states what is in Reply #2
ADC = (VIN * 1024) / VREF

However if you imagine a very crude ADC that can only report the values 0, 1, 2, 3, 4 and 5 for a range of 0v to 5v you can easily see that the only sensible calculation is
ADC = VIN * number of intervals / VREF

For example, if Vref is 5v and Vin is 3v the calculation will be 3 * 5 / 5 = 3

For the Atmega 328 the number of intervals is 1023, not 1024. And if the crude ADC was to mimic the Atmega datasheet the calculation would be 3 * 6 / 5  = 3.6 which should round to 4 and which would be incorrect. Of course an integer only system would report 3 (rounding down) so the error would not appear.

All of which leads me to believe that the Atmega 328 ADC is not sufficiently precise for it to matter whether you use 1023 or 1024. And that the assertion that started this Thread has no basis in theory and (as the OP acknowledged) does not matter in reality.

...R
(Runs for bomb shelter)
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 28, 2018, 03:49 pm
In real life, we all bodge things, but I still think it is better if you know the underlying principles, for those edge cases where it will matter
Knew this years ago. I understand what the device does and how precise it is not.

If I give it 5V and it tells me 1023 that really means 4.995V to 5V if I go by numbers alone.  That's a 5mV step.

The difference between 1/1023 and 1/1024 is less than a thousandth of that. Yeah... bodge... uhuh.

Let me know when you figure out the underlying realities including what the ADC is for.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 28, 2018, 04:45 pm
Now that my interest has been awoken sufficiently to read the Atmega328 datasheet ...

The data sheet clearly states what is in Reply #2
ADC = (VIN * 1024) / VREF

However if you imagine a very crude ADC that can only report the values 0, 1, 2, 3, 4 and 5 for a range of 0v to 5v you can easily see that the only sensible calculation is
ADC = VIN * number of intervals / VREF

For example, if Vref is 5v and Vin is 3v the calculation will be 3 * 5 / 5 = 3

For the Atmega 328 the number of intervals is 1023, not 1024. And if the crude ADC was to mimic the Atmega datasheet the calculation would be 3 * 6 / 5  = 3.6 which should round to 4 and which would be incorrect. Of course an integer only system would report 3 (rounding down) so the error would not appear.

All of which leads me to believe that the Atmega 328 ADC is not sufficiently precise for it to matter whether you use 1023 or 1024. And that the assertion that started this Thread has no basis in theory and (as the OP acknowledged) does not matter in reality.

...R
(Runs for bomb shelter)
The ADC does have 1024 steps.  The zero step covers 0V to Vref/1024 (just under 5mV whenVref is 5V) returning 0.

Quote
28.6.3.
ADC Accuracy Definitions
An n-bit single-ended ADC converts a voltage linearly between GND and V REF in 2 n steps (LSBs). The
lowest code is read as 0, and the highest code is read as 2 n -1.
2^0 to 2^(1024-1) and we get n as the return, 1024 possible values.

The fun comes when reducing a range of values to a number treated as volts. We measure a smudge and give it a number.
 
The most of time closest number would be in the middle of the range and never be either 0V or 5V.

By dividing by 1023, I cover both ends in the math with a calculated 1/1024 error, looking at less than 5 microvolts.
Unless I need to show or calculate down to 6 decimal places, it's not a problem. But if I do, I want a different ADC.

For more details on ADC, check with Nick;
 http://www.gammon.com.au/adc
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: larryd on Mar 28, 2018, 05:19 pm
This all needs to be in the 'Introductory tutorials' forum.   :(





Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 28, 2018, 06:19 pm
Perhaps the OP will consider replies, but seems pretty set from the start.

Most users want to be able to jumper 5V and see 5V displayed or jumper GND and see 0V displayed.

Now another person might make quicker and easier sense just viewing the ADC value, coding to it and never converting it to volts. And divide by what ceases to be an issue.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 28, 2018, 06:47 pm
The ADC does have 1024 steps.  The zero step covers 0V to Vref/1024 (just under 5mV whenVref is 5V) returning 0.
In the range 0 1 2 3 4 5 there are 5 intervals. 5 - 0 = 5

I am inclined to think that if I wrote out all of the numbers from 0 to 1023 I would have 1023 - 0 = 1023 intervals.

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 28, 2018, 07:49 pm

char buff[ 1024 ]; // has how many elements and what is the high index?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 28, 2018, 08:46 pm
char buff[ 1024 ]; // has how many elements and what is the high index?
I agree there are 1024 numbers. But there are only 1023 intervals between those numbers.

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 28, 2018, 09:02 pm
Do you mean?

Divide 1024 by 1024 and you can cover one end (0V to 4.995V) or the other (0.4883 to 5V) and either way the range is open-ended.

Divide by 1024 by 1023 and you have 1 extra that closes the end.

The error is 6 decimal places down, Arduino floats guarantee 6 places total and ... they're dirt-in-the-sand IEEE 32-bit FP.

At least work to the ADC value where possible, it's the conversion of ranges to values that forces errors.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: TolpuddleSartre on Mar 28, 2018, 09:15 pm
I'll just leave this (http://uk.farnell.com/c/semiconductors-ics/power-management-ics-pmic/voltage-references?reference-voltage=1.024v) here.
(Drops mic)
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 28, 2018, 09:46 pm
If you can't answer points, imply everything comes down to a single issue.

With a 1.024V ref I can work the 10 bits of the ADC ladder value as factors of 1 mV, knowing instrument error from specs.
Please understand that doing so does not involve conversion to volts and treats the ADC value as a range.

If you fixate on hardware details you may lose signt of the what you're doing with them part. Or you're a EE.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 28, 2018, 10:35 pm
Do you mean?
Look back at the 0-5 example in Reply #21

Quote
At least work to the ADC value where possible, it's the conversion of ranges to values that forces errors.
I agree 100%. I am just pursuing this Thread for intellectual fun.

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 28, 2018, 10:55 pm
The fun part is that with a 1.024V Vref, just going by 1mV x ADC the most you get is 1.023mV.

AVR has a built-in 1.1V Vref for 1.074mV steps x highest possible read 1023 = 1.099V.

If that 1.1V could be voltage divided to a 1.023Vref (perfect world), that would be conversion-friendly IF you're into converting.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Budvar10 on Mar 29, 2018, 10:20 am
ADC/1024 is correct, and ADC/1023 is wrong!

Let say we have just 1 bit ADC, 0 or 5V. Just two possible values. Should be /0 used?

And here is for n=5:
Code: [Select]

First *x/(n-1) math:                                                              
input voltage   ADC value   ADC*x/(n-1)   result   average error   output
4.00-4.99       4           *5/4          5.00     +0.50           5          
3.00-3.99       3           *5/4          3.75     +0.25           3
2.00-2.99       2           *5/4          2.50      0.00           2
1.00-1.99       1           *5/4          1.25     -0.25           1
0.00-0.99       0           *5/4          0.00     -0.50           0
Note that for the 5 possible ADC values, the output scale now has 6 values (0-5)
and the value 4 can never occur! And although the average error might look to be
nicely distributed +/- the centre of scale, the actual output result proves to be
much uglier. For slowly increasing voltage, the ADC would read; 0, 1, 2, 3, 5!!!
The maximum error in real world use is 2V, because that very tiny change of
3.999V - 4.001V would cause an output change of 3V -> 5V or a change of 2V!
Correct scaling math *x/n:
input voltage   ADC value   ADC*x/n       result   average error   output  
4.00-4.99       4           *5/5          4.00     -0.50           4          
3.00-3.99       3           *5/5          3.00     -0.50           3          
2.00-2.99       2           *5/5          2.00     -0.50           2          
1.00-1.99       1           *5/5          1.00     -0.50           1          
0.00-0.99       0           *5/5          0.00     -0.50           0                                                                                            #
All output values are properly scaled and all are represented. The average error
is never greater than 0.5 (no more average error than the /(n-1) example),      
the average error is always one fixed value (-0.5) making it very easy
to compensate. The maximum error at any time is 1V, this is half the max error of
the /(n-1) example, which can introduce extra error up to 1 in high value ADC
readings
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 29, 2018, 10:41 am
Referring to Reply #34 ...

I think the assumption that 4.00 to 4.99 gives 4 and 3.00 to 3.99 gives 3 is incorrect. I would expect 4.5 to 4.99 to give 5 and 3.5 to 4.49 to give 4

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: TolpuddleSartre on Mar 29, 2018, 10:49 am
Please, can someone put this thread out of its misery?
Oh, the humanity!

Quote
Let say we have just 1 bit ADC, 0 or 5V. Just two possible values. Should be /0 used?
No, because 21 is 2, so by GoForSmoke's logic, we would divide by 1.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 29, 2018, 10:55 am
Maybe it is time to move it to Bar Sport ?

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: lastchancename on Mar 29, 2018, 01:33 pm
...and this thread explains why binary logic wins!
We don't need no stinkin' analog!

Zero or One - none of this warm, fuzzy stuff. It just confuses things.
;)
Can we kill this thread?  Although it has had interesting moments!
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GolamMostafa on Mar 29, 2018, 05:40 pm
Quote
Maybe it is time to move it to Bar Sport ?
It's very interesting that the OP has not yet made any comment; but, you want move to the Bar Sport leaving him behind?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: lastchancename on Mar 29, 2018, 05:45 pm
Bar Sport is Probably better than TUTORIALS.
Maybe Programming Questions?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 29, 2018, 06:16 pm
It's very interesting that the OP has not yet made any comment; but, you want move to the Bar Sport leaving him behind?
No. He must come with us.

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 29, 2018, 07:44 pm
ADC/1024 is correct, and ADC/1023 is wrong!

Let say we have just 1 bit ADC, 0 or 5V. Just two possible values. Should be /0 used?

And here is for n=5:
Code: [Select]

First *x/(n-1) math:                                                              
input voltage   ADC value   ADC*x/(n-1)   result   average error   output
4.00-4.99       4           *5/4          5.00     +0.50           5          
3.00-3.99       3           *5/4          3.75     +0.25           3
2.00-2.99       2           *5/4          2.50      0.00           2
1.00-1.99       1           *5/4          1.25     -0.25           1
0.00-0.99       0           *5/4          0.00     -0.50           0
Note that for the 5 possible ADC values, the output scale now has 6 values (0-5)
and the value 4 can never occur! And although the average error might look to be
nicely distributed +/- the centre of scale, the actual output result proves to be
much uglier. For slowly increasing voltage, the ADC would read; 0, 1, 2, 3, 5!!!
The maximum error in real world use is 2V, because that very tiny change of
3.999V - 4.001V would cause an output change of 3V -> 5V or a change of 2V!
Correct scaling math *x/n:
input voltage   ADC value   ADC*x/n       result   average error   output  
4.00-4.99       4           *5/5          4.00     -0.50           4          
3.00-3.99       3           *5/5          3.00     -0.50           3          
2.00-2.99       2           *5/5          2.00     -0.50           2          
1.00-1.99       1           *5/5          1.00     -0.50           1          
0.00-0.99       0           *5/5          0.00     -0.50           0                                                                                            #
All output values are properly scaled and all are represented. The average error
is never greater than 0.5 (no more average error than the /(n-1) example),      
the average error is always one fixed value (-0.5) making it very easy
to compensate. The maximum error at any time is 1V, this is half the max error of
the /(n-1) example, which can introduce extra error up to 1 in high value ADC
readings


Please, can someone put this thread out of its misery?
Oh, the humanity!
No, because 21 is 2, so by GoForSmoke's logic, we would divide by 1.
These are rather poor strawmen.

TS, how do YOU convert a single bit into voltage with any more accuracy than what you think to ridicule?
Oh? What? You can't?

Bud, if you measure only 5 steps, the CONVERSION error is no longer 3 orders of magnitude below instrument error.
The error diminishes at n(n-1) rate. I argue on practicalities that do not scale freely. Please at least stay in the ballpark!

I gave clear reasons that you don't address at all and instead erect these pre-broken strawmen.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: TolpuddleSartre on Mar 29, 2018, 07:46 pm
Proofreading fail.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Coding Badly on Mar 29, 2018, 07:51 pm
Let say we have just 1 bit ADC, 0 or 5V.
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.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 29, 2018, 08:18 pm
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?  

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 29, 2018, 08:32 pm
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?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: TolpuddleSartre on Mar 29, 2018, 08:37 pm
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.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Coding Badly on Mar 29, 2018, 08:56 pm
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.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 29, 2018, 09:10 pm
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.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Coding Badly on Mar 29, 2018, 09:44 pm

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.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 29, 2018, 09:51 pm
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!
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 29, 2018, 09:58 pm
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. :)



Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: TolpuddleSartre on Mar 29, 2018, 10:32 pm
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."
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 29, 2018, 11:18 pm
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
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: lastchancename on Mar 29, 2018, 11:42 pm
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'  :)
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 30, 2018, 08:35 am
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.




 
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 30, 2018, 10:00 am
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
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 30, 2018, 10:29 am
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.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 30, 2018, 11:53 am
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.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 30, 2018, 03:48 pm
Turning a range of values into a single value leaves correct behind. Say hello to best fit.
I only intended the word "correct" in the context of whether 1023 or 1024 is mathematically correct for the purpose of converting an ADC reading to the voltage it represents.

I am well aware that the theoretical mathematically derived value is not a precise representation of the actual voltage value that gave rise to the number produced by the ADC. Most of the time it is sufficient to use the ADC value without any conversion.

For the real world, none of the discussion in this Thread matters. :)

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GolamMostafa on Mar 30, 2018, 04:00 pm
If it does not work in theory, it will never work in practice. If it works in theory, it may not be working in practice; to make it work, a lot of polishing works are required that are the jobs of the Engineers?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 30, 2018, 05:44 pm
If it does not work in theory, it will never work in practice.
It is possible to know how to do something in practice without knowing the theory. The method by which aircraft wings generate lift is a good example.

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Coding Badly on Mar 30, 2018, 07:17 pm
Say hello to best fit.
Say hello to your personal definition of "best fit".

For everyone else, "best fit" happens when ADC counts are interpreted as being at the midpoint in a bucket.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: adwsystems on Mar 30, 2018, 07:24 pm
If the ADC outputs 0 counts for 0V, at what voltage does it output 1 count? (given Vref=5V and a 10 bit ADC)
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Coding Badly on Mar 30, 2018, 07:41 pm
If the ADC outputs 0 counts for 0V...
Incorrect.  Given that is the assumption for the question there is no point trying to answer.

An ideal successive approximation analog-to-digital converter ("the ADC") outputs 0 for an input voltage range.

Quote
...at what voltage does it output 1 count?
Over what range does the ADC output 1?  Is that what you meant to ask?

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: adwsystems on Mar 30, 2018, 07:50 pm
Not at all. I meant to ask what I asked. Perhaps more a more explicit question. What is the lowest voltage (in microvolts) that will produce an output of 1 count from the ADC?

P.S. Based on the project work last night, an Arduino UNO ADC outputs 0 count when the ADC input is connected to 0V (ground), I'm not sure how that can be incorrect.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 30, 2018, 07:59 pm
What is the lowest voltage (in microvolts) that will produce an output of 1 count from the ADC?
The width of each "box" will be 5v / 1023 (or 5v / 1024 - take your pick)

So if the bottom point is 0v the next point will be 4.44876 millivolts. And the change-over point should be half of that. Give or take.

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: adwsystems on Mar 30, 2018, 08:16 pm
What would happen if a series if statements was written to do the conversion from A to D.

If (0 <= Vin < 2.441) then output = 0
If (2.441 <= Vin < 7.323) then output = 1
...
If (4997.559 <= Vin < 5) then output =1023

There are 1024 possible values.

But does 5/1023 or 5/1024 result in the same list of values the if statements would need?

I am using thresholds of 5/1024 in the if statements, only because I have not refreshed my memory on the internals of ADCs to know what the actual (correct?) threshold is.

For someone with the right equipment and environment (I used to have access at a previous job but don't now or in my basement), should be able to ramp the microvolt input into the ADC and see when it changes over. Is it at 1/2 of 4.88768 or at 1/2 of 4.88281?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Coding Badly on Mar 30, 2018, 08:37 pm
The width of each "box" will be 5v / 1023 (or 5v / 1024 - take your pick)
There is no "take your pick".  By design the cutoff points for the buckets are Vref / (2^bits).  That is just the way a successive approximation converter works.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: adwsystems on Mar 30, 2018, 08:44 pm
Is there agreement that from 0 to (Vref / (2^bits)) / 2 the output is 0 and the output changes to a 1 at (Vref / (2^bits)) / 2 + 1uV? Or does the output change to 1 at Vref / (2^bits) + 1uV
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Coding Badly on Mar 30, 2018, 08:49 pm
Is there agreement that from 0 to (Vref / (2^bits)) / 2 the output is 0
That is not how they work.  That is also not what I wrote.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: adwsystems on Mar 30, 2018, 08:54 pm
That is not how they work.  That is also not what I wrote.
I know that is not what you wrote. That would be what I wrote in the question. I also noticed you have skirted answering three times.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Coding Badly on Mar 30, 2018, 09:21 pm

How the cutoffs are defined...
http://forum.arduino.cc/index.php?topic=537413.msg3670319#msg3670319 (http://forum.arduino.cc/index.php?topic=537413.msg3670319#msg3670319)

How the cutoffs are applied...
http://forum.arduino.cc/index.php?topic=537413.msg3668726#msg3668726 (http://forum.arduino.cc/index.php?topic=537413.msg3668726#msg3668726)

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: adwsystems on Mar 30, 2018, 09:28 pm
So your answer is B: The output changes to 1 at Vref / (2^bits) + 1uV.

Which make my thresholds in post #68 (very) wrong.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Coding Badly on Mar 30, 2018, 09:44 pm
So your answer is B: The output changes to 1 at Vref / (2^bits) + 1uV.
Ugh.  Neither post includes "1uV".  The answer is no, the output does not change to 1 at Vref / (2^bits) + 1uV.


Cutoff = Vref / (2^bits)

For your example...

Vref = 5 volts
bits = 10

So...

Cutoff = 5 / (2^10) = 0.0048828125

Which means...

For input voltages less than 1 * 0.0048828125 volts, a successive approximation analog-to-digital converter returns a zero.

For input voltages greater than or equal to 1 * 0.0048828125 volts and less than 2 * 0.0048828125 volts the ADC returns a one.

For input voltages greater than or equal to 2 * 0.0048828125 volts and less than 3 * 0.0048828125 volts the ADC returns a two.

Etcetera.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: adwsystems on Mar 30, 2018, 11:03 pm
It would have been less typing to just say: C-neither - Vref / (2^bits)

Which I did not derive from either of the posts that you recently linked.

If you wanted to expound, the ADC stays 0 until Vref / (2^bits) -1uV.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Coding Badly on Mar 30, 2018, 11:12 pm

You are welcome.

Oh.  Wait.  You don't appear to be grateful.  Never mind.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 31, 2018, 04:08 am
Say hello to your personal definition of "best fit".

For everyone else, "best fit" happens when ADC counts are interpreted as being at the midpoint in a bucket.

Sure, transistor tolerances just like all manufacturing tolerances and standardization are my personal definition, uhuh.

added:
This must be why there's a thread on denying the utility of dividing by 1023. Because of me. Everyone else knows better. This just gets funnier.

added:
Just because your calculator spits out a string of digits, reality only matches so many in a row.
The ADC step is almost 5mV wide with 5Vref, you can stop at 2 decimal places.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 31, 2018, 10:34 am
Cutoff = Vref / (2^bits)

For your example...

Vref = 5 volts
bits = 10

So...

Cutoff = 5 / (2^10) = 0.0048828125
That seems to be a very helpful way of expressing it, and I believe it is correct.

However, in one sense it is a lot more crude than I had expected, and in another way it answers a doubt that was in my mind when I wrote Reply #67.

If you imagine a 2-bit ADC - which can return values 0, 1, 2,and 3 and if you imagine that Vref is 4v. Then the cutoff will be 4 / (2^2) = 1. Which means that the changeover points will be 1, 2, and 3. The reason I see that as crude is that anything up to 0.999v will be reported as 0. And the answer to my doubt is that, by not having the changeover points at the half-way points there is no need to figure out how the first and last half divisions need to be figured (for example 0.5 and 3.5 in the simple example).

Now, what does that tell us about the process that started this Thread - how to convert the ADC value back to the voltage from which it was derived.

If the simple ADC produces a value of (say) 3 we can convert it back to a voltage either with Vref / 2^n * ADC (the equivalent to using 1024) or Vref / (2^n) - 1 (equivalent to using 1023 - the number of intervals)
4 / 2^2 * 3 = 3 and 4 / (2^2) - 1 = 4
Which is the correct answer?

The ADC would have produced 3 for an input voltage between 3v and 3.999v and we cannot know any closer than that what actually happened. So, in a sense, neither answer is correct and a better answer would the average of them - 3.5v.

Which I think can reasonably be extrapolated to the real 10bit ADC by saying that either calculation will be close enough.

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Mar 31, 2018, 11:15 am
 It's not like anyone is being forced to only convert to volts one way... yet. But the holy warriors have been triggered. 
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GolamMostafa on Mar 31, 2018, 01:05 pm
Quote
It is possible to know how to do something in practice without knowing the theory. The method by which aircraft wings generate lift is a good example.
The aircraft wings generate upward lift owing to the very universal law of the fluid mechanics. The technician was just lucky that the wings were fit together and the law worked well or the technician was highly intellect like the people of the Feroun's times who built the arches without knowing the geometry and the solid mechanics?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Mar 31, 2018, 03:50 pm
The aircraft wings generate upward lift owing to the very universal law of the fluid mechanics.
Perhaps you would be good enough to start a new Thread (in Bar Sport) and explain how it works.

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Smajdalf on Mar 31, 2018, 08:47 pm
I was planning to start a thread with similar title for a long time. Now I feel I must act now and support GoForSmoke against "1024 holy warriors".
Often when someone shows a code where 1023 divider is used to convert ADC reading to volts a "holy warrior" jumps up and tell them 1024 is the right divider and using 1023 is utterly wrong. I believe this advice is evil and should be punished with eternal torment. These are main reasons for this:

1) The advice implies the ADC is so precise difference between 1023 and 1024 matters. A noob often expects to get 5mV resolution and accuracy from the Arduino's ADC. But since default reference is noisy and poorly regulated 5V from a USB port total accuracy of Arduino is very poor. And there is a lot of other sources of error much more important than 1023 vs 1024.

2) Also it implies converting ADC reading to "real voltage" is the right way how to work with the result. In most applications there is no reason to convert the integer ADC result to float "real voltage". It only costs a lot of resources and adds nothing. Usually you want to compare the measured voltage to a threshold. "The right way" is to calculate (or find experimentaly) the threshold value directly in ADC "units".

2b) Only reason I can imagine to convert ADC result to "real voltage" is to show something to the user. For example you measure a wiper of a pot and show its position in %. When you use 100.0*ADCResult/1024 formula I expect the user to be quite disappointed to have range of values from 0 to 99.9%. When you use 100.0*ADCResult/1023 you get the expected range 0-100%.

3) Supporters of the 1024 divisor claim it is "the correct number". Since one ADC value represents a whole interval of possible voltages - (uncountable!) infinity of values - there is no single correct voltage corresponding to given ADC result. You can define criteria how to choose "the best value" from the infinity of possibilities. But then "the best value" depends on the chosen criteria. And there are good reasons for both 1024 and 1023 divisors. Let alone many other possibilities how to choose the right value. (For example I often use ADCResult*5 to show measured voltage in mV. Since my USB provides more than 5V it gives results closer to real value than "correct" ADCResult*5.0/1024).

In short - telling a noob to use 1024 instead of 1023 will not help them to get better results but will support bad habits and wrong beliefs. And that is evil.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: lastchancename on Apr 01, 2018, 01:37 am
While we're here...
How many of your Arduinos are running at a rock-solid EXACTLY 5.00V (or 3.30V)

I doubt many at all.
So that lost resolution is fairly academic unless you're using an external precision AREF
Which is fair enough - but worth considering
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Apr 01, 2018, 02:22 am
Turning ADC 10 bit to voltage different ways, it's okay to use what fits your app.

mV = Vref * ADC / 1024 

mV = Vref * ADC / 1023

mV = Vref * ( ADC / 1024 + 1/2 step )  ---- best guess


I see far more people use analog to check the rough position of a pot. They don't need to calculate volts but so many do!

If the analog read is to be processed with a fixed calculation or one from several then I would pre-calculate and table all the possibilities to look up. If the result is a float then think of the cycles saved. The result could as easily point to a function to run.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: larryd on Apr 01, 2018, 02:30 am
map(analogRead(A1),0,1023,0,5000)




Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: zoomx on Apr 01, 2018, 10:08 am
I know that this topic is most academic but in my mind a question arise.
If I apply only a linear fit using data 0,0V 1023,5V it seems that I have to use 5/1023  (Vref is 5V)
The datasheet (and some other reasons) states that instead I have to use 1024

My question is: maybe the ADC is not able to recognise well values just below Vref, 5V in my example? If I use the bucket example there is a possibility that the last bucket is bigger than the previous, it is large 2*5/1024 instead of 1*5/1024?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Apr 01, 2018, 10:24 am
If there is any message coming out of this Thread it is that it does not matter in practice whether you use 1023 or 1024.

Using 1024 has the advantage that the maths can be much faster (but not using map() ).

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: adwsystems on Apr 01, 2018, 06:27 pm
This may be a case of you can't have your cake and eat it too.

Based on the conclusions by CodingBadly, there are in fact 1024 steps and each step covers a voltage range. So what voltage value do you choose to ouptut?

Step (count) 0 0 to 4.8828124mV
Step (count) 1 4.8828125 to 9.765624
...
Step (count) 1023 4.9951171875 to 5.0 V

If you make step 0 output 0mV, and step 1 output 4.8828125mV, then step 1023 would output 4.9951171875V. The ADC would never return an output of 5V.

How do you justify step 0 output the "lower bound" of the range and the last step output the "upper bound" of the range?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GolamMostafa on Apr 01, 2018, 06:40 pm
Quote
Step 0 0 to 4.8828124mV
Step 1 4.8828125 to 9.765624
...
Step 1023 4.9951171875 to 5.0 V
First of all, we need to be agreed on the definition of what a step is.

A step is to be counted before making the step or after making the step?

In the quoted text, a step has been counted before making the step; so, there are 1023 steps.

If the steps were counted after making the steps,there would be 1024 steps?

Step 1 0 to 4.8828124mV
Step 2 4.8828125 to 9.765624
...
Step 1024 4.9951171875 to 5.0 V
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: adwsystems on Apr 01, 2018, 06:50 pm
I was not clear in my post that step number = count number. So I can't replay the second half.

If a step is "counted before" making the step there are still 1024 steps just as there are 1024 possible input values (counts, steps).

If you wrote the steps
Step (count) 0 0 to 4.8828124mV
Step (count) 1 4.8828125 to 9.765624
...
Step (count) 1023 4.9951171875 to 5.0 V
with if statements or a case statement there would be 1024 options.

The question is what voltage do you select to output for each step. If you want step 1023=5.0V, then step 0=4.88125mV, and the ADC would never output 0V.

How is it justified to output 0V for count=0 and 5.0V for count = 1023 when each count spans a range of voltages?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Smajdalf on Apr 01, 2018, 07:40 pm
Using 1024 has the advantage that the maths can be much faster (but not using map() ).
When you make the mistake to convert ADC result to float you are wasting so much time. The improvement gained by using /1024 instead of /1023 is small if any.
If you want to make the code fast you should use raw ADC result and avoid the division at all. If you want easily readable result you probably do not care about speed (presenting the result to the user will be more likely your bottleneck).
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Paulnaude01 on Apr 02, 2018, 01:46 pm
I didn't expect any replies on this topic, I thought it would be old news by now! If I could end this thread I would, but let me see if I can put it to rest with a few pictures, explaining what we are dealing with.


(http://www.paulnaude.com/wp-content/uploads/2018/04/1023-vs-1024-debate.png)

In this image above, I show the possible Analog input (readings) and two possible digital conversions: 4bit (for simplicity sake) and 10bit, what the Arduino uses.

When any analog Volt value is read, it is converted to digital by simply putting it in the bin directly below it.

To be clear, the bin number, is NOT a volt value. It's an ID number, just like a trash can in the street with your house number on it.

Let me work on the house and trash can analogy a bit:
Imagine your are the cleaner who needs to empty the bins, and you find a rare empty bottle of Wiskey in one of the bins. You will be able to tell from which house this comes by simply looking at the bin number, right?

But what if some of the houses shares a bin? How will know from which house it comes then?


(http://www.paulnaude.com/wp-content/uploads/2018/04/houses-with-bins2.png)

This example tries to show 3 things:

1) The more bins we have, the better change we have at estimating where the bottle comes from.
2) If we know there are four houses, but give them only 3 bins to put their trash in, we will be less sure where the bottle comes from.
3) If there are 10 people living in each house, we will still not know who's bottle it was, but with 1 bin per house, at least we can narrow it down a bit.

Now back to the measurements:

(http://www.paulnaude.com/wp-content/uploads/2018/04/1023-vs-1024-debate-zoom3.png)

In the example above (just looking for now at the 2 bit A2D conversion), the first two measurements (0.3 and 1.1) will land in bin 0, nothing in bin 1, 2.6 in bin 2, and 3.9, 4.1 and 4.9 in bin 3.

Now remember, once we have the digital (bin) numbers, we know nothing anymore of the original values.

All we know now is this:

(http://www.paulnaude.com/wp-content/uploads/2018/04/table1.png)

NB: No matter what you do, once the value is converted to digital, there is now way of going accurately back to the original analog value (only more or less inaccurate ways)!

The same holds true for the 10bit (1024 bins) system. Your Arduino reports a bin number, not a voltage.

Since there are 1024 bins, that is the number to use, but, if you argue that 1023 is more convenient, since it can produce both 0 and 5Volt output values in your display, there is some merit to your madness, since lets be honest, the error is really small:

5/1024 = 4.883mV
5/1023 = 4.888mV

a difference of 4.77microVolt (0.00477mV), much smaller than the expected fluctuations in the reference voltage in any case.

But just understand, that when using 1023, although it might be convenient, adds to the inaccuracy since more measurements will be reported as 0 than when using 1024, as well as more values that will be reported as being a full 5V when in fact it should have been less.

So make your bed, and sleep in it!

Good night.
Paul











Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: TolpuddleSartre on Apr 02, 2018, 01:55 pm
Or, put more succinctly, you could RTFM, and see what the device's own manufacturer recommends..

Just a thought.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Apr 02, 2018, 03:18 pm
Or, put more succinctly, you could RTFM, and see what the device's own manufacturer recommends..
Except that Atmel was not brave enough to explain (in the Atmega 328 datasheet at least) how to convert an ADC reading back to the voltage from which the reading was obtained.

I don't think any of the subsequent comments have improved on @CodingBadly's Reply #75

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: adwsystems on Apr 02, 2018, 03:49 pm
3) If there are 10 people living in each house, we will still not know who's bottle it was, but with 1 bin per house, at least we can narrow it down a bit.
That is one of the unanswered questions. 10 people in one house, but you have to pick one person to report. Which person? The oldest, youngest, shortest, tallest? Whichever person you choose must be the same person for all the houses. You can't pick the youngest for the first house and the oldest for the last.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: TolpuddleSartre on Apr 02, 2018, 03:54 pm
Except that Atmel was not brave enough to explain (in the Atmega 328 datasheet at least) how to convert an ADC reading back to the voltage from which the reading was obtained.

I don't think any of the subsequent comments have improved on @CodingBadly's Reply #75

...R
No, really, really RTFM. As in, download the datasheet, and read it. 
Quote
The ADC converts an analog input voltage to a 10-bit digital value through successive approximation. The minimum value represents GND and the maximum value represents the voltage on the AREF pin minus 1 LSB.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Apr 02, 2018, 06:25 pm
No, really, really RTFM. As in, download the datasheet, and read it. 
I have had the datasheet for years. It is my constant bedtime companion.

It clearly explains how it converts voltages to ADC values. It does NOT explain how to convert ADC values to voltages.

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: zoomx on Apr 02, 2018, 09:01 pm
Maybe is written here
Quote
The minimum value represents GND and the maximum value represents the voltage on the AREF pin minus 1 LSB.
so 1023 represent aref-(aref/1024)?
So, as I wrote in #87, the last bucket is larger than the previous?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Coding Badly on Apr 02, 2018, 09:04 pm
I didn't expect any replies on this topic...
Surprise!

In this image above...
The last bucket is bounded by the voltage limits of the device (VCC + 0.5 for AVR processors).  If VREF is 1.1 and VCC is 5.0 then the last bucket is 1.1 - cut off to 5.5 for an AVR processor.

a difference of 4.77microVolt (0.00477mV), much smaller than the expected fluctuations in the reference voltage in any case.
That may be true for your references but it is not true for mine.

I don't think any of the subsequent comments have improved on @CodingBadly's Reply #75
Thanks!

So, as I wrote in #87, the last bucket is larger than the previous?
The final bucket is larger than all the others.  Correct.  It is bounded by the physical limits of the device.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: adwsystems on Apr 02, 2018, 09:11 pm
No matter whether you use 1023 or 1024 the bucket size is the same for each step. 5/1023 or 5/1024.

If you use 1023, then voltage value returned has a progressive shift from returning the minimum value to returning the maximum value. Count = 0 returns 0V for voltages from 0 V to just short of 4.887586mV (5/1023), but count = 1023 returns a value of 5V, and is returned for any voltage from 4.995112 through 5V.

If you follow the datasheet, then each bucket is 5/1024 volts and count = 0 returns 0 V for input voltage up to 4.8828124mV and count = 1023 returns 4.995117V for all input voltages from there to 5V. Using 1024, 5V is never returned; that is what the datasheet is saying.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Smajdalf on Apr 02, 2018, 09:48 pm
That may be true for your references but it is not true for mine.
This just shows how incompetent you are for this discussion. Using such precise reference for Arduino's poor ADC (typical error is 2 LSB!) is wasteful. You could get better resolution by using worse reference and better ADC for the same money. Or you can get the same resolution much cheaper when you use worse reference.

It clearly explains how it converts voltages to ADC values. It does NOT explain how to convert ADC values to voltages.
It does not say that because the whole idea of converting ADC result to voltage is silly. It gives hints how the ADC value is obtained. It is trivial to reverse the process. I don't understand why there is so much emotions about it.
No matter whether you use 1023 or 1024 the bucket size is the same for each step. 5/1023 or 5/1024.
The bucket size is always Vref/1024.

If you use 1023, then voltage value returned has a progressive shift from returning the minimum value to returning the maximum value. Count = 0 returns 0V for voltages from 0 V to just short of 4.887586mV (5/1023), but count = 1023 returns a value of 5V, and is returned for any voltage from 4.995112 through 5V.
Using 1023 divider you read 0V from 0V to 5/1024V. You read 5/1023V for input from 5/1024V to 10/1024V etc. But you are right the returned value is "drifting" in the correct range.

If you follow the datasheet, then each bucket is 5/1024 volts and count = 0 returns 0 V for input voltage up to 4.8828124mV and count = 1023 returns 4.995117mV for all input voltages from there to 5V. Using 1024, 5V is never returned; that is what the datasheet is saying.
The datasheet does not say you should use 1024 and so you cannot get 5V! It just says 1023 represents voltage higher than Vref-1LSB. It is up to you what value you assign to this range.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: adwsystems on Apr 02, 2018, 10:42 pm
Using 1023 divider you read 0V from 0V to 5/1024V. You read 5/1023V for input from 5/1024V to 10/1024V etc. But you are right the returned value is "drifting" in the correct range.
Not following you here. Where did you get 10 volts from?

I'm only looking at the math.
count 0 = 0/1023*5 = 0
count 1 = 1/1023*5 = 4.887586mV
count 1023 = 1023/1023 * 5 = 5V

As you can see count=0 returns the low end of the first range but count=1023 returns the top end of the last range. From that it can be concluded the values in the middle are on a sliding scale. Such that at count=512 the voltage computes to 512/1023*5 = 2.5024437V; just a smidgen higher than 512/1024*5=2.5V but not quite to the upper bound value.

The datasheet does not say you should use 1024 and so you cannot get 5V! It just says 1023 represents voltage higher than Vref-1LSB. It is up to you what value you assign to this range.
The value assigned to the voltage range where count = 1023 is Vref-1LSB.

The conversion formula in the datasheet is the count divided by the count range times Vref.

count 0 = 0/1024*5 = 0
count 1 = 1/1024*5 = 4.8828124mV
count 1023 = 1023/1024 * 5 = 4.995117V (which is exactly what the datasheet says)

Unless you put an if statement to check for 1023 and force it to return 5.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Coding Badly on Apr 02, 2018, 10:47 pm
This just shows how incompetent you are for this discussion.
Either that or you grossly misunderstood the message.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Apr 02, 2018, 11:47 pm
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
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Apr 03, 2018, 01:52 am
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!


Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: zoomx on Apr 03, 2018, 11:13 am
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.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Apr 03, 2018, 08:07 pm
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.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: J-M-L on Apr 15, 2018, 03:06 pm
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
(http://forum.arduino.cc/index.php?action=dlattach;topic=537413.0;attach=253570)

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...
(http://forum.arduino.cc/index.php?action=dlattach;topic=537413.0;attach=253572)

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


Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Robin2 on Apr 15, 2018, 05:20 pm
This is how I see this - may be we will get everyone to agree
I reckon we had got there in Reply #75 (http://forum.arduino.cc/index.php?topic=537413.msg3670409#msg3670409) and my summary in Reply #79.

Here's to the next 30 replies :)

...R
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: J-M-L on Apr 15, 2018, 05:26 pm
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

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Apr 15, 2018, 06:44 pm
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!
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Coding Badly on Apr 15, 2018, 09:10 pm
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

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: J-M-L on Apr 15, 2018, 09:40 pm
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 :)
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Apr 16, 2018, 02:44 am
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.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Apr 16, 2018, 02:46 am
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?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: zoomx on Apr 16, 2018, 03:36 pm
(http://forum.arduino.cc/index.php?action=dlattach;topic=537413.0;attach=253570)


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.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Apr 16, 2018, 07:27 pm
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.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: J-M-L on Apr 16, 2018, 11:01 pm
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)
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: zoomx on Apr 17, 2018, 09:08 am
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.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: J-M-L on Apr 17, 2018, 10:25 am
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
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: zoomx on Apr 17, 2018, 12:00 pm
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.





Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: J-M-L on Apr 17, 2018, 01:02 pm
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 (https://en.wikipedia.org/wiki/Surjective_function) but non bijective (https://en.wikipedia.org/wiki/Bijection)  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.

(http://forum.arduino.cc/index.php?action=dlattach;topic=537413.0;attach=253798)

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

(http://forum.arduino.cc/index.php?action=dlattach;topic=537413.0;attach=253800)


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:


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)


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) * Vref / 1023 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.


Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on Apr 17, 2018, 03:55 pm
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?
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Southpark on May 07, 2018, 06:59 am
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.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: J-M-L on May 07, 2018, 09:11 am
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
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Southpark on May 07, 2018, 10:48 am
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.

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.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: J-M-L on May 07, 2018, 12:02 pm
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.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: Southpark on May 07, 2018, 12:07 pm
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.
Let's take a look at what successive approximation A/D (including the D/A side of it) generally involves. Also, the hardware has a voltage resolution of 5/1024 volt..... not 5/1023 volt (for the 5V, 10 bit system). This is assuming the general internal D to A converter (inside the A to D) is based on a general R-2R system. The least significant bit being a '1' (and all the other bits being zero) will correspond to 5/1024 volt, not 5/1023.

If it's a 5V, 2 bit system, then the voltage resolution will be 5/4 volt. The very first test (of two tests) for a 2-bit system is where the reference voltage is first set to exactly half-way between 0 and 5, which is 2.5 volt. This corresponds to temporarily setting of the MSB to '1' (and the LSB remains '0'. This means the reference register is temporarily '10', or decimal 2, which converts (via a D/A) to 2*[5/4) = 2.5V. The first test will be to see if the input voltage is greater-than or equal-to that 2.5 level. If it is, then the MSB will permanently become a '1'. Then the system will know that the voltage will be in the top-half of the range (ie. above 2.5 volt and below 5 volt). And the system then proceeds to home in on the best binary representation for the input voltage....based on the 2-bit system. For the second (and final test), if the MSB is already determined to be '1', then the 2 bit register is then temporarily made to be '11' or decimal '3', which sets the new internal reference voltage (care of the internal D/A) to be 3 * 5/4 = 3.75 volt (which is exactly half-way between 2.5 and 5 volt). The comparison between the input voltage and that 3.75 volt will decide on the outcome of the LSB.

The general D/A circuit allows these internal 'half-voltages' to be generated easily. That is, voltages like 0, 1.25, 2.5, 3.75.

That other set of values you mentioned.....  ie. [(0, 1.66, 3.33, 5)] ..... the 1.66 is actually 1.666666666666666666 etc. and the 3.33 is actually 3.33333333333333333 .... which isn't very good, because they just keep going, right? Maybe for a mathematical exercise it is all ok. But from a hardware implementation perspective, that's not what they do (or want to do), right?

I'm thinking that.... the main thing is ...the question of ... does 'divide by 1023' (for 10 bit) represent the system that the manufacturer aimed to achieve? I'm thinking... no, it does not. So if it doesn't, then it'll be best to just teach it as it should be ..... divide by 1024.
Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: GoForSmoke on May 07, 2018, 06:50 pm
Do that in the online calculator and see how many decimal places the result has.
Do the math and see how quickly the digits reach insignificance in the face of noise alone.
Marvel at the number of places that a calculator can show that have already been shown to not matter.....

Quote
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).
The way it really works is all that and error. You forgot the error that is bigger than your point.

Oversampling is how ADC accuracy gets improved. That or get a better ADC but by 12 bits the wiring gets real important.

It's not like there is only ONE WAY to treat analog reads. If you're a stickler for accuracy then first divide by 4 and then get upset over the divide by 255 or 256 because then the difference will be more significant than the noise. Sure you lose precision but that's not accuracy.

Title: Re: Dividing by 1023 or 1024? The final verdict on analogRead
Post by: J-M-L on May 07, 2018, 11:06 pm
That other set of values you mentioned.....  ie. [(0, 1.66, 3.33, 5)] ..... the 1.66 is actually 1.666666666666666666 etc. and the 3.33 is actually 3.33333333333333333 .... which isn't very good, because they just keep going, right? Maybe for a mathematical exercise it is all ok. But from a hardware implementation perspective, that's not what they do (or want to do), right?
This is not the point. The point is that if you know the Digital value is X what could be a likely original Analog voltage that generated that X?

What I am saying is if X=1 then 1.66 or 1.666666666666666 or 1.5  or 2 are as likely good guesses as 1.25 (in your example). the rest is litterature...