Measuring 12 Volts

I need to measure 12v from a battery, i am using a voltage divider of 10k and 5k, so 4v=12v

the data collected is something like this when using default arduino reference (5v)

Two problems, first that there is too much variation in the voltages read, how can this be corrected

3.34
3.35
3.26
3.32
3.38
3.37
3.39
3.37
3.41
3.41
3.4
3.38
3.25
3.35
3.19
3.31
3.24
3.38
3.27
3.24
3.4
3.38
3.23
3.34
3.37
3.21
3.32
3.32
3.37
3.36

and other is that if i multiply it with factor of 3 the voltage i get is like this

10.02
10.05
9.78
9.96
10.14
10.11
10.17
10.11
10.23
10.23
10.2
10.14
9.75
10.05
9.57
9.93
9.72
10.14
9.81
9.72
10.2
10.14
9.69
10.02
10.11
9.63
9.96
9.96
10.11
10.08

which is significantly lower when measured with a meter, meter dont give a reading less than 11V !!

Code used is

  float voltsense = analogRead(voltpin);
   float voltage = 5*voltsense/1024;
  
  //Logging values to LOG.CSV on memory card
  File logFile = SD.open("LOG.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.print(voltage);
   
//  DEBUG
  Serial.print(voltage);
  logFile.close();  
}

Vout = Vin * 5K/(5K+10K) = Vin * 0.3333

Perhaps your resistors are off? Put a meter on them see if actually 5K and 10K. The 12V too, see if actually 12V.

Resistor values are correct with ~1% error !

Battery voltage using multimeter is around 11.18Volts

This is giving a different value !

But you said you only had 11.18 going in.
So Vout = 3.73V.
Analog read should be 3.73 / (5/1023) = 763.

Then 763*5/1023 = 3.729

Is your 5v sources to the Arduino really 5V? That would throw the results off also.

The source Voltage might be less as i am powering it through USB

What if i am drawing current from battery and battery voltage is dropping constantly ?

This is what i am using right now

float voltsense = analogRead(voltpin);
float voltage = 5*voltsense/1024;
Serial.print(voltage);

Measure your USB.
If you don't have 5V then this equation will be off:

float voltage = 5*voltsense/1024;

Also it should be 1023. 0b11 1111 1111 = 1023 which is the max reading you can get.

Attached is the data collected, there is too much fluctuation in the readings.

Can i smooth it out using a capacitor? what should be the value of capacitor?

Or is it better to use an averaging function like using following in the loop

voltage = (voltage + prevVoltage)/2;
current = (current + prevCurrent)/2;

?

LOG.xlsx (34.6 KB)

I agree with CR. The real issue is that you are using nominal values for all of your assumptions, which isn't the case. Small errors in the resistors (you didn't say what the actual measured value was), battery voltage, and VCC supply will result in your calculation being off.

Also, make sure you are using the correct data types for variables and constants.

  float voltsense = analogRead(voltpin);

float voltage = 5*voltsense/1024;

None of those are floats.

int voltsense = analogRead(voltpin);
float voltage = 5.0 * voltsense / 1024.0;

Welcome to the world of engineering -- which attempts to get the right answers despite the fallibility of man and his manufactured components.

First Question: What model of board are you using?
Second: What is the voltage (Vcc) present on the board?
Third: When you read the spec for your processor chip what did it say the accuracy of your readings would be? (You did read the spec -- right?)
Fourth: Are we talking about long "rats nest" wiring or a simple short run to the battery under test.
Fifth: Is this the ONLY thing hooked to your board? It's not like you have a half-dozen servos and an LED display there too right?

A 12V battery that measures 11.18 Volts sounds interesting -- they usually read higher. A AA battery normally reads what -- 1.62V? Is your voltmeter reading as it should.

The technical version of "Who watches the watcher?" is -- "Who validates the tester?"

WillR:
The technical version of "Who watches the watcher?" is -- "Who validates the tester?"

This is another excellent point.

Just because your multimeter says a number, it doesn't mean it is right.

People seem to be worrying about the Resistor Values and the tolerance value. Why? In the old days we used trim-pots to set the measuring circuit to a "reference" voltage/current/whatever. We knew and understood "error" --epsilon? The Greek E -- right?

Nowadays everything is digital -- so when we "read" the analog world it must be right? Correct?

I think the issue is the "jitter" in the values.

  1. What is the source of those Jitter amounts.
  2. Is it excessive. If so -- where is it coming from.
  3. Will averaging multiple reads compensate. (Probably...)

The Arduino "accuracy" can be adjusted -- by sacrificing "read speed" -- it can be adjusted -- I have done it. You don't need capacitors. Averaging should in most cases do the same things.

The jitter is likely coming from the onboard ADC -- why? Is it within allowable limits. Is there a source voltage problem for the Arduino Supply? Is there ripple?

Back to basics.

Where's Doc Edison when you need him?

Probably wasting time learning "C" -- while we try to remember Ohms law! rotflmao!

This is an electronics test and measurement problem first -- a programming question second.

This should answer most of your questions,

Its a controlled current sink with battery voltage measurement (attached diagram), where i am taking readings 1 second apart and storing them in MicroSD Card !

The 10k resistor reads 9.96k on meter and 5k reads 5.01k

The battery Now Reads 11.12V through Meter & Voltage After the Voltage divider is 3.76V

But my code should be independent of voltage reduction, as i need to monitor battery as it discharges but it should be able to measure and scale from 12 to 1 Volt !

simplytuff:
The 10k resistor reads 9.96k on meter and 5k reads 5.01k

And what does the 5 volt supply read?

5.00 to be precise but thats through the external 5v pin provided on arduino i believe the arduino default internal reference might be lower ?

simplytuff:
5.00 to be precise but thats through the external 5v pin provided on arduino i believe the arduino default internal reference might be lower ?

Why do you believe that?

Just do the ratios so they agree with reality. It should be fine.