I have an Arduino Uno powered from USB. I am trying to get the voltage of a 12V battery through analog pin 0.
I have 3 x 10K resistors in series (+12V --- R1 --- R2 --- R3 --- GND) . I needed 3 resistors for the voltage divider, as I don't have any larger than 10K at the moment.
Connecting the multimeter to the battery terminals reads 12.81V and between R3 it reads 4.23V, which is around the expected value.
When I connect a wire between R2 and R3 to analogue pin 0 and read the value using analogRead(0)., It returns 921. This appears higher than I expected which is 866 (866 * 5/1023 = 4.23).
What could be wrong?
By default, the analog reference is the 5V supply voltage. Measure the real supply voltage and substitute that for the 5 in the 5/1024 and see if it is closer. With USB I would expect the supply is lower than 5.00V.
The USB spec allows the USB voltage to be between 4.75 and 5.25v.
A reading of 921 on 4.23v suggests your VCC (5V) is only 4.7v.
You aren't putting 5V into Vin or the Barrel Jack are you?
You might consider dividing the voltage down further and using the internal 1.1V reference. Otherwise, you will have to recalibrate if you move the Arduino to a different power supply or to a different computer, or even just a different USB port.
In any case, Aref and AVcc should each have a 0.01uF or 0.1uF bypass capacitor to ground with -very- short wires between.
Just for giggles, did you connect the negative battery terminal to Arduino ground? If you didn't, then the reading could float anywhere between 0 and 1023.
Most likely, the 5 V at the Arduino isn't really 5 V. Use a 12 V supply at the power jack and check it again.
What exactly are your resistor values and the input voltage Vcc?
You say you don't have anything larger than 10k so would it be a corect assumption to believe you are using 3 x 10k, aiming for a 20k/10k divider?
To calculate the analog input value accurately you need to know the actual Vcc and account for all resistance in the divider, including the pin input impedance.
To expound upon what Tack is saying, 10K 'marked' resistors don't always measure 10K Ohms (tolerance and temp coeff). But, Poly and James are most likely the ones with the most appropriate responses (IMHO).