1023 value in reading analog voltage

Hi,

Pardon me from being such a noob on this field. Just wanna ask regarding on the value of voltage when using analogRead. As mentioned on this article http://arduino.cc/en/Tutorial/ReadAnalogVoltage. The maximum voltage 5volts that will be out on the potentiometer will represent a value of 1023 on the MC

What is the basis of this value or how did it acquire this number?

Thank you.

Its the maximum value of a 10 bit unsigned integer. ADCs and DACs always use a power of two.

Hi Mark!

thank you for the reply. Uhmm if it is 10 bit should it be 1024?

neil03:
Uhmm if it is 10 bit should it be 1024?

The divisor (1023.0) in that tutorial is wrong. The correct value is 1024.0

float voltage= sensorValue * (5.0 / 1024.0);

The maximum value returned by the analog-to-digital converter (1023) is correct.

0 to 1023 = 1024 values. 0x3FF = 0b11 1111 1111 = 1023 decimal = 10 bits

To be precise the best estimate of input voltage is

float voltage= (sensorValue + 0.5) * 5.0 / 1024.0 ;

Since the 0 code covers 0 to 4.9mV, midpoint being 5.0/2048

[quote author=Coding Badly link=topic=252276.msg1789197#msg1789197 date=1404267824] The divisor (1023.0) in that tutorial is wrong. The correct value is 1024.0... [/quote]

Are you sure? 1023 seems correct to me.

Are you sure? 1023 seems correct to me.

Seems right to me, too. The maximum reading is 1023, which corresponds to 5.0 (nominally) volts. So, reading * 5.0 / 1023 would, when reading is 1023, result in 5.0. reading * 5.0 / 1024 would result in 4.995.

PaulS:

Are you sure? 1023 seems correct to me.

Seems right to me, too. The maximum reading is 1023, which corresponds to 5.0 (nominally) volts. So, reading * 5.0 / 1023 would, when reading is 1023, result in 5.0. reading * 5.0 / 1024 would result in 4.995.

Section 23.7 of the datasheet explicitly says 1024.

All I know is that if I put more than 5 volts into the analog port, I get a value of 1023.

No question that the analogRead() returns ADC count values ranging from 0 to 1023, so 1024 possible values. Now if one should use 1023 or 1024 as the scaling value in any remapping of the value is a topic that always seems to keep coming up, with proponents for both values that claim it's a no brainer as to the correct value. I have no clue as to which is correct, but am surprised that it doesn't have a definitive mathematical provable answer.

Come on software gurus, put this topic to bed once and for all. ;)

Coming at it from a hardware guy I have a more practical 'answer', it doesn't really matter at all because as far as it effecting the accuracy of the remapping calculation as the 0.1% difference probably pails in comparison of the raw accuracy specification of the ADC and basing the calculation assumption that the Avcc reference voltage being exactly +5.000000 vdc, which is probably never the case.

After a bit of head scratching, I have come to agree with Coding Badly. 1024 is the correct value.

As far as I understand it, the issue is that the value of 1023 represents a range of voltage values so the question is: what value within that range do you want the calculation to return? If you want the lowest value, dividing by 1024 gives you that. If you want the highest value, adding one and dividing by 1024 gives you that. If you want the median value, adding 0.5 and dividing by 1024 gives you that.

Come on software gurus, put this topic to bed once and for all. smiley-wink

No head-scratching required.

It’s 1024.

We could go into a long argument, with already-posted reductio ad absurdum arguments, but there’s a much simpler, compelling argument - Ask yourself - Why else would NatSemi and all the others invest all that money in producing voltage references with strange, non-integer outputs like 1.024V, 2.048V etc?

0b11 1111 1111 = 1023 decimal = 10 bits

Is correct. 0 - 1023 range. 1024 would be 0b0100 0000 0000 or 11 bits. Count them. If you think it's 1024 then provide documentation link.

MarkT: To be precise the best estimate of input voltage is...

What I remember is that the overall error is the same. The end result being that adding an offset does not accomplish anything mathematically useful. In some cases adding an offset would wreak havoc (e.g. certain PID strategies).

A much better way to improve the estimate is to buy a better ADC. (Or over-sample.)

steinie44: If you think it's 1024 then provide documentation link.

Reply #8

If you think it's 1024 then provide documentation link.

Reply #8

That is not a link Hyperlink please.

Yep, the topic that will never die. ;)

steinie44: Hyperlink please.

http://lmgtfy.com/?q=atmega328p+datasheet

23.7 ADC Conversion Result
After the conversion is complete (ADIF is high
), the conversion result can be found in the ADC
Result Registers (ADCL, ADCH).
For single ended conversion, the result is
where V
IN
is the voltage on the selected input pin and V
REF
the selected voltage reference (see
Table 23-3 on page 262
and
Table 23-4 on page 263
). 0x000 represents analog ground, and
0x3FF represents the selected reference voltage minus one LSB

0x3FF represents the selected reference voltage ---->minus one LSB<----
1024 - 1 = 1023