• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Battery Capacity logger?

Q&A Forum › Battery Capacity logger?
0 Vote Up Vote Down
William Lucid asked 1 year ago

Any recommendations for finding Sealed Lead Acid battery capacity using an ESP32?

Update  11/15/2024:  Demo of Battery Capacity Monitor

Planning to log capacity and send alert once capacity reaches a set point.  I have seen  Amazon Smart Battery Monitors

Regards,

William

Question Tags: Battery, Capacity, ESP32
31 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi.
I’m sorry, but I’m not familiar with this subject.
Regards,
Sara

1 Vote Up Vote Down
William Lucid answered 1 year ago

Prompted Google’s Bard; this is the response for using a INA219 sensor:

Sure, here is an example of integrating the current over time to calculate the battery capacity:

C++

#include <Wire.h>
#include <INA219.h>

INA219 ina219(0x40);

long totalCharge = 0; // Variable to store the total charge consumed
long startTime = 0; // Variable to store the start time

void setup() {
  Serial.begin(115200);
  Wire.begin();
  ina219.begin();

  startTime = millis(); // Record the start time
}

void loop() {
  long current = ina219.getCurrent_mA(); // Read the current in mA
  long elapsedTime = millis() - startTime; // Calculate the elapsed time in milliseconds

  totalCharge += current * elapsedTime; // Update the total charge consumed
  startTime = millis(); // Reset the start time for the next iteration

  Serial.print("Total Charge: ");
  Serial.println(totalCharge);

  delay(1000); // Delay for 1 second
}

This code will print the total charge consumed by the battery to the serial monitor every second. You can modify this code to calculate the remaining battery capacity by dividing the total charge by the battery’s nominal capacity (in mAh).

Will investigate and experiment further.

Regards,
William

0 Vote Up Vote Down
William Lucid answered 1 year ago

Good Morning,

Planning to use a INA226, I2C Current, Voltage, and Power device for measurements.  INA226 will be part of the EByte, E220-900T30D project:

Remote-Relay-with-Battery-Monitor  (a work inprogress..)

William

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Great.
Thanks for letting us know your progress.
Regards.
Sara

1 Vote Up Vote Down
William Lucid answered 1 year ago

Good Afternoon,

Progress is good on project; mostly complete, want to add two-way messaging, that will come later.  Now the project uses two, EByte E220-900T30D transvievers to wake a sleeping ESP32 with the first WOR preable message,  When a person sends a URL request to view camera; the solar charged, battery power is switched on with a KY002S mosfet switch, happening at same time a count down timer is started to turn off battery power once timer expires.  When the battery power is turned on battery status is checked using a INA226 Battery monitor module.  Battery data is logged to LittleFS file.  Stand alone FTP sketch allows access to file for viewing.

Camera is a Wyse Cam v3 that draws 180 mA; that until now used battery 24/7 on a 10,000 maH power bank that only lasted about day.  This improvement; should conserve the majority of lost battery power.

Updated project code

Short video of the project:

William

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

That’s great.
thanks for sharing your findings and progress.
Regards,
Sara

0 Vote Up Vote Down
William Lucid answered 9 months ago

Good morning,

Taking another look; prompting ChatGPT for example code using ESP32 and INA226 for a battery capacity monitor.

ChatGPT responses

William

0 Vote Up Vote Down
William Lucid answered 7 months ago

Some details and sample code from ChatGPT:
INA266 Details and Code –ChatGPT
Do not have a dev board with me; rehabbing from  back surgery.

Will do more INA266 coding; Good Start in project E220-Remote-Switch on Github.com/tech500.

William

1 Vote Up Vote Down
Bernie answered 7 months ago

When building a li-ion cell tester, I had a pretty bad experience with current sensors.  The analog sensors like the INA169 and INA219 lacked accuracy when reading by a 10 bit ADC on a MEGA.  I then used the Hall effect ACS712 — but again had a lot of issues.  I needed to sense currents as low as 25 mA and as high as 10A.  The Hall effect sensors lack low current resolution.  You need to be over 500 mA to get a decent reading.
I will be overhauling the project and will now use a I2C digital sensor such as the INA236.  There are many sources of error in an analog current sensor, and it is not just the ADC resolution.  Playing with both analog and digital temperature sensors, I have concluded that the digital sensors are quite more accurate and once one has the driver, the digital sensor is always the easier and superior.
Berni

0 Vote Up Vote Down
William Lucid answered 7 months ago

Hello Berni,

Okay on reworking your project; did not know of the INA236, think I read of a INA238 in addition.
Looking for volunteer to try Battery_Capacity_Monitor.ino.  I have no hardware with me while finishing my rehab.for back surgery and will not be able to test before Mid-November when released.

Sara or Beni could you try the code. Anyone else want to volunteer to try code?

Regards,
William

