Thursday 31 January 2013

First Graph

I now have the first graph of the battery onitor output as captured to the SD card!
Plot of voltage over time

The graph is a plot of the voltage of my battery bank from just after midnight until just before 8am in the morning.

Two important facts:

  1. My battery bank appears to be dying. The internal resistance of the batteries seems much higher than it should be. This means a load on the battery drops the voltage more than it should
  2. The voltage measurements are not accurate. According to my digital multimeter the tops of those peaks should be closer to 12.8v. So I think we are reading approx 0.1v too low. I believe this is due to a voltage drop over the earth connection in my test setup.
I had the compressor fridge on throughout the night and the cyclic nature of the plot is the fridge compressor kicking in. It was about 15 degrees C in the van and the fridge was kept closed and was fairly empty.
I had the Webasto diesel heater on at the start of the plot so you can it brought the voltage down low as the glow plug was on. I ran the heater twice more: around 1:45am for about half an hour, and again at around 7:20am for about half an hour.

Zoom in of one section
Zooming in you can see that when the fridge kicks in the voltage drop is around 0.24V which if the fridge is drawing around 3A equates to an internal resistance on the battery of around 80 milliohms. Which whilst better than previous measurements still seems pretty high.

The time is missing from the X-axis, but the fridge is kicking in about every 5 minutes.


Wednesday 30 January 2013

