As you probably know all batteries are definitely not equal, and how about that new PowerBank? Their stated capacity and their real capacity often differ quite a lot! They do for me! Especially if you buy cheap batteries! Is that cheap battery really a bargain or …? A battery capacity tester will give you a very good knowledge of what to expect from your batteries & PowerBanks and also a good point of argument if you need to talk to the seller! You take a fully charged battery and let the battery tester discharge it while keeping track of all pertinent variables. A real bore to do manually, but the Ar2uino Battery Tester does it all for you – automatically! 🙂
I recently bought some Li-Ion batteries and wanted to test their real capacity and I (naturally) decided to use my trusty old Arduino Battery Capacity Tester. As soon as I set it up again it I realized that I had a number of ideas for improvement.
There has been some interest lately in the tester that I used exclusively to test Li-Ion camera batteries with – in my Photoman Camera Review blog – so I decided to write this article about an improved and more general version of it.
The original article will still be at my Photoman Camera Review blog – because of its limited ambition in just measuring “PEPs”. The new, much improved and still simplified one, will be here! Well it is actually the circuit that was very simple to start out with that has gotten still a little simpler – the software is much more extended and more versatile – but hopefully still easy enough to use! It is easy enough for a beginner to build – hardware wise but still very versatile software wise!
Features of the Ar2uino Battery tester:
To make it clear this is an Automatic Arduino Battery Capacity Tester! I thought that the description was too long so I (stupidly) cut out the “Automatic” and “Capacity” and now it is too late to change that! It is not the current state of charge of the battery that is tested but the current capacity of the battery – and it does this automatically!
- Automatically detects most batteries: Ni-Mh, Alkaline, Lithium, Li-Ion, Li-po and PowerBanks
- Automatically selects a safe cut off voltage for each battery type
- Continuous and pulsed load
- USB connected and Standalone modes
- Automatic and manual calibration for higher precision
- Very little leak current from battery <5 µA
- Output data directly suitable for Microsoft Excel to create discharge curves and further calculations
- Permanent storage of set up data
- Easy on-screen instructions
- Very simple circuit
So how do you measure the capacity of a battery? As I stated above you take a fully charged battery and let the battery tester discharge it while keeping track of all pertinent variables, those being the battery voltage, discharge current and time. A typical Li-Ion discharge curve will look like the example on the side here. The voltage is shown on the vertical axis and capacity on the horrisontal axis. Different battery types have different safe cut off voltages. Li-Ion usually has 3.0 V.
Battery capacity for these types of small chargeable batteries is usually measured in mAh (milli Ampere hours) – that is how many mA a battery should be able to supply (in theory!) during a full hour. 1000 mAh means it should be able to supply a current of 1000 mA for 1 hour. That could theoretically be translated to 500 mA for 2 hours or 100 mA for 10 hours – or whatever combination that gives the same result. A 20 mA LED (with suitable resistor) would shine for 50 hours! In practice this above is NOT the entierely correct, because the heavier load you put on a battery the less capacity it will be able to give! And vice versa! 😉
Manufacturers have therefore decided that you should use a standard load of only 10% of the stated capacity of a battery when you measure its capacity. Often written as “0.1C”. Say you have a battery with 2400 mAh written on it – in that case you should use a load of 240 mA (=0.1 * 2400). Apply this load to your battery and measure the time before it goes below the safe cut off voltage and you can calculate the capacity by multiplying 240 mA by the time. Ex: Assume our battery goes flat after 9.5 hours with a 240 mA load. Lasting 9.5 hours would give (240*9.5=) 2280 mAh real capacity. This also implies that – if all batteries held exactly the stated capacity – all battery tests would take exactly 10 hours with the standard load!
If you, like me, are more interested to find out what the battery will deliver when I actually use it: in a powerfull flashlight with a load of 1A or when I take pictures with a camera or when the battery powers an Arduino in sleep mode over years, then you will be interested in both higher and lower loads and also in pulsed loads!
The software handles all these requirements! It can be used both for continuous discharge and for pulsed discharge. It can handle both high loads and low loads. As batteries handle quite differently, for a long high continuous load than for a number of low short loads, you can specify how you want the test to be performed. If your intended use lets the battery rest a little between loads, it will perform even better! But you should try to mimic your actual use – within reason!
I tested an unmarked Li-Ion 18650 battery with a continuous load of 700 mA and got 1300 mAh capacity.
Then same load pulsed 16 sec on/ 16 sec off (virtual load of 350 mA) and got 1950 mAh out of it.
Finally with 350 mA load, pulsed 16 secs on and 16 secs off (virtual load 175 mA) and now I got 2450 mAh!
My expectation is that with even lower load that battery would deliver even higher capacity! The disadvantage with low loads is of course that it will take quite long to test the battery – especially high capacity types! That last test above took close to 14 hours! Testing a load of say 16 mA for 1 second every hour would not be practical – you will have to settle for some calculations and a few (well educated) guesses! 😉
With very low loads (a few mA’s) over several months the capacity will also be affected by self-discharge. According to Wikipedia it’s around 1.5-2% per month. So, in theory, a battery could lose up to 25% of its capacity over a year. Way back I started a test to see how much capacity a few of my batteries would lose! Will be a separate article … soon!
Improvements in Hardware
First of all I decided to use a better transistor with lower internal resistance – thereby making the circuit a little simpler! 🙂 By using a MOSFET IRFZ44N transistor, with its very low resistance of typically just 17.5 milliOhm, no extra measurement is needed of the voltage over the transistor, because it will only be in the order of a few tenths of a percent! No need to measure that separately, but we include a tiny compensation for it in our calculations.
Remember that there are many other factors that influence the result much more than the transistor! Did you just take the battery out of the charger … or was it charged last week … what is the room temperature … is the battery new or old … has it been charged/discharged many times before … will you load it very heavily … etc?
The new simplified circuit diagram of the Ar2uino Battery Tester is on the left – I just soldered it together on a small PCB. More about construction later on! The new tester automatically handles several more battery types: Ni-Mh 1.2V, Alkaline 1.5V, Lithium 3.0V, Li-Ion/LiPo 3.7V, Li-Ion 7.4V and also the very common 5.0V PowerBanks – depending on the load you want, you will probably have to change the load resistor! There will be more on hardware construction in a later instalment!
Getting just 3 load resistors; 5.6, 12 and 22 ohm you can combine these in parallel and serial to give you a whole set of loads; 3.3, 3.8, 4.5, 5.6, 6.9, 7.8, 9.8, 12, 15.6, 22, 27.6, 34 and 39.5 ohm! I might even have missed some combinations! 🙂
Add to that a couple of higher resistances like 68 and 150 ohm and you are equipped to handle almost any battery and load combination you could possibly want!
I have included two different tables; one mA table and a Watt table! Use the mA table to find the right load resistor for the battery voltage and mA load you want! Use the Watt table to find the Watt generated for the battery voltage and the load resistor you have chosen! Make sure your load resistor has at least that wattage! The Watt table colours indicate roughly how hot the resistor will get … I would recommend never using lower than 5 W power type resistors! The greener colours indicate not so hot and yellow and redder colours indicate hot and hotter! It is difficult to declare any load “safe” depending on what you are worried about! Touching is probably OK with the greener colours gradually turning hotter and hotter until the redder colours that can even indicate even fire hazard – if you have flammable materials near or in contact with the load resistors! So take care and make sure you have free air around the load resistors! During my testing the transistor has not heated up that much…! See the infrared picture below!
Ex: Two 5 W power resistors sharing a 300 mA load @ 3.7 V certainly gets too hot to touch! That is just 1.1 W total power and only 10% of what the resistors can handle! Check out the infrared picture – those 5 W resistors are over 56°C! Maybe(?) not enough to give you blisters but sure HOT to touch!
So take into account that the resistors can get very very hot at higher loads – even if they are far from their rated power capacity! Be sure to get the correct power capacity on your load resistor – with a wide margin!
By pulsing the load on and off you can vary the load even more – from full on to full off. 🙂 It is not exactly the same as using another load resistor but can still be used to vary the load and also to simulate the actual load like when taking pictures with a camera! (A battery will behave slightly differently when exposed to short high loads than to a long low load.) Ex: You only have 10 ohm resistor and need a lower load of say 20 ohm – by pulsing the 10 ohm load with 50% of the time on and 50% off you will simulate a load of half – that is 20 ohm! The result will not be as exact and depends on the battery under test! (I will do some tests and see if I can find out some rule of thumb of the variation.)
There will be more on hardware construction in a later instalment!
Improvements in Software
I have re-written the software completely and it now prints much more complete information to the serial port! It now prints: measurement number, seconds since start, battery voltage and capacity measured so far in mAh. I use the tab character between values so that you can just hit <ctrl>A and <ctrl>C in the Arduino serial monitor window to copy the printouts to the clipboard. Then just paste with <ctrl>V it into your favorite spreadsheet (Excel) and it will fill very nicely into separate columns. All calculations are done in the software so you will only have to use your spreadsheet if you want to create a niiiice discharge curve of your battery. 🙂
I might add an option to use the all new Arduino IDE Serial Plotter – even if that does not have all the functionality we want for this type of measurement it might work – I will test it out later!
New Stand-alone Mode
The new tester can also work in the new stand-alone mode! It will not require a computer while you do the tests! Still you get the info so you can make the nice discharge curves! And know the capacity! Good?! Eh..? 🙂
NOTE: Due to memory restrictions the stored information is limited to 100 readings. This is also necessary due to the fact that it is not possible beforehand to say how long a battery test will take! The readings will automatically be evenly spread out over the full voltage span for the current battery! During printout of the stored data it will be converted to time spaced readings and it will be a fixed number of 100 readings! (Number can be changed in the code!) The restored readings will be averaged from the stored full resolution readings. The total capacity measurement will naturally also be printed out.
Calibration is necessary!
Jiri was good enough to point out (here) that if you have a varying voltage from the USB port that will affect the measurements. That is true! But the USB port on a computer is very stable and usually very close to 5.0 V – so it can definitely be used as a voltage reference! A low quality cable can introduce an unwanted voltage drop though..!
As the software now allows both USB connected and standalone modes it is now definitely necessary to use the built-in 1.1 V reference voltage for measurements – as the tester now can be powered from different sources the supply voltage can vary much more than the USB port on your computer. Most USB-chargers, for example, gives around 5.2 V! Using the built-in 1.1 V reference voltage will give us the ability to power the battery tester with different voltages without it affecting the measurement precision hardly at all.
Even if using the built-in reference generally is the best, there is a very big but: The 1.1 V reference is stable BUT it is not very calibrated, varying from 1.0 V – 1.2 V between different samples of ATmegas according to the Atmel Datasheet! That’s a full 20% span or +- 10%! So, to get our measurements more precise we will have to calibrate it! For this we will need a good multimeter, hopefully with a precision of 1% or so. You will get the same precision on the battery tester as the precision of your multimeter! 🙂
Even if the manual calibration with a good multimeter is the best, if you do not have a multimeter available or you are just lazy, you can actually auto calibrate it to the USB voltage – it will then give very stable measurements with reasonably accuracy – even when you power it from different sources!
The USB-standard specifies 5 V +- 5% which is pretty much OK! On five tested computer USB ports they were much better measuring 5.07, 5.05, 5.03, 5.01 and 4.97 Volts – most of them less than 1% off!
Be sure to use a good quality USB cable, if you auto calibrate, as it might otherwise cause a voltage drop – and throw the measurements off a bit!
The calibration will be made with the software so it is not at all difficult to make. More about that later on!
The new software
The software is named Ar2uino Battery Tester and can be used in two ways:
- Connected mode – all detailed measurements will be printed continuously to the Arduino serial monitor window. These measurements can then be used in a spreadsheet program to make discharge curves or other calculations.
- Stand-alone mode – the measurements are stored in the ATmega EEPROM – and can be printed out the next time you connect the tester to your computer.
- Both modes requires that you have set up and calibrated the tester first!
- All keyboard input you give should be ended with an <enter>.
- The two modes are selected by fitting the battery to the Tester after or before you apply power!
A FEW IMPORTANT NOTES!
- I have had nothing bad happen during all my testing and I do not want to scare you, but batteries, especially Li-Ion batteries, can be dangerous! So as a precaution make sure you know what you are dealing with by googling “dangerous li-ion batteries” and go to wikipedia and read up on batteries!
- If you put the battery in the wrong way around you will almost certainly ruin things – like the Arduino – and it could even hurt your computer!
- Be aware that the load resistor could get very hot!
- Batteries of low quality or batteries loaded very heavily could also get very hot! The tester should stop the load at a safe discharge level, voltage wise, but that is not any guarantee that something might not malfunction!
- Do not leave the battery in the tester for several hours after the finish of the measurement! This is because that a load of about 5 uA is constantly being applied through the voltage divider used to measure the battery voltage! It is a very low load, but you never know…
- Do not store a battery in the tester! On top of the constant load mentioned above the FET might spontaneously switch on and load the battery when the tester is not connected to a power source!
- Do the tests in a safe environment! This especially applies to stand-alone mode when you leave the tester unattended!
Should anything go wrong it is – as always – your own responsibility!
I decided to write two versions of the user guide, first the normal short one and then an extended one! I think that the battery tester is really quite easy to use and gives instructions on the sceen, therefore the short one – but I also wanted to give a little more information (to those interested) about what happens and therefore the extended one!
In the Arduino serial monitor: Generally just hit enter if the value shown within brackets is what you want – otherwise write it on the input line in the serial monitor and hit enter. (It will remember your entered values and suggest them the next time you run the tester.)
Default speed is 115200 bps but you can change that in the software code.
NOTE! Add both CR and LF in the Arduino serial monitor (far down in right hand corner)!
User Guide – short version
- First: Connect USB power to the Arduino!
- Then: Start the Arduino serial monitor! (115200 bps)
- Follow the instructions on the screen!
- First: Connect the new battery to the tester!
- Then: Connect power to the Arduino!
- The test will start with the set up parameters!
- During the test the LED will either be “flickering” briefly or blink according to your set pulse rate.
- When the test is finished the LED will “double-blink” quite rapidly!
- Disconnect the power source!
- Connect the tester to a computer as in “Connected mode” above to print out the result!
User Guide – extended version
This guide is a rather extensive text so you will find it here! I recommend reading it through if you get stuck using the program or just want a more thorough explanation of what happens! 🙂
It is a very simple circuit and you can very easily build it directly around a few screw terminals – actually even without soldering! I built my prototype on a piece of low cost experimenter’s board and made it into a small simplified Arduino Shield. Easy to plug in to my Arduino UNO whenever I want to test some batteries! I also brought out the pin 13 LED through a resistor so that the blinking can be seen clearly! Whatever way you go in construction, be sure to factor in that the load resistor gets hot during measurements! Also make it easy to change load resistor – if you plan to test different batteries and different loads! A screw terminal works very well! Note that I have used a 3 terminal one – to be able to combine a few load resistors! If you decide to go with high loads then also make the fatter connections in the circuit diagram fatter in reality!
The transistor is a key component but it will not have to be the MOSFET IRFZ44N – any MOSFET n-channel transistor with a decently low internal resistance (<20 mOhm) will do. Just enter its internal resistance in the software code and it will be taken into account in the calculations.
It is a very simple circuit – just connect it as in the circuit diagram – but if you still feel you need more help on construction – please let me know and I will add some more on construction!
Using the built-in 1.1 reference voltage
We have to divide the voltage down, by using resistors, so that the Arduino will see a maximum of 1.1 V when the battery voltage is 8.5 V.
According to the datasheet the ATmega should see around max 10 kOhm for the analogue input to give quick and stable readings! That means that the correct resistors to divide down the voltage should be 1.2 kOhm and 8.2 kOhm.
You can also use much higher resistors like 120 kOhm and 820 kOhm. The advantage in using these higher value resistors is that the battery under test will be less loaded from the voltage divider. We are talking about a very small load here in both cases! Less then 0.5 mA and 5 uA respectively. Also if you measure low capacity button cells and so on you want to keep that extra load as low as possible!
I recommend to go with this latter alternative – in all cases!
There is a small disadvantage in picking the higher resistor value alternative: you have to add a small 0.1 uF capacitor from the analog input to ground. That capacitor is added to stabilize the voltage during measurements. I have already included that in the circuit diagram.
You can go overboard with precision ambition on anything and this tester is no different – the software can handle most of that precision ambition but you yourself must decide how far you want to go… Using manual calibration takes care of most error sources in just one measurement – and I recommend it if you have a multimeter! Still auto calibration with the help of USB voltage is a “lazy dog” alternative that appeals to me … and usually gives a good enough precision with very little work … and very little equipment! 😉
Here is (stupid?) suggestions to improve precision even more:
Supposing you are not a “lazy dog” but you only have a very low precision multimeter: You can improve auto calibration from a computer USB port by using your low precision multimeter and measure the voltage drop over the USB cable.
Or even better just use a good quality (short) USB cable and just do auto calibrate!
Measure the voltage at the USB port and the voltage at the Arduino. It is not so easy to get at the voltage of the USB port at the computer. Easiest is to make a measurement probe from an old USB cable and plug it in to another USB port adjacent to the one you are using! Or why not make a measurement breakout box while you are at it!
Enter the difference (in mV) into the software. (At line no 54) Ex: Say you measure 5.03 V at the USB port and 4.72 V at the tester – then enter 31 (mV) into the software. By using the difference you will almost eliminate the effect of the low precision of your multimeter. Note that you will have to do this measurement while the Arduino is connected – and draws current from the USB port!
Look for the definition of variable “voltageDrop” at the beginning of the code and enter the mV drop there! Recompile and upload! Then auto calibrate!
The above handled, all that is left that affects the measurement is the precision of the voltage divider and that is dealt with by using high precision resistors. 😉 Or you can measure the voltage divider resistors before you solder them in and enter their values into the code.
Note that with manual calibration ALL dependencies (ATmega unprecision, resistor variations, USB cable voltage drop,…) will be taken into account with that single measurement! Good eh..!?
You can also measure your load resistor for higher precision and key in that better value during the set up!
Extrapolating the measurements:
This is quite tricky and is a combination of guesswork, luck and hocus pocus! 🙂 Say that you want to see how much capacity you can get out of a battery rated at 2000 mAh with very light load, say 0.5 mA. Just doing the calculation from the rated capacity indicates a testing time of 2000/0.5=4000 hours which converts to a test of over 166 days!!! NOT very practical! The obvious solution is to do a test with a higher load and calculate/guess it from that … So you would instead first decide how long you can accept the test to be and then pick a load to give that time. Say that you pick 24 hours! That would need a load of around 85 mA. (2000/24=83.333)
Assuming a fully charged Li-Ion cell (around 4.2 V) the resistor would have to be around 50 Ohms. (4.2/0.085=49.4) Nearest standard values would be 47 Ohms. The chosen resistor value will not affect the correctness of the measurement as you will use the value in the calculations!
Testing with a higher load than the planned load will show a lower capacity! Another way of saying it is that the real capacity will be higher with a lower load! At the same time will a very low load (just a few mA) be affected by self discharge that will lessen the actual capacity. If you are having a high quality battery – and goes from a high load to a very low load – you could probably add another 50-100% capacity. If you have a low quality battery you should probably instead deduct 25-50% of the capacity.
Soon I will publish my 1 year self discharge test here!
My own measurement indicates that reducing a high load to 25% load could increase capacity with close to 90%. I will soon put up some real life values here – but note that it all depends on sooo many factors that you should ony use the figures given as a rough estimate or even just a guess as how much capacity will increase when load goes down!
Well, finally, here is the code for the Ar2uino Battery Tester! Most of you, I guess, will just download and run it on your Arduino and be happy with that! Nothing wrong with that – but for myself I wanted to learn something along the way writing the code.
As I have said earlier I used to be a software developer, but that was many years ago, so even if I know how to program, my style nowadays has turned out to be more of an “ad-hoc” approach to development. Develop as little as possible to get going and then add what is necessary as I go on! I iterate a lot! Rewriting small parts! I test a lot and get insights as to how the software and electronics work together. I am not very current with object oriented development and classes and objects and inheritance so my programming is very straight forward – just read it from the beginning and when you get to the end – well you have actually reached the end! 😉 I comment the code in the hope of understanding what I did today – when I check out the code in a few years!
When I read through the code before putting it up here I very much wanted to refactor the code, rewrite it, with all my insights I mind, but I decided against it. The Ar2uino Battery Tester works as it should (as far as I know!) and I have many other areas that I want to explore – so the code will have to do in its current state!
Even if I will not rewrite the code, one thing I might do is to create a stand-alone battery powered battery tester with an integrated Arduino Pro Mini. They are so low cost today, so why use a shield at all as I have? Why not just build it right into the tester – and let it stay there? It will be using low power modes to be able to be battery driven even if the tests run for several days or even weeks! It would always be used in stand-alone mode – except for printouts and setups!
One thing that has me a little confused is the question of the license – open source or not? I have read through some and I think they are all too long and too complicated! I decided to go with the simplest of them all – the “Beer-ware” license. So if we ever meet we might have a drink together! 🙂
NOTE! Code updated to v2.2! Download new code! Read comments at line 40 to see changes!
Remember to do the tests in a safe environment!
NOTE: Right click on the link above and select “Save target as…” Due to limitations with WordPress this Arduino source code file has been given the extension “.doc”. Change the extension to “.ino”.
It compiles to 20420 bytes – which is quite a lot for “just” a battery tester. The reason it is so big is that it contains many texts! Instructions are built into the program so that it is (hopefully) easy to use. There was no reason to save space … so there you are!
(I used Arduino IDE 1.0.5 r2.)
Below is some of the things I think got some insight into during the development and testing!
All data (even texts!) is stored in RAM – of which there is only 2 kB in the Arduino UNO. Putting in too many printouts soon eats all available RAM. What to do? Put the texts in the same flash memory as where the code resides – of which you have 32 kB! I wrote some short macros to simplify the printout. Check them out in the beginning of the code (around line 30)! Very hand here and I will probably have use for them in the future as well!
Storing data in internal EEPROM is quite simple but making a memory map and writing special functions for accessing the EEPROM simplifies even more! Very hand to be able to store readings when in stand-alone mode so that they can be printed out later on! EEPROM is also handy to store set up parameters in!
FETs (Field Effect Transistors) are VERY light driven! They might even switch on just from the static electricity in the air! So make sure they are at 0 V (“grounded”) when you want them off!
The one thing that was giving me the most trouble, sorry I meant gave me the most insights 😉 , was using the ADC to read analogue values! It is very easy to get unstable analogue readings if you do not code right and also if your electrical layout and construction is unsuitable! As this is really a key feature of the tester, I included the ability to do a analogue stability test if you connect a battery and select “I” for Info. It will print out how stable the ADC readings are! For some time I thought that there was something very much like Harry Potter mixed into the ADC of the Atmega328. But it cleared eventually and I am now quite confident that magic has nothing to do with it! But I still have a few lines of code in there to do a little magic – but I am unsure of if they are really needed! But they can’t do any harm ehh…? 🙂
Related to the analogue readings is the auto-calibration. After I got the idea it was quite fun to develop and the very talented Jean-Claude Wippler at Jeelabs had a nice code example that I could use to read the supply voltage – and with a little coding of my own I could use that to auto-calibrate the tester! Very handy I think! 🙂 It is not quite as good as manual calibration with a precision multimeter but it is quite a lot better than to go without any calibration at all! The ability to read the supply voltage will be very handy in my Home Automation projects later on!
Next up is the communication of parameters and printouts to and fro the PC! I ended up with writing a few functions to simplify that! They will also be of use in future projects, I am sure!
Research into all kinds of batteries was very interesting and testing quite a number of very cheap batteries bought from sellers on eBay has taught me that there are reality and fantasy also when it comes to battery capacity! A fantasy figure of 8000 mAh turned out to be 255 mAh in one case! 😉 Getting genuine well known brand batteries generally gets much closer to the stated capacities!
The stand-alone mode offered a few challenges! When you measure an unknown battery (or a known one as well!) you can not beforehand know how long that test will take at the selected load! This makes it impossible to store all the measurements in EEPROM – that is only 1 kB! Will it be 25 measurements or 1500? The trick is to change from time period measurements (ex every 20 seconds) to voltage based measurements (ex every 12 mV). So in the EEPROM only voltage based measurements are stored! To simplify the creation of nice discharge curves in Microsoft Excel I convert the voltage based measurements (that only give a sloping straight line with Excel standard settings) back to time based measurements that gives a nice discharge curve with standard settings!
One final lesson – that is just a repeat lesson since earlier – is that testing is SO IMPORTANT and it must be allowed to take time! That is the main reason that this last bit of stand-alone code took some extra time!
Well, this has been an interesting little journey into battery land and I think I have gained insights into batteries and their capacity that will come in useful in the Home Automation project!
This concludes the article on the Ar2uino Battery Tester!
See you soon!