# Determining state of charge using current sensor (ACS758) with arduino uno

I am working on a university project and I am struggling with estimating the state of charge of our batteries, by using coulomb counting.

Background: We are supposed to build a power supply for a mostly constant load of 100 W (can go up to 140 W) which will be placed outdoors for two years. The plan is to have a small wind turbine plus some solar PV connected to a large battery assembly. The system has to run close to 24/7 with limited downtime.

I have bought some ACS758 50A bidirectional current sensors as well as an Arduino UNO. I am extremely new to Arduino and I would really appreciate some help with the coding. The plan is to have the arduino do the sampling of current and integration, which is then translated to SOC as a percentage and sent to a Raspberry Pi which will send email alerts if the SOC is below a certain level.

I have been looking at sample code describing how to extract the raw data from the ACS sensor: https://www.dfrobot.com/wiki/index.php/50A_Current_Sensor(SKU:SEN0098) as well as sample code that performs integration (Integrating an analog signal using ARDUINO UNO SMD), which is what I would use for coulomb counting.

What I need is most likely a combination between the two, however I do not even know where to start from.

Thanks in advance!

• I'd just measure the voltage, and use that to calculate the charge level. Or at least use it to "reset" the current, when the battery is full. Just measuring the current in and out will eventually lead to some drift. Due to self discharge of the battery and/or losses. Apr 15, 2017 at 15:59
• Why not do both? Apr 20, 2017 at 11:09
• Is there any progress on this project ? It would be a great help if there is because our project is somehow like this.
– smg
Feb 10, 2018 at 0:52

## 2 Answers

Instead of using a separated component for each thing you can use a proper solution that is itself already a combination of both: the DS2438's integrated current accumulator. This link explain it pretty well, also here is a library to use it with arduino. They are pretty cheap (a few dollars) and will perform very well!

Although, in your place I would consider choosing a different approach. I don't know which kind of battery (Li-ion, LiPo, Lead-acid..) you are using, but in many cases if you monitor the battery voltage, you can have enough information about the battery state. I can't ask you in comment yet beause I'm new here and don't have enough reputation, but if you can describe better the kind of battery you are using and what you intend to monitor which your circuit, I can edit this post to offer you a better solution.

• Hi there, I appreciate the help. We are using some Rolls 4000 series, 12kWh total (windandsun.co.uk/products/Batteries/…). Both the solar PV and the wind turbine have charge controllers, and together with the battery they will all be connected to a positive and negative bus. What we want is to monitor battery SOC to ensure that if the battery level goes below a certain level, a warning is sent to someone. We will also attempt to estimate SOC via voltage measurement (what Gerben suggested) however doing coulomb counting is a project requirement :( Apr 16, 2017 at 11:01
• Which is the voltage of your battery bank? Apr 17, 2017 at 12:14

you should probably determine the soc using both current integration and open circuit voltage method.It is possible to cover the weakness of both techniques and provide an accurate monitoring technique.I can provide you the codes if you need,

``````#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int mVperAmp = 100;const float chargingdfficiency= 0.7;
int RawValue= 0, ACSoffset = 2500;
double Voltage = 0, Amps = 0;
double g,q;
int m=0,c=0,d;
void resett(){
digitalWrite(8,LOW);
digitalWrite(7,LOW);}

void setup() {
pinMode(A0, INPUT);pinMode(A1, INPUT);
pinMode(7,OUTPUT);pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
Serial.begin(9600);
delay(10);lcd.begin(20, 4); delay(10);}

void loop() {
resett();
double vini=analogRead(1);vini = vini*5/1023;
vini=5*vini;Serial.print(vini,3);vini=vini/6;
double soc=((666.6667)*vini)-1300;delay(10);
q=soc*.42;
if(soc<30){m=1;resett(); digitalWrite(7,HIGH);} //charging
if(soc>=100){m=0;resett();digitalWrite(8,HIGH); } // DISCHARGING
if(m==0){resett();digitalWrite(8,HIGH);}
delay(10);
while(1){
RawValue = analogRead(0);
Voltage = (RawValue / 1024.0) * 5000;
Amps = ((Voltage - ACSoffset) / mVperAmp);Amps=-Amps;
if((Amps<.098)&&(Amps>-.098)){Amps=0;}
if(Amps>9){
resett();
lcd.clear();
lcd.display();
lcd.setCursor(1,0);
lcd.print("Overload");digitalWrite(9,HIGH);
while(1);}
if(m==0){
if(Amps<=1.5){g=1.183;}
else if(Amps<=2){g=0.042*Amps+1.12;}
else if(Amps<=4){g=0.028*Amps+1.148;}
else if(Amps<=5){g=0.032*Amps+1.132;}
else if(Amps<=6){g=0.03*Amps+1.142;}
else if(Amps<=7){g=0.325*Amps-0.628;}
else if(Amps<=7.5){g=0.066*Amps+1.185;}
else if(Amps<=8){g=0.028*Amps+1.47;}
else if(Amps<=10){g=1.694;}
soc=(q-Amps*g*0.0002778)*2.38095; q=soc*.42;
if(soc<5){m=1;resett();digitalWrite(7,HIGH);} }
if(m==1){
soc=(q-Amps*chargingdfficiency*0.0002778)*2.38095;
q=soc*.42;
if(soc>=100){m=0;resett();digitalWrite(8,HIGH);}}
lcd.clear();
lcd.display();
lcd.setCursor(1,0);
lcd.print("Soc = ");
lcd.setCursor(13,0);
lcd.print(soc,1);
lcd.setCursor(19,0);
lcd.print("%");
lcd.setCursor(1,1);
lcd.print("Amps= ");
lcd.setCursor(13,1);
lcd.print(Amps,1);
if(m==1){lcd.setCursor(1,2);lcd.print("Charging");}
else{lcd.setCursor(1,2);lcd.print("Discharge");}
lcd.setCursor(1,3);lcd.print("Normal");
Serial.println((vini),3);
Serial.print("Amps = ");
Serial.println(Amps,9);
Serial.print(" Soc = ");
Serial.println(soc,9);

for(d=0;((Amps==0)&&(d<=1200));d++){RawValue = analogRead(0);
Voltage = (RawValue / 1024.0) * 5000;
Amps = ((Voltage - ACSoffset) / mVperAmp);Amps=-Amps;
if(d==1200){vini=analogRead(1);vini = vini*25/(6*1023);
soc=((666.6667)*vini)-1300;d=0;}}

delay(1000);
}
}
``````