State of health calculation for li-ion battery

So I'm doing a project that monitors a li-ion battery. I am using the asc712 current sensor.
I need to find the State of Charge (SOC) and state of Health (SOH). I have codes ready for the SOC.
But the SOH, I need the current max ah of the battery. So I have an idea to do it. Whenever the battery is fully charged, the code can measure the total ampere-hour (AH) and update the max AH. But I can't figure out how to code it as I have one current sensor and a loop will be running to find the total AH when the ESP32 runs. And to get the Max Ah is only when the battery is above 98% capacity.
This is the snippet code of the part that continuously run to finds the total ah

currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis1 - startMillis1 >= period)  //test whether the period has elapsed
  {
    totalCoulumbs = totalCoulumbs + AcsValueF;
    float TotalAh = totalCoulumbs / 3600.0;
     SOC = (TotalAh / (max AH of batt))*100;
     SOH = (max Ah of battery / 50)*100;
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current state.
  }

So like what I mentioned above, I can continuously find the total, AH but I want to find the max AH of the battery when the battery is above 98% capacity. So how do I do it?

This is my full code.


#include <EEPROM.h>
#include "WiFi.h"
#include <Wire.h>
#include "cactus_io_HIH6130.h"

const char* ssid = "dlink-muthus";
const char* password =  "Sujitha97";

//define the address used by the HIH6130 sensor (default is 0x27)
byte address = 0x27;
HIH6130 hih6130(address);

const int VoltageSensor = A0 ;
float vOUT = 0.0;
float vIN = 0.0;
int value = 0;

const int CurrentSensor = A1;

int smokesensor = A2;

int buzzer = A3;
int led = A4;

float SOC;
float SOH;

unsigned long startMillis1;  //some global variables available anywhere in the program
unsigned long currentMillis1;
const int period1 = 1000;  //the value is a number of milliseconds
float totalCoulumbs = 0.0;


void setup() {

  Serial.begin(115200);
  WiFi.begin(d-link-muthus, Sujitha97);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");

  startMillis1 = millis();  //initial start time

  Wire.begin();
  //Serial.println("Honeywell HIH6130 Humidity - Temperature Sensor");
  //Serial.println("RH\tTemp (C)\tHeat Index (C)");

  pinMode(smokesensor, INPUT);

  pinMode(buzzer, OUTPUT);
  pinMode(led, OUTPUT);

}

void loop() {
  value = analogRead(VoltageSensor);
  vOUT = (value * 5.0) / 1024.0;
  VoltageValue = vOUT / 0.2;
  //Serial.print(VoltageValue);

  unsigned int x = 0;
  float AcsValue = 0.0, Samples = 0.0, AvgAcs = 0.0, AcsValueF = 0.0;
  for (int x = 0; x < 150; x++) {
    AcsValue = analogRead(CurrentSensor);
    Samples = Samples + AcsValue;
    delay (3);
  }
  AvgAcs = Samples / 150.0; //Taking Average of Samples
  CurrentValue = (2.5 - (AvgAcs * (5.0 / 1024.0)) ) / 0.185;
  //Serial.print(CurrentValue);

  int MQ2Sensor = analogRead(smokesensor);
  //Serial.println(sensorValue, DEC);


  hih6130.readSensor();
  //Serial.print(hih6130.humidity); Serial.print("\t");
  //Serial.print(hih6130.temperature_C); Serial.print("\t\t");
  //Serial.print(hih6130.computeHeatIndex_C()); Serial.print("\t\t");

  if (hih6130.temperature_C > 60 || hih6130.temperature_C < -20 || MQ2Sensor > 400 || hih6130.humidity > 75)
  {
    tone(buzzer, 1000);
    digitalWrite(led, HIGH);
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000);
  }
  else
  {
    noTone(buzzer);
    digitalWrite(led, LOW);
  }

  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis1 - startMillis1 >= period)  //test whether the period has elapsed
  {
    totalCoulumbs = totalCoulumbs + AcsValueF;
    float TotalAh = totalCoulumbs / 3600.0;
    // SOC = (TotalAh / (max AH of batt))*100;
    // SOH = (max Ah of battery / 50)*100;
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current state.
  }

Your post was MOVED to it's current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

You can't measure how many amp hours there is in the charged battery unless You discharge it using a well known constant current and You need to use the voltage defined as the lowest allowed.
All batteries degenerate as they are cycled. Some batteries are specified for only 500 cycles.

yes, so the module will be connected to the battery and will measure it while it discharges
the battery is 12v and has 2000 cycles

A 2000 cycle Li is mostly likely a LiFePo4, right?

Li is NOT equal to LiFePo4. The wrong info supplied will give garbage.

If the battery is a LiFePo4 then look up how to build a PWM Charge controller. There you should see the circuit that determines a charged Lead Acid/LiFePo4 battery. Which could then be incorporated into your project.

Note a MPTT or PWM Charge Controller for a lead acid battery will not charge a LiFePo4 12V battery to full charge. The max volts applied to a lead acid is 13.8V. To get a full charge on a LiFePo4 the voltage needs be over 14 volts, 14.7.

The lowered max charge voltage of a charge controller is, actually, good for the life cycles of a LiFePo4.

Anyways, you'll sample the charge volts being put out by the charge controller. When the volts of a charge controller drops, that means the charge controller has stopped charging the battery and the circuit is now running on battery volts, which would indicate max volts had been reached.

I strongly advise getting a charge controller that has a lower limit cutoff voltage.

Is the project intended to monitor the discharging of the battery during delivery of power to some load, or during charging?

Yes it monitors when discharging

Yes it's lifepo4
Ah from what I understand from your answer the charge controller helps ro charge the battery right?
Cause my project will monitor the battery when it discharges.

A charge controller, as one of its jobs, will charge the battery. Also, a charge controller monitors the state of discharge so that it may know when to charge the battery.

I monitor the battery voltage, current drawn from the battery, and send that info, along with other info, to my website.

Ohhh then I shall look into a charge controller
Thank you

I have used several PWM controllers. This PWM Charge controller, for the price, has the great feature of a voltage cutoff. That way the LiFePo4's are not completely drained.

ah, I see. ill try to find the connections for it and try to replicate it thank you