My goal is to measure the voltage of a battery (24V when full) on an Arduino Nano.
For testing, I connected the system to a PSU, which delivers constant 12V (multimeter measures 12.08V).
To bring down the voltage to a safe range for the Arduino I made a voltage divider using a 20kohm (R2) and a 94kohm (R1) resistor. So when 24V is attached, the voltage on the pin is going to be V_pin=24V*R2/(R1+R2)=4.21V < 5V.
Now the program gives me a total voltage (voltageIN) of 12.91V, which is higher than 12V.
The Arduino also measures a voltage after the divider (voltageOUT) of 2.27V.
These values are clearly wrong, since I measured with a multimeter a total voltage of 12.08V and after the divider a voltage of 2.08V.
I have also tried using the other analog pins, but the results are the same.
If somebody has an idea what could be the problem, any help is appreciated.
Thanks in advance.
We like to see the sketch that causes the problem. It seems that you have been editing the sketch before putting it here.
The Arduino board does not know a voltage, it uses a known reference voltage and then measures the analog input relative to the reference voltage.
You could use the 7805 as reference (not very accurate), and power the Arduino Nano via the 5V pin. Or you could use the internal voltage reference of 1.1V.
It does not compile for me. I doubt if you are uploading a new compiled sketch.
Could you try the normal Arduino IDE ? Which operating system do you use ?
The actual voltage can be from 1.0 to 1.2 Volt, and should be measured for each individual Arduino board. Select the internal 1.1V, then do a analogRead() to make it active, then measure the AREF pin, and then use that voltage in the sketch.
Suppose R2 is 10kΩ, then you can make R1 330kΩ for a voltage up to 36V.
I prefer a large margin to measure more above 24V. If you can measure up to 25V, and the battery turns bad and is 26V while charging, then you can not measure it.
The accuracy will almost be the same, you won't notice it when taking the average of a number of samples.
[UPDATE] I see that you changed the sketch, now it compiles
If you still have problems after sorting out the reference voltage, read the ATmega328P data sheet section on the ADC - particularly the input impedance section. Your divider resistor values may be a bit high. If you really want to use high values you need a capacitor from the junction to ground to hold the voltage and then read it at a rate much lower than the time constant for the RC of the divider.
Please no, your "reference" is not 4.65V, it varies and it is sometimes 4.65V (most of the time it is not).
You need to know the "reference" voltage, to be able to measure something.
The LM1117 has a voltage drop of 1.1V at 100mA. The LM1117 voltage regulator is between VIN and 5V pin.
There are two options: Be sure that the 5V pin is always close to 5V or use the internal voltage reference.
A third option is to use a external voltage reference.
Stop. If that first schematic is correct, you've fed the Arduino from the 7805 to Vin. This is not correct, and at best your Arduino will function erratically. Vin must be >= 7.5VDC. When using a 5V supply, wire it to the 5V pin, which you may consider to be "bidirectional".
Let us know if things work better this way.
I'm now powering the Arduino over the 5V pin and have now a constant 5V between the 5V and GND pin. Now, the voltage reading works very well using 5V as reference.