## Measuring the average current of an acid battery

19 posts / 0 new
Author
Message

In a DC to AC inverter set, the in/out current of a battery could be periodic (mainly during its charging) or real DC (if discharged by a DC load, as LEDs connected directly to it).

Let us assume an MCU of a current meter measures the battery current every 104 usec. The MCU, I have, is ATmega8A.

What could be the optimum algorithm (no need for details) to find out the ‘average’ current from the measured samples? (during practical intervals, about 1/2 sec, and without adding a synchronising circuit)

Please note that in case of charging (by the mains) or discharging (by a DC to AC inverter), the frequency is usually not known (around the nominal one) and likely varies continuously (mainly of the relatively small private AC generators).

Thank you.

Kerim

This topic has a solution.

Last Edited: Mon. May 30, 2016 - 08:44 AM
Total votes: 0

The average algorithm is widely known. But if you keep the samples and total, you can subtract the old sample from the total, add the new sample into the total, divide the total by the number of samples to get the latest average. If you have 1V of ripple during charging, and you average 100 samples, the ripple will get down to 10mv, which is about 2 lsbs. Smooth enough?

Imagecraft compiler user

Total votes: 0

Thank you, Bob, for your prompt reply.

I will examine your solution when the supply frequency varies around the nominal one (higher and lower) and in case the current is of a full-wave rectifier. As you know, in this case, the current varies from zero (during a certain time of each half-cycle) to a peak value (up to the permissible maximum one).

Kerim

Edited:

The main point is to find out if, for a constant current shape (hence the average), it is possible to let the display refresh rate be constant (about 1/2 sec for example) while the displayed number is stable and independent of the mains frequency.

Last Edited: Sun. May 29, 2016 - 05:45 PM
Total votes: 0

I'm a bit confused...lead-acid battery is only DC.  So the charging current is DC too, isn't it?  DC-in and DC-out, right?

Some of the AVR "battery" models have a columb counter, right?  Dunno whether suitable for SLA work.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Total votes: 0

"I'm a bit confused...lead-acid battery is only DC."

You are right, but I am interested in its current which is usually not constant with time (while it is DC, not AC, of course).

Kerim

Total votes: 0

But the current could be either into the battery (charging) or out of the battery (discharging) - so, in that sense, it can "alternate" ...

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Total votes: 0

awneil wrote:

But the current could be either into the battery (charging) or out of the battery (discharging) - so, in that sense, it can "alternate" ...

And my job is to display the battery average current (that can alternate), during a certain interval, with time ;)

'Only' when one tries to apply the known algorithms to get this average (as positive or negative, and without a synchronising circuit with the varying frequency of the mains and inverter), he will find out it is not trivial to get a steady display always even if the current average (of the sinusoidal pulses in the intervals when the AC supply voltage becomes higher than the battery DC one) is made constant.

Kerim

Last Edited: Mon. May 30, 2016 - 07:36 AM
This reply has been marked as the solution.
Total votes: 0

Hello Kerim.

Hope you are well.

Could you simply feed the voltage produced across your current sensing resistor into a first or second order low pass filter and measure the filter's output voltage?

Cheers,

Ross

Ross McKenzie ValuSoft Melbourne Australia

Total votes: 0

awneil wrote:

But the current could be either into the battery (charging) or out of the battery (discharging) - so, in that sense, it can "alternate" ...

KerimF wrote:
And my job is to display the battery average current (that can alternate), during a certain interval, with time ;)

So what you really want to know is whether there's been a net charge or discharge over the period?

In other words, what you're really doing is a so-called "coulomb counter"?

So it's not so much averaging the current as integrating it over the interval ...

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Total votes: 0

valusoft wrote:

Hello Kerim.

Hope you are well.

Could you simply feed the voltage produced across your current sensing resistor into a first or second order low pass filter and measure the filter's output voltage?

Cheers,

Ross

Hello Ross,

Hope you are too.

So far, I am fine. Thank you for asking.

You are right. Adding a simple LP filter solves the problem.I already have 3 unused opamps that could be used for it.

On the other hand, I try to design and produce a meter not to measure the average current only, but also to measure the battery internal resistance (actually impedance).

I guess if I measure the battery average voltage also after an LP filter, the ratio of the two averages also gives a sign of how good the battery acts under heavy loads.

I was also looking to capture the battery voltage at zero current while its i(t) is varying with time. But having now an idea of the battery internal resistance (Ri) one can apply the simple formula:

V_bat (at zero current) = V_avg - Ri * I_avg (during discharging, I_avg is negative)

Since I am also interested in getting I_peak to have an idea of how smooth the charging current is, I think I will add a peak detector. In this case, three ADC pins will be used; for I_avg, V_avg and I_peak.

Finally, if I continuously read I_avg, the quantity of charge (positive and negative 'separately' and accumulated since the reset time) could be known. And this helps knowing, to a good extent, the battery efficiency concerning its Ampere/Hour capacity, not power that needs v(t) too.

Thank you for reminding me the simple way.

Kerim

