rlogiacco / BatterySense

Arduino library to monitor battery consumption
GNU Lesser General Public License v3.0
402 stars 75 forks source link

Battery voltage reporting too high #51

Open entozoon opened 2 months ago

entozoon commented 2 months ago

Hi, I'm surely doing something wrong, but here is my situation in designing a voltage tester for 3S li-ion batteries using a Wemos D1 Mini. Note the only connection to the arduino is the A0 sense pin and ground. The battery being tested is a separate entity, if that's significant.

#include <Arduino.h>
#include <Battery.h>
// *    +--------+
//  *   |        |
//  *   |       +-+
//  *   |       | | 40k  R1
//  *   |       | |
//  * -----     +-+
//  *  ---       |---------+ A0
//  *   |       +-+
//  *   |       | |
//  *   |       | | 10k  R2
//  *   |       +-+
//  *   |        |
//  *   +--------+---------+ GND
//  **/
const float R1 = 40000.0; // ohms
const float R2 = 10000.0;
const uint16_t battMin = 10500; // millivolts
const uint16_t battMax = 14400;
Battery battery(battMin, battMax, A0);
void setup()
{
  Serial.begin(76800);
  while (!Serial)
    ;
  float ratio = (R1 + R2) / R2;  // 5
  battery.begin(5000, ratio);
}
void loop()
{
  int analogValue = analogRead(A0);
  float batteryVoltage = battery.voltage();
  int batteryLevel = battery.level();
  Serial.print("Analog value: ");
  Serial.print(analogValue);
  Serial.print(", Battery voltage: ");
  Serial.print(batteryVoltage);
  Serial.print(" mV, Battery level: ");
  Serial.print(batteryLevel);
  Serial.println("%");
  delay(200); 
}

At 10.5 volts (the minimum, which should report as 0%), the output is similar to:

Analog value: 675, Battery voltage: 16479.00 mV, Battery level: 100%

Perhaps I'm misunderstanding how the ohms law ratio works here, but the analog value seems to be safely within 1024 as expected but yeah, the battery voltage stated in mV is way too high

entozoon commented 2 months ago

Hmm, if I set my refVoltage to 3306, which is what I measure across the 3V3 pin and GND (despite powering from 5V USB) then the figures are much closer. Still half a volt out, but I guess it's better than nothing and I can fudge the numbers ..

rlogiacco commented 2 months ago

Which MCU is your code being run on? The 5000 value passed as argument at the begin() method is the microcontroller’s beef value of the internal ADC…

Il giorno lun 8 lug 2024 alle 23:57 Michael Cook @.***> ha scritto:

Hmm, if I set my refVoltage to 3306, which is what I measure across the 3V3 pin and GND (despite powering from 5V USB) then the figures are much closer. Still half a volt out, but I guess it's better than nothing and I can fudge the numbers ..

— Reply to this email directly, view it on GitHub https://github.com/rlogiacco/BatterySense/issues/51#issuecomment-2215417329, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABQWMNTBCUNMSNNDEJH6YDZLMDMZAVCNFSM6AAAAABKRQW7BSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMJVGQYTOMZSHE . You are receiving this because you are subscribed to this thread.Message ID: @.***>

entozoon commented 2 months ago

Hi @rlogiacco, thanks. I have a Wemos S2 mini which is an ESP32-S2FN4R2. I do not understand how to discovery the correct ref voltage.. :(

This ESP32-S2 document mentions 1100mV as the ADC calibration reference voltage but that sounds way too low to me..

rlogiacco commented 2 months ago

Yours is a 3.3V board and also the ADC seems to be recommending an external 100nF capacitor:

So, it would help if you change the begin method parameter to 3300 (as for ´3.3V´) and add the capacitor. I also suggest measuring the real values of the two resistors used for the voltage divider to increase accuracy: unless you go for precision resistors, values might be quite different from what you expect.

To be clear, the ESP32 (the MCU at the core of your board) has a 1.1V vRef (specs from the manufacturer state this is just a median value though), this is then converted to a 3.3V reference by the board (by using a voltage divider, which might be introducing inaccuracy), on top of that you are using an additional voltage divider... Sadly the board you are using is not really the best choice for your goal if accuracy is key.

BTW, I do not own the board you are using, thus I’m only reading specifications found on the internet.