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

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

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.

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

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 V_{REF} = Full Scale) at the input of ADC ----> ADC produces 11 1111 1111
**3.** V_{DC} (unknown voltage) at the input of ADC -----> ADC produces ? (say, ADC)

ADC = ((11 1111 1111)/5)*V_{DC} = (1023/5)*V_{DC}

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

What are these two known points

So then you go and list three points ! :D

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.

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.

So then you go and list three points !

A linear system like the ADC of the ATmega328 requires only two point responses for calibration?

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.

GoForSmoke: 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?

Whandall: Doing things deliberately wrong/incorrect would make me feel dumb, how about you?

Can I give you a hug? Would that be awkward?

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

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

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.

Now that my interest has been awoken sufficiently to read the Atmega328 datasheet ...

The data sheet clearly states what is in Reply #2
ADC = (V_{IN} * 1024) / V_{REF}

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 = V_{IN} * number of intervals / V_{REF}

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

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

Robin2: Now that my interest has been awoken sufficiently to read the Atmega328 datasheet ...

The data sheet clearly states what is in Reply #2 ADC = (V

_{IN}* 1024) / V_{REF}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 = V

_{IN}* number of intervals / V_{REF}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,

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

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

This all needs to be in the 'Introductory tutorials' forum. :(

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.

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