Go Down

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

Paulnaude01

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!

(deleted)

TolpuddleSartre

#2
Mar 25, 2018, 04:48 pmLast Edit: Mar 25, 2018, 04:52 pm by TolpuddleSartre
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."

Robin2

And you can divide by 1024 with >>10

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

TolpuddleSartre

...which is probably what the compiler does for you.

robtillaart

... unless you are using float
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Robin2

...which is probably what the compiler does for you.
I don't think it converts divisions by powers of 2 into right-shifts

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

robtillaart

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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

TolpuddleSartre

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.

Robin2

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

GolamMostafa

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

ADC = ((11 1111 1111)/5)*VDC = (1023/5)*VDC

TolpuddleSartre

#11
Mar 26, 2018, 08:56 pmLast Edit: Mar 26, 2018, 10:05 pm by TolpuddleSartre
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 !

larryd

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.

No technical PMs.
If you need clarification, ask for help.

TolpuddleSartre

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.

GolamMostafa

#14
Mar 27, 2018, 06:20 amLast Edit: Mar 27, 2018, 10:00 pm by GolamMostafa
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?

Go Up