Problem with measuring voltage on analog pin

Hello

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.

Here's an overview of the schematics.

Now to measure the voltage on the analog pin, I use this code:

int voltageSensorPin = A0;
int voltageSensorValue = 0;

float resistor1 = 94000;
float resistor2 = 20000;
float voltageOUT = 0.0;
float voltageIN = 0.0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  voltageSensorValue = analogRead(voltageSensorPin);
  voltageOUT = voltageSensorValue * 5.0 / 1024.0;
  voltageIN = voltageOUT * (resistor1 + resistor2) / resistor2;
  Serial.println(voltageIN);
  delay(1000);
}

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.

Welcome to the forum.

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.

Thanks for your response.

How would I implement this in my code (using the internal voltage as reference)? Do I have to tell the Arduino, what reference I'm taking?

Also this is the original sketch I'm using. I compile and upload it with the arduino-cli toolset.

Thanks

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 ?

Everything is online, the schematics, the source code, the documentation.
This is the analogReference(): https://www.arduino.cc/reference/en/language/functions/analog-io/analogreference/
Read the warning, keep AREF disconnected if you select a internal voltage reference.

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

Use 2 known voltages and calibrate, instead of using the voltage divider formula.

please measure voltage between pin "5v" and GND

1 Like

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.

Thanks for all the suggestions.

I measured the voltage between the 5V and GND pin and got 4.65V. So I adjusted the formula to

voltageOUT = voltageSensorValue * 4.65 / 1024.0

After that, I get an accurate voltage reading.

so the problem solved?

Yes

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.

@eliashauksson Is that really a 7805 ? Does it have capacitors ?

Alright, thanks for the warnings.

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.

PS: @Koepel yes it's a L7805CV

Ok. Now, what remaining problem do you have? Sorry, not re-reading the thread to see where we're at.

Nothing remaining. The problem is solved now.

Then please flag it as solved, marking the appropriate reply that resolved the issue.
Thank you!
C

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.