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:
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.
. 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)
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.
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.
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