Edited:

Please note I did a big mistake. Ri above is NOT V_avg / I_avg. It should be Ri = dV_avg / I_avg instead, sorry.

Therefore, the formula of V_bat at zero current cannot be applied as well if Ri is calculated as V_avg/I_avg.

Last Edited: Mon. May 30, 2016 - 09:09 AM
Total votes: 0

I'd like to see a block diagram or schematic. You asked about an average, and I suggested a software way and Ross suggested a hardware way. Its hard to imagine that a couple of Rs and Cs could add to the cost of an item, but if you are making 1000s, hardware costs get more important, and it becomes worthwhile to push more functions into software.

Imagecraft compiler user

Last Edited: Mon. May 30, 2016 - 09:46 PM
Total votes: 1

You are right, Bob.

Based on the 'incomplete' title of the thread (that is... just for measuring the average current only and perhaps the A/H charge), adding a simple LP circuit could be a good idea for hobbyists and even for a production that aims the local market.

But if your read my last post, I look for more data; as I_peak, V_bat at a high and low current samples to deduce, even approximately, the battery internal resistance and V_bat @I(t)=0.

Tomorrow I will test a simple method and see what I will get on the display.

As I mentioned earlier, my sampling interval is 104 usec.

I add 8192 (32*256) samples which last for about 852ms.

The range I chose for the current range (and accuracy) is -80.0 to +80.0.

I will be back tomorrow ;)

Kerim

Total votes: 0

Wait a minute. Read msg 11 again. Did that guy ask to see a block diagram or schematic? And you just didnt mention it? You could have said... 'Sorry dude. This is a Top Secret project and I'm under a Non Disclosure Agreement and can't discuss any of the implementation details'. If the dudes at your outfit are so darn smart, how come cant they answer a basic ee question or two?!?

AVRfreaks like challenges. Just post a spec. Its Smart Car Battery Charger right? List the requirements: Must have 3 charge profiles: constant current, constant voltage, finish and float. Must display volts, current, percent charge, temperature, time to 100%. How many watts? 60Hz transformer or a switcher? Obsolete old 7seg numeric display, or nice OLED alphanumeric display? Mechanical switches, or dome switches right into the microcontroller? It can indubitably be buit with a mega1280, but the challenge is to see if you could optimize it down into a mega48 or mega88. Target price and number of units? Battery chargers are a Tight Market. Have you typed 'smart battery charger' into Google? You have to compete on Features and/or Price. So see what feature is missing from all chargers in the lo, medium or hi range segment, and set a price 10% under that for the target. Got it? Good.

Imagecraft compiler user

Last Edited: Mon. May 30, 2016 - 10:00 PM
Total votes: 1

Dear Bob... It seems you have no idea yet that we live too far away from each other ;)

So while I was replying you, I was called for having diner (it was about 9 PM). But at the same time you might enjoy a pleasant afternoon break out there ;)

On the other hand, my city (Aleppo-Syria) doesn't have any more the luxury it had before 4 years ago (and since I was born; 67 years ago).

The mains supply is now distributed by local private generators. Their frequency and voltage fluctuate and their mean values also vary in a rather wide range.

Also the distribution (to every house) of the city fresh water (drinkable) may stop in any hour and for undetermined time; for a few hours, a day, a week, even for many months (as it happened last year).

I guess you got the big picture on how I live now and why I am interested in acid batteries ;)

For instance, since my graduation (1975) I started my small private business. I used designing and producing products for the local market; according to its needs in every period of time. So I am a real independent free person who has no secret; technically and socially as well ;)

About my schematic, it has nothing new; Atmega8A, LED 4-digit 7-segment display and an opamp to amplify the vey small voltage of a current shunt (a couple of milliohms rated at 100A). The opamp output is shifted to 1V3 (as 0 reference) since I use the MCU internal reference of 2V56 for ADC. In case of the ADC 10-bit reading, I let 512 be equivalent to 80.0 A (+ or -).

I did the test as explained on my previous post. As expected, the displayed number alternates among nearby values due to the lack of synchronization between the samples window and the supply period. Obviously, the range of these values decreases if I decrease the current average.

So I had to add a sort of software filter to minimize this random change on the display. I am experimenting it now in order to find its configuration which could be acceptable for all possible readings (after all, its output is just for the display, hence not to be used in any calculation).

About the economical point, if someone needs a product which cannot be available in his local market (via traders), do you think Google could help him know how much the product should cost him? ;)

Anyway, I am sorry because, on these days in the least, we have no choice but living in totally different worlds though we were both created as humans on the dear planet Earth... in this huge universe ;)

Kerim

Last Edited: Tue. May 31, 2016 - 02:12 PM
Total votes: 0