0 Vote Up Vote Down
LoetLuemmel answered 7 months ago

Hi William, Berni,
I was playing tons of hours with my BMS this summer.
As result, I did use the calibrated ADC on my ESP32 dev boards and was surprised about the jitter. Sure, I could flat-down jitter with a low pass plus averaging measurements.
However, I did experiment with TI‘s ADCs with more success and need much more than one ADC anyway.
Here I was more interested in the cell voltage, but also started looking at currents.
Well, I have some hundreds of amps to measure and I wasn‘t interested in accuracy, just the occurrence of strong fields were enough information.
However, I did notice that hall sensors need to be well shielded or physically isolated, not also measuring the neighborhood.
For some sources I did use shunts with satisfying precision. But I don‘t have them for the real big currents.
I can’t promise big support here, but try to test William‘s code, if I find some time.
However, I will most likely test on ESP32.
Here the 10 bit accuracy with calibration will yield to about ~1% accuracy. Don‘t know if this is sufficient for monitoring SOH of LiIon cells?
Yours,
LoetLuemmel

0 Vote Up Vote Down
William Lucid answered 7 months ago

Good Morning,

Thank You Berni for your willingness to try the code when you have time.  Line number 64 is set for a 12 volt battery; edit for voltage you are using.

Used Arduino IDE verify function to compile code; compiled no errors.

Regards,
William

0 Vote Up Vote Down
LoetLuemmel answered 7 months ago

Ha, thank you for the good morning, William!
That wasn‘t Berni, but me, the LoetLuemmel.
Thanks for the 12V hint, I might change it to monitor my ESP power supply Vin.
Have a good night!
LoetLuemmel

0 Vote Up Vote Down
William Lucid answered 7 months ago

Hi LoetLuemmel,
 
My sincere apologies for the my confusion.
 
Please advise if the code works; feedback is appreciated; as I am unable to test the code.
Away from my lab bench in rehab.
.

Regards,
William

0 Vote Up Vote Down
LoetLuemmel answered 7 months ago

Update below…

0 Vote Up Vote Down
LoetLuemmel answered 7 months ago

Hi William,
I did stat an ePaper project yesterday, so good chance to couple this with tests on your code!
Just ordered an INA226. Best offers were for <3 $ from Shenzen, but shipment would take about a month. Finally ordered one from Europe for 15 bugs and it should arrive next Tuesday. 
Have a good recovery and I bet you are already hot getting back to your lab!
Yours,
LoetLuemmel

0 Vote Up Vote Down
William Lucid answered 7 months ago

Hi LoetLuemme,
Thank you for ordering the INA226.  Do have an Amazon nearby; they have a large selection of sensors and electronic components!  They even have Silicon, Dupont wires and Silicon wire; both recommended by the “Swiss Guy” on “YouTube.”

Amazon Prime is a little pricey; but well worth the cost, have saved $330 (US) this year in shipping costs!  When you need a part there Prime delivery can be as fast as overnight!  

Feeling good about rehab progress; had VA appointment today to have 46 staples removed from the back.  Anxious to get back to lab; may still need to wait; apartment hunting, then building to fit; new lab.  Already have some ideals for new lab space.
 
Regards,
William

0 Vote Up Vote Down
Bernie answered 6 months ago

Hi William and Loet.  I will try to look at the code.  Thanks for your comments.

I am also building a 2170 power pack of about 4 kWh.  This will be 2x 14S8P.  Will be Panasonic or Samsung.  The controller will be ESP32 and 2x 14S BMS.  This should supply avg 102 V, and max 115 A, and about 12 kW.  Can be charged at 60 V. Hopefully I can find a digital SPI or I2C current and voltage sensor rated up to 60V.

0 Vote Up Vote Down
Bernie answered 6 months ago

1% accuracy is not sufficient when testing and comparing cells from different manufacturers.  However for operating a bank of cells, it can be sufficient.
 
The code looks good.  I will come back to it when I get my own INA226.  Just a couple of comments.  Assuming that the INA226 supplies the sense in integer and not float, I hesitate to convert to float.  Not only it is inefficient spacewise when dealing with a massive volume of data (a short is probably sufficient rather than a float or double), but also noise is introduced when converting in and out to float.  I keep sensor data as a rational number with a numerator and denominator, and use rational addition and multiplication – I believe this is faster than floating operations when dealing with massive data.  It is also easier to round numbers as integers rather than as floats, as you need to round when dividing.
 
Also I noticed that every 15 mins (or faster when needed) a call is made to NTP (I could be wrong though).  Imagine the billions of calls they must get every hour.  And there is a delay in dong that.  Who knows, the delay may also be intentional per IP address.  Better to keep an internal software timer that you synchronize with NTP once a day or so.
 
Please get well soon.
Bernie

0 Vote Up Vote Down
LoetLuemmel answered 6 months ago

