Lipo measure battery INA219

Hi, I have a question. I have a circuit with an ina219 that measures the battery of the circuit, a lipo 3s 2200mah 35C. In the circuit there is a stepper motor with a l298n driver. When I read the bus voltage from the ina it gives me a value that is almost stable around 12.46 for example, while as soon as I turn on the engine for 10 seconds it immediately drops to 12.18. If I then turn off the engine it returns to around 12.46. Now since I wanted to write a code that actually tells me the remaining battery life to estimate the battery consumption as a function of the execution time of my code, how should I write the code? Initially I wrote it like this on MATLAB:


current_A = current_mA / 1000;  % Converti la corrente da mA a A
potenza_W = busVoltage .* current_A;  % Calcola la potenza istantanea in W

energia_J = cumtrapz(tempo, potenza_W);  % Energia totale in joule
energia_Wh = energia_J / 3600;  % Converti da joule a wattora

% Plotta l'energia consumata nel tempo
figure;
capacita_batteria_Wh = 11.1 * 2.2;  % Capacità della batteria in Wh
plot(tempo, capacita_batteria_Wh - energia_Wh, 'b-', 'LineWidth', 1.5);

But every time I turned off the code the measured level was reset instead of starting from the actual reading. While if I use a formula to calculate the SOC:

 batteryVoltage = ina219.getBusVoltage_V();
 batterySOC = ((batteryVoltage - BATTERY_MIN) / (BATTERY_MAX - BATTERY_MIN)) * 100.0;

The level is distorted when I turn off the engine or if I turn it on.

What do you recommend I do?

If you can track the motor status (ON / OFF) I would just track both levels.


batteryVoltage = ina219.getBusVoltage_V();
 batterySOC = ((batteryVoltage - BATTERY_MIN) / (BATTERY_MAX - BATTERY_MIN)) * 100.0;

What are the types of the variables used?
This snippet gives too little information.
Better post a minimal sketch that shows the problems

Then you want to take voltage and current measurements while the motor is running.
It's normal for the battery voltage to drop while a load is connected and then recover while it is disconnected.

the problem is not at the code level but at the conceptual level. ina219 connects to the battery input and I can measure these values:


busvoltage = ina219.getBusVoltage_V();
shuntvoltage = ina219.getShuntVoltage_mV();
loadvoltage = busvoltage + (shuntvoltage / 1000);
current_mA = ina219.getCurrent_mA();
power_mW = ina219.getPower_mW();
sensorData = String(busvoltage) + "," + String(shuntvoltage) + "," + String(loadvoltage) + "," + String(current_mA) + "," + String(power_mW) + ", §";

. I was wondering how to write a formula that sensibly estimates how much residual battery charge I have left (so that I can evaluate the % use of the circuit a priori whether the engine is on or off, so if I turn on the system I understand how much I have discharged the battery)

so how could i write a string of codes to calculate the remaining battery capacity?

I know nothing about MATLAB code but basically you want to take voltage and current measurements from the INA219 say every 100ms when the motor is running. Then take the average of each and multiply to give you power in Watts (W). Then multiply that by the running time to give you the energy consumed in WattHours (Wh)

If the battery is a 12V 5Ah battery then it has 12V x 5Ah = 60Wh of energy when you started, so just subtract the energy consumed from 60 to give you the remaining battery energy.

yes it makes sense, my only doubt is that ina219 delivers instantaneous mW, not mWh. how should I reformulate it on arduino?

To do that, you need a model for the battery capacity, which must be reasonably correct for the battery you have, and must be updated as the battery ages and capacity decreases.

To learn how such models are formulated and used, take a look at the data sheets for "fuel gauge" ICs. TI has application/theory notes and a selection of ICs.

It will also give you voltage and current so just do as I said in post #6

@andreaarcarisi

Maybe this library can help you

1 Like

The above library is a good place to start. It uses a linear model for the battery capacity, which you determine for each battery by conducting a few charge/discharge cycles.

From the library README:

The big assumption here is that battery capacity is linearly correlated to its voltage: the assumption itself is wrong, but in most cases, it's close enough to reality, especially when it comes to the battery's higher capacity side

1 Like

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