Hep with ADC, measuring battery voltage

Hi,
I'm using a Atmel32u4 / 8Mhz / 3.3v
Atmel powered through a dc-dc buck/boost circuit.
whole project is powered with a single li-ion cell.
I'm trying to measure the battery voltage,
but the first read (4.19) is very accurate and there after all measurements are wrong (3.81).

Schematic:

Code:

void setup() {
  analogReference(INTERNAL);

  Serial1.begin(9600);
}

void loop() {
  unsigned long currentmillis = millis();

  if ((currentmillis - bat_volt_prev_millis) >= 10000))
  {
    Serial1.println(getBattery());
  }
}

float getBattery() {
  int batteryRawCount = 0;
  float voltage = 0.0; 

  for (int i = 0; i < 10; i++) {
    batteryRawCount += analogRead(VOLT_SEN);
    delay(10);
  }

  Serial1.print("batteryRawCount: ");
  Serial1.println(batteryRawCount);

  voltage = ((float)batteryRawCount / 10 * 2.56) / 1024.0;
  voltage = voltage * 5.992;

  return voltage;
}

Serial output:

batteryRawCount: 2794
4.19
batteryRawCount: 2547
3.82
batteryRawCount: 2546
3.81
batteryRawCount: 2546
3.81
batteryRawCount: 2544
3.81
batteryRawCount: 2546
3.81
batteryRawCount: 2544
3.81
batteryRawCount: 2543
3.81
batteryRawCount: 2545
3.81
batteryRawCount: 2548
3.82
batteryRawCount: 2545
3.81
batteryRawCount: 2543
3.81
batteryRawCount: 2544
3.81
batteryRawCount: 2543
3.81
batteryRawCount: 2545
3.81
batteryRawCount: 2546
3.81
batteryRawCount: 2545
3.81
batteryRawCount: 2547
3.82

Have you confirmed the readings are incorrect with a meter?

You are charging a capacitor trough a 50kOhm resistor. @ 100nF the time constant tau is about 5ms. You are sampling every 10ms so, your capacitor is charged to 2 x tau which is not enough for an accurate result. You need about 7 x tau to get within 1LSB for a 10-bit ADC.

1 Like

@missdrew yes. 4.19 is very close. meter reading is 4.184v

And is your DMM calibrated ?

Haven't done so for a while.
I have a Fluke 15B+ and it's about 2 years old.
So should I?

Hi,

I don't think you are going to get any closer.
4.19 - 4.184 = 0.006V

0.006/4.19 = 0.0014 error or 0.14%.
That is better than the 0.5% accuracy that is quoted for the 15B+.
I do calibrate certifications and Fluke 15B+ as with most Fluke will take literally 10s of years to go out of calibration.

Calculate what resolution you are getting from the ADC.
If you are using 1024 ADC and 5V ref.
1 count = 5/1024 = 0.00488Volts
That is the resolution of your ADC.

So 4.19 to 4.184 which is 0.006 is just ONE count out.

You do not have a problem.

How accurate do you want your reading?

Tom... :smiley: :+1: :coffee: :australia:

1 Like

Of course I'm happy with 4.19v reading.
But the issue is, there after every consecutive reading gives a 3.81v, even though DMM reads @ 4.184v.
4.19v output come only once when I power up the chip.

I'm using the internal voltage reference of 2.56v and should be 2.56/1024 = 0.0025v
Thanks!

Hi,
What happens if you remove C25?

Also;

voltage = ((float)batteryRawCount / 10.0 * 2.56) / 1024.0;

Try
Increase the analog read delay from 10 to 20 ms
AND/OR
Decrease your potential divider values by a factor of 10.

What is the internal reference voltage for a 32U4?

Just some suggestions.. Tom... :smiley: :+1: :coffee: :australia:

Let me try that.

After reading @Klaus_K post, I already tried increasing the delay up to 80ms, didn't help at all.

2.56v as per datasheet

Hi,
What voltage do you read with the Fluke at the Volt_Sen point then at the battery.
Does it agree with the 5.992 divisor that your potential divider exhibits?

Also can you try with lower value potential divider resistors.
Try 4k9 or two 10K in parallel to get 5K,and 1K, just to see if the ADC and circuit impedance is the problem.

Tom... :smiley: :+1: :coffee: :australia:
PS. I'm off to bed, 12:44am here and I very badly need my beauty sleep. :laughing: :sweat_smile: :rofl: :sweat_smile: :laughing:

Good morning!

Removing the 100nf cap did the trick.
Thanks for your help.
Have a good day ahead!

1 Like

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