@William: I did receive the INA226 already last night!
AMZN is really fast and I should also consider saving on the freight charges, as I am also a great supporter of our good Andreas with the Swiss accent, ha, ha!I’ll try to carve-out some time today for some measurements. We‘ve fall holidays and my grandson is here, so I‘m fully booked with building his 1st radio controlled model plane.  ; >
I‘m glad to read, that your rehab is progressing well. – Stay confident and hold course!

@Bernie: Wow, looks like a big project! I need to read the start of this thread to understand, what you‘ll plan to do with all that energy.
From hat I‘ve seen in a sample program, it seems to be float. But that was just the output, so not a reliable call!
Thanks for your comment about the 1% accuracy. This was also my move to the 16 bit external ADC. I‘m still on oldisch lead batteries, so I could survive with just 1%. But need to change to LiFePo in some years anyway.
I2C for 60 Volts? – Could be a little hard to get, as mostly the story ends slightly above 30 volts. But with high precision voltage divider, you should be good to achieve your measurements.
At the end of the day it‘s all physics and U=R*I helps us.
OK, gotta get back my attention to my family, I hear som noise downstairs..
Yours,
LoetLuemmel

0 Vote Up Vote Down
LoetLuemmel answered 6 months ago

Quick interim status: Bricked my ESP32 DEV with the ominous:

ets_main.c 371 
ets Jun  8 2016 00:22:57
 
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
ets_main.c 371 
ets Jun  8 2016 00:22:57
 
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
ets_main.c 371 

 

Ln 24, Col 22
ESP32 Dev Module
on /dev/cu.usbserial-0001
2
 

The ESP32 was just connected to the bare INA226 (power and I2C).
But I don’t think, the INA board was causing this. Forums are floated by similar occurrences.
 
Erasing flash also doesn’t work, it looks like this ESP32 can be only used for soldering trainings anymore.
I still have a couple of ESPs, but they are all wired. Let me see, if I can release one of them for our INA226 testing.
 
 

 

0 Vote Up Vote Down
William Lucid answered 6 months ago

Hi LoetLuemmel,

I have let the smoke out devices not often but a time or two. Marked it up as a learning experience.  Devices with no smoke remaining go in the trash.  Do not want no smoke devices mixed in with the good.  I have no skills for working with surface mount components.

If you want I will replace your ESP32; feel responsible for loss of your smokeless ESP32.

Did you have a common ground between ESP32 and INA226?

Article and Hookup INA226

Both ESP32 and ina226 need a common ground; component could be damage without common ground.

Regards,
William

0 Vote Up Vote Down
Bernie answered 6 months ago

Hi Luet, forget about lead acid.  Li-ion is so much better.  Its energy to weight ratio is 3x better than Lead Acid.

LiFePO4 is only good for flying because of safety.  For all other applications use 2170 cylindrical li-ion.  Not only safer than LiPoly, but lighter and cheaper and smaller.

I am not sure how a 30V INA226 can measure current for a 60V supply with a voltage divider that you mention.  Let’s say you divide the voltage by 2, and the INA226 ground is set to 30V.  Then you can measure a 60V current.  But your output bitstream will be from 30V (zero) to 35V (one).  You need to downshift this by 30 volts.  A voltage divider cant help.  I guess you could use a 30V zener diode to downshift?

 

I am using the 4 kWh battery pack for testing electric flight motors and propellers.  I need at least 30 kWh for flight, so I will be building seven of these boxes, for a total of 140 kg!

0 Vote Up Vote Down
Bernie answered 6 months ago

Sorry to hear that you have bricked the ESP32.  The INA output should not exceed 5V.  So this is strange.  Most likely the 30V battery must have touched Vin?
 
I once had flash problems when I connected a 4K7 pullup resistor to GPIO12 on a DevKit.  Pin 12 is apparently reserved for flash.

0 Vote Up Vote Down
William Lucid answered 6 months ago

Morning,

Made a couple of changes to “Battery Capacity Monitor” code.  Changed low Voltage check to loop and set NTP call to 10 second interval.  Updated Gist below:

Battery_Capacity_Monitor 2.ino

MIT –Lithium Battery Safety.pdf

Regards,
William

0 Vote Up Vote Down
William Lucid answered 6 months ago

Gave Microsoft’s Copilot message to create C++ code for a battery capacity monitor, with thingspeak graphing, and Arduino IDE plotting.

Copilot’s code for Battery Capacity Monitor 

Good starting code.  May need debugging and refinement.

William

0 Vote Up Vote Down
LoetLuemmel answered 6 months ago

Hi William,

The 1st code blob did not work on the WiFi-Side.

Your last code (Copilot’s code for Battery Capacity Monitor ) complains with the following error messages:

