I am very inexperienced so please forgive my failings, that being said I attempted to read a voltage from the Arduino and the values I am getting don't make sense. I tried using input and input pullup (believing pullup would merely be the inverted response 0V = 1023, 5V=0), but something else is wrong with my program I am also measuring temperature, but that part appears to work. There is something funny happening. I need to log both temperature of 3 sensors and voltages of 6 separate devices. Circuit is 5V sent through a 4.7K resistor to digital pin 2, with three DS18B20 sensors (working), when the analog outputs are in open circuit I would expect to get near 0 or 1023, but the values are all over the place...

when the analog outputs are in open circuit I would expect to get near 0 or 1023, but the values are all over the place...

What you are seeing is the result of reading a "floating" pin. The input impedance of an analog input is very high (on the order of 100 megohms) so that it will pick up all sorts of noise. So the ADC reading can be any value between 0 and 1023. This is normal behavior of a floating pin.

groundFungus:
What you are seeing is the result of reading a "floating" pin. The input impedance of an analog input is very high (on the order of 100 megohms) so that it will pick up all sorts of noise. So the ADC reading can be any value between 0 and 1023. This is normal behavior of a floating pin.

Thanks for the help, okay that makes sense, and I thought as much might happen. The bit I didn't expect was when I either grounded the pin or applied a small voltage (3.3v) to the pin (connecting through GND naturally) it did not read the correct value, and if it did it would throw off the value of another analog pin.

Thank you I did find that useful, after reading the page, I was curious about many things

If we use the onboard voltage regulator instead of the USB source do we effectively reduce some of the noise? I'm not sure how much variance there is on the internal voltage regulator, would it be the same (%) as you stated with 1.1 volts (10%), that seems like a really large range, is that true?

Can we use a zener diode as a reference to increase our precision? I have seen these as tight as 2% for pennies...

Actually, it is not useful and continues to muddy the water. This material has been debated endlessly in this forum, and neither value in the code line above is correct. Not the 5.0, and not the 1023.

For accurate results the "5.0" must be replaced by a calibrated or measured value, and the correct divisor is 1024.

Don't confuse accuracy with stability.
The graph in the link shows accuracy of e.g. the 1.1volt Aref (not relevant) instead of stability.
Accuracy can be (must be) calibrated out in code.
And if you use a voltage divider, to measure a higher voltage, then you must calibrate (for resistor values) anyway.
Even with a 4.096volt reference.
For voltages (not ratios), it could be better to forget about Arduino's A/D, and use an external A/D like the ADS1115.
Leo..

Secondly, while there is continual disagreement about whether it should be 1023 or 1024 - which depends on how the adc is scaled - its really irrelevant as its within the limit of the accuracy and resolution of the adc.

mfisc - yes the on-board references are really not accurate although often better than the quoted figures. You can not easily get good precise voltage references from a zener due to the slope resistance and temperature sensitivity.

Talking about references - I taught electronics at university for many years, and hold a masters degree in physics and am a Member of the institute of Physics. Tehe info on my site can be (and mostly already is) supported by references from reliable sites (eg Analog Devices)

Also wrong, as are many of the "official" Arduino tutorials.

while there is continual disagreement about whether it should be 1023 or 1024

Only among the mathematically unsophisticated. There is certainly no confusion among the engineers at ATMEL who designed and tested the ADC, then wrote the ATmega328 data sheet:

I taught electronics at university for many years, and hold a masters degree in physics and am a Member of the institute of Physics

Now I am REALLY surprised. You should understand the difference between measurement intervals and endpoints, but you don't. Have you not yet learned that the manufacturer's data sheet is usually a more reliable reference than a random web page?

We, the forum members, have no editing control over the tutorial pages on this site.
There are many errors on these pages.
As an example, the shift register page had a serious hardware error. It took nine years to correct half of it.

Basically, don't trust any page on the internet that readers are not allowed to correct.
Or not allowed to correct without a "must sign in" barrier.
Leo..

The largest numerical value that can be produced is 1023. If you get that when you apply a 5V source with suitably low source resistance then the scaling factor is 5V ==1023. You also need to look at the zero to ensure it is scaled properly.

If you get 1023 when a slightly lower voltage is applied (depending on where the transition between 0 and 1 or 1022 & 1023 occurs) then use 1024.

Perhaps thats clearer if you refer to the diagrams here

However this is all irrelevant unless you have a suitable source to measure.

If you are using a resistive divider you need resistors matched to better than 0.1%.

Also you will note from the data sheet an integral non-linearity of 0.5 LSB and +_2LSB absolute accuracy.

Anyway, for accurate results the whole measurement circuit needs to be calibrated.

You still don't understand how a successive approximation ADC works. It rounds down.

If the ADC reference is exactly 5.0V, then a reading of 1023 ideally means that the ADC input voltage is greater than 4.9951V. 1023 points to a voltage interval, not a value.

With some study, you should be able to catch up. Please correct your web page so that you don't further confuse the beginners, and get on with your life.

Good to understand the theory behind it, and to use the 'right' divisor in your maths lines,
but none of this really matters in practice.
The returned A/D value depends on two things. Input voltage and reference voltage.
Input voltage could also depend on voltage divider resistor values (precision/drift).
All of the above could easilly be off by 5% or more.
An example is the ~4.6volt (not 5volt) default Aref a Nano has on USB power.
That results in a ~9% higher output of your code, despite of expensive 0.1% resistors.
A precision voltage reference and a divider could be off by more than 1%. That's about 10 A/D values.
If you want good results, then use stable (not necessarily accurate) parts, and calibrate the final product.
Leo..

Is it so simple? consider a 1 bit adc scaled 0-1V So 0=0V and 1=1V right?

So to convert our reading back we take the digital value 1, divide by the number of intervals (2) and get 0.5. Clearly that is wrong

So should we divide the reading by (the number of intervals -1) =1?
then 0=0v and 1=1V

Thats also wrong.

There are some major misconceptions here, and they apply just the same to any quantized voltage domain.

In fact, to convert our reading from the 1 bit adc all we can say is that

a 0 indicates a voltage between minus infinity and 1,
and a 1 indicates a voltage between 0 and plus infinity.

no not 0.5V because we can not say at what exact voltage the change from a 0 to a 1 occurs. we need to allow for a +- 0.5 lsb error.

A zero reading on your arduino adc corresponds to a voltage of + a little bit to - infinity.
a reading of 1023 indicates around 5v to plus infinity.

UNLESS you can guarantee that the input voltage to the adc can never be less than 0V or more than 5V*1024/1023 exactly - which adds two more interval boundaries.

To exclude these possibilities readings of 0 or 0h3ff should be excluded from conversion.

Thanks for your comments wawa.

and no, jr, very simplistic. adc's dont round down. or up. and remember the +_0.5 lsb applies throughout the conversion range. to make the above easier for you the fence does not have posts at both ends, just unsupported wire to infinity.

According to the school that so adamantly adheres to the "divide by 1023" fallacy, you should divide by (number of intervals)-1, or in the case of a 1-bit ADC, divide by zero.

LOL I think you guys should first figure out the difference between number of steps the ADC can make, the number of states it can be in, and what each state represents.