YAPP: Battery monitoring

AngusMcDoon

Well-known member
Joined
20 Oct 2004
Messages
8,830
Location
Up some Hebridean loch
Visit site
Yet Another Pointless Project

This time - watching what's going in and out of the battery. I have a current sensor that can be used on one of the lines to the battery that continuously monitors what's going in and out, records and plots it, and totals all the ins and all the outs. Here it is running showing the display...

bmon.jpg


It takes a sample every few milliseconds and adds this to the total in/out values, averages over a second for the instantaneous displayed value, and averages over 6 minutes which is logged and plotted on a rolling 24 hour plot. The top graph is Amps - green for in and red for out. The bottom graph is volts. The vertical grid is 2 hours. The Amps and Volts axes are labelled with their units.

When it was run to take the photo it wasn't actually connected to a real battery system, rather a power supply driving a headlight bulb, hence the low volt readings. Also it was sped up 150 times to get some plots without waiting.


The processor is a cheap PIC 18F26K22 costing £2.50. It has an on-chip 10 bit ADC and also a 0.1% tolerance voltage reference. It's this one...

http://uk.farnell.com/microchip/pic18f26k22-i-sp/mcu-8bit-64k-flash-xlp-28spdip/dp/1814975

The display is the same as I have used before for YAPP projects. About £15...

http://iteadstudio.com/store/index.php?main_page=product_info&cPath=57_58&products_id=55

The current sensor is quite expensive at £15. It doesn't require any electrical connection to the circuit being sensed. Instead, the cable carrying the current to be measured is poked through a toroid. An advantage of this type of sensor is that there is no limit to the current that can be safely passed through the sensor other than what the cable can take. If overdriven the sensor will saturate, but won't be damaged by overloading which can happen with a shunt type sensors. It's this one...

http://uk.farnell.com/jsp/search/productdetail.jsp?SKU=1653524

This sensor needs a stable supply between 6 and 12 volts to operate. I am running it at 8 volts. I use a voltage divider to bring the sensor's output down to a safe value that the PIC can measure. I am running the PIC at 3.3 volts as this is the logic level required by the display.

The voltage swing from the current sensor per amp is quite small (20mV). Therefore I need to be able to measure the voltage it produces fairly accurately. The PIC can measure to 0.1% accuracy, so the resistors in the voltage divider are also 0.1%. (These are getting harder to source in through hole shape.)

While I am at it I am also measuring the boats 12V supply using the same technique.

There are quite a few voltages on this design... 12V supply, 8V current sensor, 5V display backlight, 3.3V processor. Therefore regulators abound.

A question I have pondered is can this then give a reasonable indication of the state of charge of your battery, like I believe the Nasa battery monitor claims to do. Well, despite their claiming that they can, I have my doubts. Reading around, it seems that the proportion of current that goes into a battery that remains as charge varies between 50% and 90% when charging, and 0% when fully charged as a fully charged battery will still take a small charging current. It depends on rate of charge, battery charge state, electrolyte concentration, battery age, temperature, battery technology and whether there's a marketing man in the offing. So I'm not convinced that it can be done in any meaningful way. So instead, I'm just showing the ins and the outs as a guide. If anyone has any further information on this, I'd be interested to know.

Programmed in C using the free version of the Microchip C18 compiler. I am using graphics drivers and libraries from earlier YAPPs. There is no OS running on the processor. The graphics is from first principles, i.e. writing a byte of display memory. Two fonts (small and large) are hard coded into the flash. The flash on the chip is 64kBytes, buit this app is using less than 20% of that.

Things I could have done better...

I have chosen a 125A maximum current sensor. Now I'm not really interested in the really high currents, for example engine starting, as because they are short duration they don't use many coulombs. As it is safe to pass more current through the sensor than its rated maximum measuring value, I should have got a lower maximum sensor because these can record lower currents more accurately. With my sensor I can't resolve the current to less than 50mA. I have an outboard powered electrical system, so small currents are important to me.

Things yet to do...

1) The current plot is a fixed +-10 Amp scale at the moment. This could be made zoomable. The data is logged with maximums of +-327 Amps, beyond the capability of the sensor.

2) The current sensor needs calibrating to find its zero current point, i.e. run the device with no current passing through the sensor, tell it to take a reading, and save that as the calibration point. The sensor data sheet says that this value may vary between devices. At the moment it is hard coded. The voltage response of the sensor per Amp measured is fixed and stable, if not very accurate.

3) The processor is running idle most of the time. The PIC has a whole host of low power modes and these should be used.

4) The backlight is the biggest current draw. This needs to be switched off when not needed. This will need some sort of FET I guess to switch the power. Can anyone suggest one?

5) Add a reset capability.

6) The logging can be extended to a week and associated plotting could be added.

7) The ins and outs and time running could be stored in flash so that they persist over power cycles.

8) 2 minor bugettes spotted - one in the seconds to hms conversion, and also seen -0.00 displayed. Need to remove the sign when value displayed is zero even if the full accuracy value is not quite zero.

Here it is in all its spaghetti glory...

bmon2.jpg


Sauce code and schematic (when I have drawn it) to anyone who is interested.
 
Last edited:

Hoolie

Well-known member
Joined
3 Mar 2005
Messages
8,146
Location
Hants/Lozère
Visit site
Yet Another Pointless Project

... ... ...

Things I could have done better...

I have chosen a 125A maximum current sensor. Now I'm not really interested in the really high currents, for example engine starting, as because they are short duration they don't use many coulombs. As it is safe to pass more current through the sensor than its rated maximum measuring value, I should have got a lower maximum sensor because these can record lower currents more accurately. With my sensor I can't resolve the current to less than 50mA. I have an outboard powered electrical system, so small currents are important to me.
... ... ...
As overloading does no harm, could you fit two current sensors covering both high and low currents? Assuming you have a spare input on the PIC, of course.
 

the_branflake

Member
Joined
6 Jul 2010
Messages
240
Location
Crewe, Cheshire
Visit site
That is really cool. I would be interested in trying to put one together.

Also - Could you include a readout that shows if you have taken more out than put back in at a glance or replace the in/out?
 
Last edited:

AngusMcDoon

Well-known member
Joined
20 Oct 2004
Messages
8,830
Location
Up some Hebridean loch
Visit site
As overloading does no harm, could you fit two current sensors covering both high and low currents? Assuming you have a spare input on the PIC, of course.

I thought about this and could. There are plenty of unused analog channels on the processor, and 1 remaining pin after Ed's idea. However, the sensors are £15 a pop, and being a tighwad, it was this that put me off the idea.
 

AngusMcDoon

Well-known member
Joined
20 Oct 2004
Messages
8,830
Location
Up some Hebridean loch
Visit site
That is really cool. I would be interested in trying to put one together.

Also - Could you include a readout that shows if you have taken more out than put back in at a glance or replace the in/out?

If you PM me an email address I will send the source code and schematic when I have drawn it.

That could be done, but it would only be a rough indication as charging efficiency is never 100%. I could put it where the "YAPP? YES" text is. I only out that in to fill in the space.
 
Top