/Users/pitforster/Documents/Arduino/ESP32_INA226/ESP32_INA226.ino/sketch_nov3e/sketch_nov3e.ino: In function ‘void setup()’:
/Users/pitforster/Documents/Arduino/ESP32_INA226/ESP32_INA226.ino/sketch_nov3e/sketch_nov3e.ino:44:23: error: ‘INA226_AVERAGE_1’ was not declared in this scope; did you mean ‘INA226_AVERAGES’?
44 | ina226.setAverage(INA226_AVERAGE_1);
| ^~~~~~~~~~~~~~~~
| INA226_AVERAGES
/Users/pitforster/Documents/Arduino/ESP32_INA226/ESP32_INA226.ino/sketch_nov3e/sketch_nov3e.ino:45:12: error: ‘class INA226_WE’ has no member named ‘setBusVoltageRange’; did you mean ‘getBusVoltage_V’?
45 | ina226.setBusVoltageRange(INA226_16V);
| ^~~~~~~~~~~~~~~~~~
| getBusVoltage_V
/Users/pitforster/Documents/Arduino/ESP32_INA226/ESP32_INA226.ino/sketch_nov3e/sketch_nov3e.ino:45:31: error: ‘INA226_16V’ was not declared in this scope; did you mean ‘INA226_WE’?
45 | ina226.setBusVoltageRange(INA226_16V);
| ^~~~~~~~~~
| INA226_WE
/Users/pitforster/Documents/Arduino/ESP32_INA226/ESP32_INA226.ino/sketch_nov3e/sketch_nov3e.ino:46:12: error: ‘class INA226_WE’ has no member named ‘setResolution’
46 | ina226.setResolution(INA226_ADCRES_12BIT);
| ^~~~~~~~~~~~~
/Users/pitforster/Documents/Arduino/ESP32_INA226/ESP32_INA226.ino/sketch_nov3e/sketch_nov3e.ino:46:26: error: ‘INA226_ADCRES_12BIT’ was not declared in this scope
46 | ina226.setResolution(INA226_ADCRES_12BIT);
| ^~~~~~~~~~~~~~~~~~~
/Users/pitforster/Documents/Arduino/ESP32_INA226/ESP32_INA226.ino/sketch_nov3e/sketch_nov3e.ino:47:12: error: ‘class INA226_WE’ has no member named ‘setShuntResistor_mOhm’
47 | ina226.setShuntResistor_mOhm(100); // Adjust based on your shunt resistor
 
Maybe the header file is from a different source.

My bricked ESP32 is still dead and I was forced to unmount another device from a TFT Touch Display project
(too many cables).

The INA226 is a nice power module.
I need to run it in the right current range, now I did connect a ATtiny with a blinking diode:

Shunt Voltage [mV]: 0.01
Bus Voltage [V]: 3.32
Load Voltage [V]: 3.32
Current[mA]: 0.08
Bus Power [mW]: 0.00
Values OK - no overflow

Best,
LoetLuemmel

 

0 Vote Up Vote Down
William Lucid answered 6 months ago

Hi LoetLuemmel,

WIFI in 1st code using static network addressing and not dhcp.  Also it uses a less common address range 10.0.0.x instead of 192.168.1.x; for example if your on 192,168,1,x you will never be able to “talk” to anything on the 10.0.0.x network.  Another WIFI error; there are two include headers for WIFI.

Compiler errors on the 2nd code:  Fixed the Microsoft Copilot errors; this is why there are cautions about the generated code; most of the time I end up fixing coding error.  In this case wrong keywords for the INA226_WE library, it is example code, not perfect code ready to use in most cases, based on my experience.

Here is the modified code; compiled on ESP32 Core:  2.0.17.

Modified –Battery Capacity Monitor

Best Regards,
William

0 Vote Up Vote Down
William Lucid answered 6 months ago

Demo code with “dummied” variables; plots graph on Arduino IDE, Serial Plotter.  Code for INA226 and Thinkspeak  is commented out:

Demo of Battery Capacity Monitor

William

0 Vote Up Vote Down
Bernie answered 6 months ago

What is the current range that you are measuring, and is the INA226 working properly at low currents < 50 mA?

0 Vote Up Vote Down
William Lucid answered 6 months ago

Hi Bernie,

I am still at the rehabbing facility; away from home lab and components.  Several things are keeping from testing in real world environment.  Will be discharged tomorrow; and moving in two weeks and a day to an apartment..  .

My use case is two buck converters one; input 12 volts, output at 5 volts for a Wyse Cam 3.  Cam 3 draws about 180 mA.  Solar panel charges 12 Volt 7AH battery.  Last buck converter input is 12 volts, output voltage is 3.3 volts for an ESP32 and a EByte E200-900T30D transceiver.

Would like to plot the discharge curve for the 12 Volt battery.

Reading your post to this thread; sounds like you have more experience than I with the INA226.  This is my first go at using a INA226.  Have allot to learn in real world application of the INA226.

William

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.