Couting amp hours in arduino

Hello everyone! Hope all of you are doing well!

So for some time I have been trying to make my own arduino amp hour meter, and have been struggling on one single part of it. Put aside measuring the current, let's just say that a variable is being updated with a float of the current amperage.

Is there a simple way to calculate amp hours for let's say, taking readings 5 times per second? I would assume using the millis() function, but I just can't wrap my head around this right. I am going to use arduino nano.

If anyone can help me, it would be much appreciated!I just to simply count amp hours over time given a float that is being updated with the current, and I would like to take samples 5 times per second or so.

Thanks!

What does the current flow loook like? Is it a slowly varying current flow or is it, for example pulse width modulated? In principal You measure current at a certain sampling rate and multiply current with sample time and accumulate. That works for slowly varying current flows. For noicy or spiky currents some filtering will be needed.

Take a look here, it should give you a good idea how to do it. You have to also consider if you take more than one amp hour out of the battery which will cause the battery to drain faster.
I have built one of these and it seems to do a good job

All credit goes to the author of it.

Calculating battory status is one subject and calculate calculated energy, Ampere Hours, are different in my opinion.

Integration means current multiplied by time frame. What is the problem?

Sorry I should’ve specified the use for this. It’ll be used in an electric vehicle, with currents up close to 1000A, constantly fluctuating as I drive. I’ll be using an ADS1115 to read a 75 mv shunt bidirectionally.

It’s just the math of calculating amp hours I have trouble with. I’ve read some posts of calculating like amp-seconds and stuff using millis and I’m just not sure how to do it correctly and very accurately :confused:

To handle currents of that magnitude You must have a huge power controller. Does that controller provide any data, any information? It must be using PWM and some internal current control and most likely have built in intelligence. I used to work with 48 volt 500 Amp controllers.....

Railroader:
To handle currents of that magnitude You must have a huge power controller. Does that controller provide any data, any information? It must be using PWM and some internal current control and most likely have built in intelligence. I used to work with 48 volt 500 Amp controllers.....

Yep! It is a 1000A controller, a Zilla Z1k, has it's own intelligence, DTCs etc. Best high power DC motor controller on the market. That 1000A get shoved into a 9" DC motor to load out 350 ft lbs of torque!

Anyway...back to the problem. If I want to sample 5 times per second to have good accuracy, how would I go about doing the math to calculate amp hours given I have the float variable. So basically, I just need help with the calculation, I should have no problem measuring the current.

My idea was that the controller possibly could provide the average current. That would be the absolutly best way to use that information.

Depending of the frequency of the current measuring calls for quite different methods. Measuring DC or measuring some 10 - 15 kHz pulsed current needs totally different approaches.
Do have an oscilloscope that can look at the current? Just remember that 100 Amps through a well specified resistor seams a bad way to me. Current sensors, looking like coils around the pwr lines to the motor is what I think about.

What current sensoring stuff do You have?

@Delta_G
Thanks for explaining #1 and #3.
My question is what that "float with the amps" would be. Is there a reliable measurement? "Shit in, shit out" goes for the following math.

Thank you guys for the replies! @Delta_G thanks for clearing that up, for whatever reason it didnt make sense to me in my head, but the way you said it kinda made it click. Millis now - millis then, then convert to hours, that multiply by the current amperage. Then I think I just have an amp hour variable and I will keep adding the readings to?

@Railroader , the way I am measuring it is through a shunt. I have a JLD404 meter hooked up to that shunt currently, and it is fairly accurate. You are right, a nice current sensor might certainly be more accurate, but I want to tap into the shunt that is already in my car so that I can be developing my arduino amp hour meter, and verify the readings with the JLD404.

Thoughts on that? Seem like a good idea?

Sounds good that You are inspired. Dealing with measuring high currents several times in life I wanted to make sure that the current measurement was done the proper way. Else.... all math would be hopeless.

Railroader:
Sounds good that You are inspired. Dealing with measuring high currents several times in life I wanted to make sure that the current measurement was done the proper way. Else.... all math would be hopeless.

Definitely. That's why I want to keep the other energy meter so I can compare when I make mine, then I can compare accuracy.

A shunt and an ADS1115 for bi-directional current sounds suspicious.
How did you make sure the shunt is/stays within the common mode range of the ADS.
Leo..

Is the value in "the float variable" in Amps?

long AmpHours = 0;  // Fetch from EEPROM in setup() if you want it persistent.

void loop()
{
  static unsigned long lastSampleMicros = 0;
  float theFloatVariable = GetAmps();
  unsigned long currentMicros = micros();
  static float lastSample = 0;
  float averageAmps = (lastSample + theFloatVariable) / 2.0;
  lastSample = theFloatVariable;
  unsigned long newAmpHours = averageAmps * (currentMicros - lastSampleMicros) * 1000000.0 * 60.0 * 60.0;
  lastSampleMicros = currentMicros;
  AmpHours += newAmpHours;
}

You'll probably want to scale things differently. Pick the smallest unit of current and time that that will fit your expected capacity into a long integer (about 2 billion units). For example if your battery pack is 500 Amp Hours you could use Amp-Minutes but that would only be 30,000 units between empty and full. Amp-Seconds would give you 1.8 million units. Still well short of 2 Billion. So you could measure the current in milliamps for 1.8 billion units, a good fit for a signed long.

1 Like