Go Down

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

zoomx

This way the last segment has not the same length of the others, for sure is longer, and you don't know how is long because you can get the same value, 1023, also for 5.5V, maybe more, maybe less.
In other words 1023 mean a value that is bigger than Vref-Vref/1024 (not Vref!) but you don't know how many is bigger.


J-M-L

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
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

zoomx

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.






J-M-L

Your reasoning is "flawed" because there is no angle and there is no continuous transfer function from voltage to digital value

The math problem to solve is that you are looking at the reverse function of a surjective but non bijective  piecewise affine function. (the steps)

You really lost information when the ADC converted the input Voltage into a digital representation, and the amount of information you lot is directly related to the number of bits (and hardware quality) of the representation and there is no way to get back to the original value.

All you can do is take an estimate, based on simple principle that it has to be a likely original value (ie within the interval). There is no good or bad estimate as long as they sit there in the interval. You can add extra personal constraints like "I want to see a nice 5V when the ADC tells me 1023" or "I want to minimize the max average error" or "I want a constant max error"... Those are arbitrary choices you can impose to yourself.

that means basically that any of the affine function crossing every "step" is legit to consider.



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




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.


Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

GoForSmoke

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?
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up