const int voltagePin = A0; // Voltage sensor pin
const int currentPin = A1; // Current sensor pin
const float C = 100.0; // Battery capacity in Ah
float SoC = 100.0; // Initial State of Charge
// Define the resistances for the voltage divider
const float R1 = 10000.0; // Resistance R1 in ohms
const float R2 = 10000.0; // Resistance R2 in ohms
void setup() {
Serial.begin(9600); // Initialize serial communication
}
void loop() {
// Read voltage and current values
float voltage = analogRead(voltagePin) * (5.0 / 1023.0) * (R1 + R2) / R2; // Voltage calculation
float current = analogRead(currentPin) * (5.0 / 1023.0); // Current calculation
// Calculate change in charge (Ah)
float change_in_charge = current * (1.0 / 3600.0); // Convert seconds to hours
// Update SoC
SoC += (change_in_charge / C) * 100;
SoC = constrain(SoC, 0, 100); // Clamp SoC between 0 and 100%
// Print estimated SoC
Serial.print("Estimated SoC: ");
Serial.print(SoC);
Serial.println("%");
delay(1000); // Delay for next reading
}