Good explanation. Yes, I had no idea where you lived. That's why there is a place to put ones city and country in the forum profile. Welcome to AVRfreaks. Maybe c can be the International Language Of Engineers And Programmers That Love Peace. Sounds like you have a current sensor with bias so you can read the + and - voltage from the ct with the avr a/d. It is possible to convert this 'bipolar ac' signal to dc.... just add the bias value to all samples that are less than the bias value. Example... samples go 0 to 1023, bias is 512, just add 512 to all samples less than 512. Now all samples are 'positive'. 50 Hz is 20ms. If you take 40 or 50ms worth of samples, you can scan through and find the peak, and you can calc the average, which will include any contribution from ripple due to the battery charger. This wont be present when not charging, but it should measure the discharge accurately. Many engineers here on AVRfreaks will be able to help debug your program... The description you gave was a good start.

Imagecraft compiler user

Total votes: 0

KerimF wrote:
For instance, since my graduation (1975) I started my small private business. I used designing and producing products for the local market; according to its needs in every period of time. So I am a real independent free person who has no secret; technically and socially as well ;)

You're doing what I should of done.

You also have the ability to repair some of the out-of-market products that do arrive there then malfunction or succumb to entropy.

KerimF wrote:
... and an opamp to amplify the vey small voltage of a current shunt (a couple of milliohms rated at 100A).
A concern is the op amps you have might not be the ones you want (iow have high offset voltage); but, some op amps have an offset voltage trim.

KerimF wrote:
Anyway, I am sorry because, on these days in the least, we have no choice but living in totally different worlds though we were both created as humans on the dear planet Earth... in this huge universe ;)
That which is unsustainable will not be sustained.

Hang in there ... well.

Sustainable -

You mentioned in another post, IIRC, the lack of supply of photovoltaics.

In other parts of Earth the PV cells are approaching a relative dime a dozen.

Major PV cell production in Germany and China; a guess is a China source is more likely for you.

There is PV panel production in Jordan; but, I don't know if sourcing such from Jordan is an option for you.

Doesn't take much to make your own PV panels.

Simple CV charging a lead-acid battery up to 2A (approx 10Ah battery) by shorting the PV panel with an N-channel device via a comparator.

Not dependent on gasoline or diesel; dependent on sunshine.

A proposed solution for radio transceivers and such; might work for a small water pump.

"Dare to be naïve." - Buckminster Fuller

Total votes: 0

Hi Bob,

Since a few years I became confused about stating (or not) where I live, as you proposed, every time I join a forum. I am afraid that not all people, even engineers, are like you and I. It happened that, sometimes, when I say where I live, I was accused, by some moderators, of breaking the rules for talking politics ;) In such cases, I just left in peace after shaking off their dust of my feet ;)

As usual, my circuit has a single supply (regulated 5V). So if the opamp output gives an ADC reading above 512, the sensed current is positive, otherwise it is negative (or vice versa). Therefore, after calculating the average value of the ADC raw readings (in my case, average = total / 256 / 32), I subtract 512 to get the absolute value with a sign (saved by a flag). For instance, an inverting opamp accepts both polarities at its input if its output is biased properly (from its non-inverting input).

You are right, though I couldn't get well: "This wont be present when not charging, but it should measure the discharge accurately".

The first thought I had is to detect the peak samples so that the average could be known by dividing the sum of samples between two peaks (likely consecutive) by their numbers. Then I remembered that the battery current could be real DC mainly during discharging by a DC load (as LEDs). The case becomes worse if a random varying DC load is also present. So the program should be made intelligent to differentiate, on the fly, all different situations.

For instance, when I, as it is the case for many other mature engineers, ask a question, I do it to know if someone may have an answer which could be 'more' practical than the one I have. For example, I designed (in early 80's and I even took advantage of it, myself) a simple low-cost reliable DSB-SC demodulator (linear) that no one in the world seems having an idea of it yet ;) I started a thread about it here.

Finally, since I need measuring the battery varying voltage as well, the two ADC readings (free running) should be interleaved. So, in the window of the current samples, the number of samples is halved. I guess I will need filling each of the missing samples with the mid-value of its adjacent ones to make things better, mainly for summing up the battery charges (Ampere.Hour) [not A/H as I wrote it earlier ;) ).

Best wishes,

Kerim

Total votes: 1

Hi Kerim,

We are happy to know where people are living. It helps to know what components might be available for the solutions discussed/proposed. We have several countries represented that might cause "political" discussions on other forums. For example, Iran (hello Amin  ). But here we stay away from politics and then everyone stays happy and courteous. Our common theme for discussion is engineering (hardware and software). So as I have said before... welcome my friend.

Cheers,

Ross

Ross McKenzie ValuSoft Melbourne Australia

Total votes: 1

Hello gchapman,

It happens I am not good in repairing other's products, as others find rather impossible repairing mines ;)

About the opamp, you are right. The one I can use is standard, like LM324 or equivalent. Fortunately, my customers don't expect from me a meter (or else) to be produced for NASA or medical care ;)

I usually design and produce something in electronics that I also need (not just for money). And when its design satisfies me, I assume it will likely satisfy many people in my surrounding who may have the same need as mine. It happens that most of what I offer for sale compete similar ones made locally or imported, in respect to functions/cost. This is how I was able gaining my daily bread for about 40 years ;)

Kerim