Damn... dead batteries :(

This evening I've been out in the van playing around with the battery monitor as it stands. I still don't have my shunt yet, so can't connect it up to read current. However it should still be able to read voltage (via the INA219).

Initial results are a bit disappointing.  The readings I was getting from the INA219 were wildly different from the readings I was getting from my digital voltmeter. Now, my digital voltmeter is only a cheap one from Maplins, so it could be inaccurate too, but its readings do correspond to what my Stecca PR3030 solar regulator int he van say. So on that basis I think the battery monitor is the one reading wrong :(

It would seem that the way I had connected it up to test it (via a cigarette lighter socket in the van, supplying a nominal 12v to the Arduino) didn't work to well. The reason being the ground connection. It would appear that there was a 0.3v potential difference between the ground on the Arduino and the ground of the battery terminals. This means that the ground path from the lighter socket back to the battery (via a convoluted route, including the Stecca PR3030) is not all that good. When I connected a wire directly the readings seemed a bit better, but were still quite a way off. I'm hoping that when I connect it up properly via the shunt and with a direct connection to the battery negative terminals that I will get a more accurate reading.

But the bigger... and worse news... its seems that my batteries are dead :( I switched on a light in the van drawing 1.4A from the battery as measured by putting my digital voltmeter between the battery terminal and the wire normally from it. The voltage drop as recorded by the DVM was from 12.80 - 12.63 volts. ie. 0.17v. Using Ohms law that means 0.17V / 1.4A = 0.121 Ohms. Or 121 milliohms. This is the internal resistance of the battery, and should normally be in the range of 5 - 10 milliohms. 121 milliohms is very high and indicates that there is a problem with the batteries. All three batteries of mine exhibited similar behaviour. [Thanks to eagle eyed Stewie for the correction that at 1am this morning I wrote amps instead of ohms for the resistance figures above]

The batteries are coming up to 3 years old, and have been used quite a bit, but I am pretty careful with them and they never get left in a discharged state, so I am a bit disappointed that they have died already. I was hoping to get at least another year or two out of them. So I'm going to do a bit more investigation, but seems like these batteries might have had it :(

Tuesday 29 January 2013

Idea for SoC algorithm

I'm still waiting for the 50A shunt to turn up from China, so can't connect the battery monitor so far to test it out. My plans are to remove a single 115Ah battery from my van (of the three) and bring it indoors and connect it up in a stable, warm environment for initial tests. I should be able to then connect up the shunt, and wire up a spare cigarette lighter socket I have lying around and plug my small 150W inverter into it. I can then plug a desk lamp into the inverter. This should give me a nice stable load of a few amps.

I can then run the battery down, with the monitor connected and logging to SD card, and see how the voltage varies with the load. It will also give me a chance to try and estimate the number of amp-hours the battery can hold. My batteries are coming up to 3 years old this summer, so it will be interesting to see how they are still performing.

Last night I started coding some of the Arduino software to deal with estimating the state of charge of the batteries.

There are two types of battery monitor, the ones that just look at voltage, and the ones that count the charge going in/out of the battery. I'm hoping to do a hybrid approach of both. You can only really get a good estimate of the SoC by looking at the voltage if the battery has been 'at rest' for some time. This gives you an absolute reading of the SoC. Counting the charge in and out can be done at any time, but only gives you a relative SoC reading and is prone to drift. You also need to calculate things like charging factor (e.g. it can take 1.2-1.3A charging input to store 1A of charge). And deal with Peukerts law (drawing a large current is less efficient than a small current).

The problem with voltage readings is that a battery in a motorhome is seldom 'at rest' due to continuous loads such as the fridge compressor kicking in and lighting etc. However my battery bank is designed to let us run for 3-4 days off-grid and there *are* times when the batteries are at rest (e.g. at night when lights are off and fridge comes on less often as isn't opened as much, and cooler ambient temperature).

So my cunning plan is to try and periodically take 'at rest' readings when the battery *is* at rest. And the estimate between those readings by counting the charge going in and out. Hopefully when the next 'true' reading is taken it can be compared against the estimate so far by counting the current and the parameters used for counting the current can be updated to provide more accurate estimates.

We should be able to then extrapolate these readings to calculate the effective capacity of the battery bank. As this will be based on actual voltage readings, it should effectively compensate automatically for battery age and temperature.

 I decided to work on a state system in which the batteries can be in one of four states: UNKNOWN, ATREST, CHARGING, LOAD. When it starts up the batteries are in UNKNOWN state, and then progress though the other states as things happen. ATREST is defined as when the load on the batteries is between 0.1A and -0.1A.

When the state changes, I store the time of the state change and the last state in some variables. I then check if we are in the ATREST state and how long since the last state change. If the last state was CHARGING then we must be ATREST for at least an hour before we take a voltage reading. If the last state was LOAD then we must have been ATREST for 5 minutes. These timings are just a guess, but from what I've observed of the batteries before they seem to 'settle' much quicker after a load then after charging. I think this is due to 'surface charge' on the batteries.

Once I have a 'true' voltage reading after being at rest, I then calculate the SoC as:
soc = 100 - ((12.80 - voltage) * 100)
Again, we can tweak this once I get a better idea, but seems about right, with 100% SoC being 12.80v and 50% SoC being at 12.3v.

When I take a 'true' reading I will zero a variable the counts the charge that has gone through the system. Every second when I take a current and voltage reading this counter will be updated with the Ah that has been removed/added to the battery and will be used to estimate the SoC since the last 'true' reading. When the next true reading happens I can then compare it to my estimated SoC and adjust variables accordingly.

Wednesday 23 January 2013

Initial Success!

The Data Logging Shield with INA219 breakout soldered on,
on top of an Arduino Uno
So, the Data Logger shield by Adafruit turned up today. This is their new version which is full assembled, so very little work to do. All you have to do is solder on the header pins (plain or stacking depending on what you want to do).

This shield has a real time clock, SD card reader and a small prototyping area. So ideal for what I want. I soldered the header pins on then attached it to my Arduino Uno. I uploaded a number of test sketches from the Adafruit website to test it and within minutes had both the RTC and the SD card slot working. I have a donated SD card, but had left it in the office today as wasn't expecting the data logger shield to turn up so soon. So, being impatient I pulled out the SD card from my camera to use that. I wasn't expecting it to work, as the cards need to be formatted, and apparently the SD card reader sometimes have trouble with cheap cards.

But lo and behold, the test sketch was able to open the SD card and print out a directory listing of the files on the card... being a camera card this took quite some time!

So now I knew the shield was working, I set about soldering on the INA219 breakout board (also from Adafruit) to the card. This is the current monitor that I'm testing out which read both the current (across a shunt) and the voltage and reports them back over the I2C bus. The I2C bus is already in use on this card for the RTC, but you can have multiple devices on the bus as they all have separate addresses.

I soldered the breakout board to the shield, and then soldered up the 4 necessary wires: +5v, ground, SDA and SDC. The shield has a couple of nice holes to connect SDA and SDC up. It took me a few goes to get the hang of soldering up wires on the prototype area. So far I've only ever used stripboard, and not had to create my own bridging connections before. I worked out the best way (after some trial and error) was to run the wires on the front of the board and push them through to the back through an adjacent hole to what I wanted to connect to. I then pushed the bare wire over and soldered it to the adjacent pin.

I then set about writing a sketch to see if I could get everything working together. Taking examples from all the various components (INA219, RTC, SD card) I was able to put it all together. The only piece I'm missing at the moment is a temperature sensor. I want to get some pcb screw terminals to solder to the board so I can then fit in a Dallas Semi 1-wire temp sensor to the board. I've seen some nice assemblies on eBay that are the 1-wire temp sensor enclosed in a small metal tube, sealed waterproof and on the end of 1 meter of cable. These would  be idea, as ultimately I'd like to have the temperature sensor physically connected to the battery, probably to the negative post on the battery. I should get enough conduction to get a decent reading of the battery itself. I could probably crimp the temp sensor into a ring terminal as a way to mechanically attach it to the battery posts.

Values recorded from the battery monitor stored on the SD card
Anyway.... it all works! I am now talking a time, voltage, and current reading once per second and writing it to the SD card. I'm flushing the SD card every 10 seconds. After a while I pulled the SD card out and connected it to my laptop (via my camera) and opened up the file in OpenOffice... tada! So I just need to work out a way to get OpenOffice Calc to recognise a unix timestamp and I can then graph the results. At the moment it is not connected to a shunt and the INA219 has not been calibrated, so is showing odd readings. Hopefully the 50A shunt I ordered from eBay should turn up soon and I can fit this to the van and test it out.

The next stage is going to be to have some way to set a nominal battery capacity in Ah and start keeping track of the current going in and out and having a first stab at trying to get a state of charge estimation. This is where it is going to start getting all fun with Peukerts law, and related things!

Monday 21 January 2013

SD cards and 3.3v

So it seems that whilst the SD card reader I ordered from China *says* that it will work with 5v or 3.3v it doesn't actually have the necessary circuitry to drop the 5v signals down to 3.3v. This means that there is a very good chance it will damage any SD card put in it if run from 5v.

I have a couple of options:
  1. Ignore it and hope 5v won't blow my SD card up
  2. Build some level shifting circuitry to drop the 5v signals down to 3.3v
  3. Buy an existing Arduino shield with a SD card reader and that circuity built in.
No, I can can make my own (option 2) and there are instructions online from someone else on how to do this. But to be honest that kind of sounds like too much work. Well, I'm not afraid of work per se, but there is an expression in the IT world called Yak Shaving and this is apt here.

Actually, Adafruit make a quite nice Datalogger Shield which contains both a SD card reader (and level shifter) and also a Realtime Clock. It also has a prototyping area on there on which I could put the voltage/current sensors and the temperature sensor.

This would increase the cost of the project a little bit (maybe about £10) but would mean a lot more of the system is pre-assembled and would make it much easier for others to replicate.

This actually means I could get all of my circuitry I need for this project on a single shield, which could then be put on top of any Arduino (ie. an official 'full fat' Arduino or a bare-bones one such as the Xino Basic that I plan to use.

Sunday 20 January 2013

Measuring Voltage and Current

The main thing this system needs to do is measure the voltage of the batteries and the current going to and from them. This may sound simple, but isn't necessary so. As it is the main function of this device it deserves its own blog post.

At the moment I have two different approaches to measure the current that I am investigating:

There are pro's and cons to each approach, so I've got one of each sensor and will have a play with each of them to see how they go.

ACS714 Hall Effect Sensor

This sensor is good in that is is isolated from the current wire and there is no electrical connection between the sensor and the system being sensed. The sensors however can only measure up to 30A each way. My Sterling B2B charger can put in 50A which would overload this sensor. The sensor would not be damaged, but would just read a maximum of 30A. I have 3 batteries in parallel and so I could attach 3 separate sensors, one to each battery. This means a total of 50/3A or 16A would go through each sensor to each battery. I can then sum the current in software. This has the advantage that I could detect if there are any abnormalities in the batteries, such as a failed cell, that would cause one battery to take a higher/lower current than the others.

One of the main disadvantages of this sensor is that the current itself has to flow through the sensor. This means running a wire from the positive terminal of the battery to the sensor and then from the sensor to my main power distribution point and fusebox. The sensor seems pretty small and I'm not sure how it will like having 16A pushed through it for long periods of time.

The sensor outputs an analog voltage between 0 and 5v that corresponds to a current reading of -30A to +30A. The Arduino has 6 analog inputs that have a 10-bit analog to digital convertor (ADC) . This means it reports values of 0 to 1024. Whilst this will give us a current reading, I would still need a way to get a voltage reading. This is normally done using a pair of resistors to divide the voltage to an appropriate value to be read. In our case, by using, say a 10K Ohm and a 20K Ohm resistor we can scale a voltage of 0 - 15V to 0 - 5V to be read by the Arduino. This does however require us to get very accurate resistors. Another issue is that the Arduino's ADC relies on having an accurate 5v feed in order to accurate measure inputs. If the 5V feed fluctuates or is not exactly 5.00V then the readings from the ADC will be inaccurate.

INA219 High Side DC Current Sensor

This sensor is designed to read the very small voltage drop that occurs over a resistor when a current passes through it. This is the approach the Nasa BM-1 takes and any other system you see that requires a 'shunt' to be fitted. The advantage of this system is that it can be scaled to measure any current by using different resistance shunts. So I could measure 50A in/out of the whole battery bank by using a suitably sized shunt. The accuracy would depend on the accuracy of the shunt, but you can calibrate the sensor based on the resistance of the shunt, so I can measure the shunt resistance once it is installed and adjust accordingly.

This sensor communicates with the Arduino via I2C which is a digital protocol, rather than an analog signal. This means that we are not reliant on the Arduino's ADC to provide accurate readings. Multiple sensors can be attached to the same I2C bus should I want to take separate readings from each battery as above. This sensor also measures the voltage as well, which means we don't need to bother with a voltage divider as detailed above. A library has been created to read the INA219 sensor from an Arduino, which makes it very easy to interface with.

The INA219 is a very very small chip and way beyond my soldering skills to try and solder it directly. Luckily Adafruit have created a INA219 breakout board for it which makes it much easier to work with. The disadvantage with this board is that it has a 0.1 Ohm resistor already attached to it to measure the current. This means that it is only able to measure up to 3.2A. You can however un-solder the 0.1 Ohm resistor and run wires to an external shunt. So I have a 50A shunt on order, again from China via eBay. Once it arrives we will see how it works with the INA219.

Hardware Needed

To build this I'm going to be using the Arduino platform. This is a microprocessor platform with a very big community online and lots of tools and accessories. There are a number of 'official' Arduino boards (e.g. the Arduino Uno or Arduino Leonardo) that are developed, plus a whole load of 3rd party compatible boards. they all share a standard shape so that additional boards called 'shields' can be stacked on top. Shields include things like relay boards, wireless boards, motor driver boards, etc.

I'm currently prototyping with an Arduino Uno as that has a USB port and can be easily plugged in to my laptop and programmed. For the actual system I put into the van I'm going to build it on a more 'barebones' board that is cheaper and uses less power.

The additional items needed to be added to a base Arduino to provide the functionality I need:

  • A Real Time Clock (RTC) -- needed to be able to record the current time and date for each voltage and current reading
  • A SD Card reader -- needed to have somewhere to write the readings we are taking from the batteries
  • A way to measure the current being drawn
  • A way to measure the voltage of the batteries
  • A way to measure the temperature of the batteries
I am looking at using a Xino Basic as it is very cheap (£7.68 including the voltage regulator, processor and VAT) and has a small prototyping area directly on the board onto which I should be able to attach the RTC, SD card reader, and sensors.

I've ordered a RTC (£1.73) and a SD card reader (£1.24) from eBay. Both are coming from China and so will take a month to get here, but at that price I couldn't resist. To measure temperature I'm using a Dallas Semiconductor DS18B20 sensor. This is a digital temperature sensor that uses a '1-wire' bus. This means multiple sensor can be daisy chained together on the same pair of wires.

As for measuring voltage and current... that is worthy of a whole separate post....

Battery Terminal Voltage

One of the main measurements you can take of a lead acid battery is measuring the voltage across the terminals. The battery is nominally called a 12 volt battery, but the voltage across the terminals varies quite a bit. Depending on the exact battery type, a fully charged battery is 12.8v and is empty at around 11.8v. When on charge it can go up as high as 14.4v. So being able to measure the battery voltage to an accuracy of 0.1v is pretty important.

To make things more complicated, a lead acid battery has an internal resistance that varies with the state of charge. This means that the voltage measured on the terminals varies with the current being drawn from the battery. Ohm's law states that voltage = current * resistance. And so as the current increases the voltage drop over the internal resistance increases.

So your battery might say it is 12.5v, which might mean that it is about 70% charged with no load on it, or it might be 100% charged, but under a load of maybe 5-6 amps.

So in an ideal world the only way to get a true reading of the state of charge of a battery is to take a reading when it has been sat still for several hours with no charge going in or out ('at rest'). However in the general life of a battery in a motorhome this ideal scenario is very unlikely. The battery is constantly under a load (e.g. the fridge compressor kicking in every 10 minutes or so). Or the solar panels are putting some charge in.

There are two general ways to try and monitor the state of the battery:

  1. Counting the current going in and out (coulomb counting)
  2. Monitoring the raw voltage very closely
The Nasa BM-1 is an example of the first approach. It monitors the current very frequently (maybe many times per second) and keeps track of the current drawn over time. If you have a 100Ah battery and the BM-1 monitors that you draw an average of 2 amps over 24 hours then it knows you have drawn 48 amp-hours (Ah) and so have drawn about half of energy out of the battery. In most cases you don't want to go below around 50% repeatedly from a leisure battery otherwise you will shorten it's life.

To make matters even more complicated there is an effect called Peukert's law which states that the greater the current you draw from the battery, the lower its apparent capacity is. This is why proper leisure batteriers give several 'capacities' depending on how much current is drawn from them. This is normally expressed as a fraction of the total capacity. e.g they may say a 'C/20 capacity of 115Ah' This means this if you draw the capacity of the battery (115Ah) over 20 hours (ie. 5.75A) then it will be a 115Ah battery. If, instead, you draw the total capacity over 5 hours, C/5, ie 23 amps, then is will appear more like a 90Ah battery.

So in short, the greater the current you draw from the battery, the smaller the effective battery capacity.

One battery monitor which doesn't use coulomb counting is the SmartGauge. This *just* monitors the battery voltage and from there attempts to mathematically model the battery and estimate the state of charge. To be honest, it sounds like a good idea, but again, as it doesn't monitor the current being drawn I can't see how it can accurately determine the state of charge of a battery under load.

Oh, and if that is not complicated enough, temperature also plays an effect. The ideal temperature for a lead acid battery is 25 degrees C. If the battery is used in temperatures lower than that then the capacity appears smallers. If above that then the capacity appears greater -- but the battery's life will be shortened by higher temps. Think about how your car battery has a harder time starting the car/van when it is cold. This is why.

A battery at 0 degrees C will only 90% of that of one at 25 degrees.

There is a large number of research papers out there that try to formulate ways to estimate the terminal voltage of a lead acid battery under load. I'm hoping to be able to understand some of them and try and use some of the equations in this project.

Battery Chemistry

The idea of this project is to be able to monitor the leisure batteries in my motorhome. These batteries run things like the fridge, lighting, water pump, etc in the van. It also supplies power to the heater (a diesel-fired Webasto Thermotop) for the water pump, glow plug etc.

There are 3 x 115 Ah leisure batteries connected in parallel giving me a 345Ah 12V battery bank. This is charged by either a 64W solar panel on the roof and/or a 50A Sterling Battery to Battery charger that charges the batteries when the engine is running.

So first to mention, lead acid batteries are pretty much the oldest form of battery type and have stood the test of time well. They are fairly robust (compared to battery chemistries like Lithium Polymer), and fairly forgiving of abuse. That said there is accepted best practise with them, and by using them properly you can extend their life and performance significantly.

Dealing with Lead Acid batteries though is quite a complex issue when you get under the covers. Trying to answer seemingly simple questions such as 'how much charge do I have left?' or 'How much longer until my batteries are empty?' are actually very complex and dependant on a number of factors such as temperature and current load on the batteries.

I am *not* an expert in this, and don't profess to be. I've done a fair bit of reading on the subject and do think I know what I'm talking about, but please feel free to correct me at any point! I'm also not aiming to get a 100% accurate answer. I want an answer that is 'good enough' but at the same time I want a reasonable confidence that the answer is fairly accurate.

And so it begins

And so it begins.... this is an attempt to build an Open Source battery monitor for my motorhome. So why do I want to do this? Several reasons: cost, flexibility, and... just for interest.

The idea is to build something akin to the NASA BM-1 battery monitor. This is a unit that retails in the UK for around £95 incl VAT. So I'm wanting to build something that is cheaper than that.

Three specific things I want to be able to do with this unit, which you can't with the BM-1:

  1. Output the voltage, current and temperature readings real time via a serial port (so it can interface with another general motorhome control project I'm working on)
  2. Record the voltage, current, and temperature to some kind of storage (an SD card most likely) every few seconds so that the data can be subsequently graphed on a computer to get a better idea of battery performance.
  3. Be able to set the meaning of 'empty' for the battery. The BM-1 tells you what the state of charge is as a percentage, but with lead acid batteries you don't want to take them down below 50% state of charge regularly. So when the BM-1 says 50% left, does that mean 50% until the battery is half empty, or 50% until the battery is at 0% and damaged.
I'm going to be building this on top of an Arduino-based system. No doubt there will be mistakes along the way and this might end up costing me more than a BM-1 in the process of learning, but hopefully the end result will be something that can easily be replicated by anyone else.