Author Topic: LITHIUM BATTERY TESTING WITH A UNO  (Read 2664 times)

0 Members and 1 Guest are viewing this topic.


  • Hero Member
  • *****
  • Posts: 1088
  • Country: us
« on: December 12, 2016, 09:52:25 AM »
A short while ago there was a discussion about building lithium battery packs
out of old 18650 cells found in laptop battery packs.  I too have a collection
of these and have used them for LED flashlights.  It is generally easy to find
the bad pair of of batteries in the packs that caused them to be discarded. But,
just how good are the other ones in the pack. This seemed like a nice little
demonstrator project for a UNO. This shows how you can program a UNO to solve a
momentary problem adding a few extra parts and then it can be taken apart and
used for another project.  It reads out in amp minutes and will overload the
counter at a little over 5AH.  It also has an analog voltage output that can be
read by a digital meter once filtered.  The range is 0-5V corresponding to 0 to
255 amp minutes.  The counter will overload before that time (300 A min), still
more than sufficient for testing a pair of 18650 batteries. Never parallel more
than two batteries together without fusing for each pair.There is an easy fix for
this and a chance to improve your skills. You won't learn if I just hand working
programs to you.  This does work sufficiently well as it is to be quite useful.
The voltage and resistance is known. From that the amps could be calculated each
minute and added together.  Rough estimates are more than adequate to evaluate a
battery. There can easily be a 10% difference charging the battery with the same

When first powered, a HELP screen appears in the compilers TOOLS section on the
serial screen. It is calibrated for 1A standard test when "S" is entered. Currents
are based on 3.7V with a 2.5 ohm resistor (two 5ohm 10W resistors in parallel). You
can change resistances for whatever you have. Currents can be adjusted on the screen
to whatever is desired by choosing set values and incrementing or decrementing from
them.  A timer gives the elapsed time of the test.  It doesn't start till a PWM
load value is entered.  The current is calculated by MAX CURRENT * PWM / 255. 
When battery voltage drops below a set value the test is ended and DONE appears on
the screen.  The last elapsed time, Amp Minute and current are saved. R is entered
on the screen to reset the program. Then select a current Any preference for another
current can be added to the program.  Just select a convenient keyboard character and
edit the program to include that.  Remember, you must type that character in the
upper box and then click SEND.

If there are questions, please ask.  Not going to write elaborate explanations for
a program that isn't used. This is probably the most useful program for the majority
of people here.

   LIPO_BAT_TEST program for testing AH capacity of battery
 1.  select various PWM for current draw
 2.  Read battery voltage & stop at minimum
 CREATED 12/10/2016
 REVISED 12/12/2016

// Declare variables as integers and set initial values

int AI0            = 0;           // Battery raw A/D conversion
int AI1            = 0;           // spare
int kbd            = 'h';         // start with help screen
int blinktime      = 0;           // blink counter
int PWMdrv         = 0;           // PWM drive
int LASTpwm        = 0;           // final PWM drive
int battery        = 0;
int END            = 3600;
int TOTAL          = 0;
int AMPmin         = 0;
int DONE           = 0;
int seconds        = 0;
int minutes        = 0;
int oneamp         = 175;         // PWM for one amp
void setup() {               
// initialize the digital pins
// Pin 13 has LED on Arduino UNO board

pinMode(13,  OUTPUT);               // board LED

Serial.begin (9600);                // Set up serial monitor routine

void loop() {                       // This is the start of the program

// This section reads the analog signals.  Range is 0 to 5V DC or 0 to 1023 counts or 4.9mV per count

  AI0  = analogRead (0);             // Anolog In.  Read the battery voltage  PIN #A0  BATTERY
  AI1  = analogRead (1);             // Anolog In.    PIN #A1  36V PANEL
  battery = battery - battery/ 6;    // subtract one average reading
  battery = battery + AI0;           // add in a new reading
  AMPmin = minutes * (PWMdrv *100/ oneamp) / 100;     // calculate amp minutes
  if (battery <= END && PWMdrv > 0 && seconds > 10)   // check battery if A/D stable > 1 minute
  {                                                   // and  drive is on
    TOTAL      = AMPmin;                              // end amp minute
    DONE       = 1;                                   // end of test
    LASTpwm    = PWMdrv;                              // end PWM drive
    PWMdrv     = 0;                                   // turn off load

  // This section sends out serial data contained in the program. It has no actual functional use in the program.  It can be viewed by
  // sing SERIAL MONITOR in TOOLS when the ARDUINO programmer is running.  The program can run without any serial monitor connected. 
  // Some diagnostic sectin like this should be included in all programs.  It will save a lot of time in the development of programs.

  if (Serial.available() > 0) kbd =;    // look for and get keyboard character 
  // kbd is the character returned from keyboard

  //             COMMAND FOR PWM TEST
    if (kbd == '0') PWMdrv  = 0;          // OFF
    if (kbd == '1') PWMdrv  = 10;         
    if (kbd == '2') PWMdrv  = 20;         
    if (kbd == '3') PWMdrv  = 30;             
    if (kbd == '4') PWMdrv  = 40;           
    if (kbd == '5') PWMdrv  = 50;             
    if (kbd == '6') PWMdrv  = 60;         
    if (kbd == '7') PWMdrv  = 70;         
    if (kbd == '8') PWMdrv  = 80;         
    if (kbd == '9') PWMdrv  = 90;       
    if (kbd == '-') PWMdrv  = PWMdrv - 1;
    if (kbd == '+') PWMdrv  = PWMdrv + 1;
    if (kbd == 'm' || kbd == 'M') PWMdrv  = PWMdrv + 100;
    if (kbd == 'i' || kbd == 'I') PWMdrv  = PWMdrv + 10;
    if (kbd == 'd' || kbd == 'D') PWMdrv  = PWMdrv - 10;
    if (kbd == 'f' || kbd == 'F') PWMdrv  = 255;
    if (kbd == 's' || kbd == 'S') PWMdrv  = oneamp;  // Standard 1A test @2.5 ohm
    if (kbd == 'r' || kbd == 'R')                    // RESET everything
      PWMdrv   = 0;
      seconds  = 0;
      minutes  = 0;
      TOTAL    = 0;
      DONE     = 0;
    kbd == 'z';                 // reset keyboard to non used character each loop
  //                                        HELP SCREEN
  if (kbd == 'H' || kbd == 'h')       // if H send out HELP screen
    Serial.println("  ");
    Serial.println("  ");
    Serial.println(" 0 OFF ");                // load OFF
    Serial.println(" 1-9   10 to 90 ");       // preset PWM
    Serial.println(" + increment by one ");
    Serial.println(" - decrement by one ");
    Serial.println(" I increment by ten ");
    Serial.println(" D decrement by ten ");
    Serial.println(" M increment by 100 ");
    Serial.println(" F 255 ON ");             // max load
    Serial.println(" S STANDARD 1A TEST ");
    Serial.println(" R RESET TEST ");
    Serial.println("  ");
    Serial.println("  ");
    delay (5000);          // keep screen for five seconds
    kbd = 'z';   
  if (PWMdrv >255) PWMdrv = 255;   // set upper limit of PWM
  if (PWMdrv < 0) PWMdrv = 0;      // set lower limit of PWM

  //                                  BLINK BOARD LED and COUNT TIME
  // This section blinks the board LED when the micro is working.
  // seuenced using a count routine. 

  blinktime = blinktime + 1;       // increment blink counter
  if (blinktime  == 100)           // 100 ish is about 1 second
    blinktime = 0;                 // Reset blink counter
    if (DONE == 0 && PWMdrv > 0) seconds = seconds +1;   // increment seconds if running
  if (blinktime  == 0 && seconds >= 60)   // 60 is 1 minute
    minutes = minutes +1;                 // add a minute
    seconds = 0;                          // reset seconds
  analogWrite(10, PWMdrv);                 // analogWrite values from 0 to 255
  delay (10);                              // delay creates second interval

  //          BLINK LED
  if (blinktime == 0) digitalWrite (13,LOW);     
  if (blinktime == 5) digitalWrite (13,HIGH);   // on blink
  if (blinktime == 8) digitalWrite (13,LOW);
  if (PWMdrv >= 1 && blinktime == 40) digitalWrite (13, HIGH);   // indicate LOAD ON   
  if (blinktime == 60) digitalWrite (13, LOW);                    // indicator LED OFF

  if (PWMdrv >= 100 && blinktime == 80) digitalWrite (13, HIGH);    // indicator LED ON   
  if (blinktime == 90) digitalWrite (13, LOW);                      // indicator LED OFF
  //              SCREEN PRINT ROUTINE
  if ( blinktime == 0)
    Serial.print (AI0);
    Serial.print (" ");
    Serial.print (battery);
    Serial.print ("mV  ");
    Serial.print (END);
    Serial.print (" END  ");
    Serial.print (PWMdrv);
    Serial.print (" PWM   ");
    Serial.print (minutes);
    Serial.print (":");
    Serial.print (seconds);
    Serial.print (" elapsed  ");
    if (DONE == 0) Serial.print (AMPmin);     // overloads at 327Amp Min
    else Serial.print (TOTAL);
    Serial.print (" AMP min  ");
    if (DONE == 0) Serial.print ((float)PWMdrv / oneamp);
    else Serial.print ((float)LASTpwm / oneamp);
    Serial.print ("A  ");
    if (DONE == 1) Serial.print(" DONE");   // indicate test finished
    Serial.println(" ");       // start new line
  // filter with a 4 to 5K resistor and 2.2uF cap or higher
  // Voltage is divided by two because 255 = 5.1V.  The division
  // will make 128 = 2.55V  Normal micro supply voltage will be between
  // 5.05V and 5.2V   A CAL for 100 is included
                                              // output is X2 volts
    if (DONE == 0) analogWrite (5, AMPmin);   // overloads at 255 Amp Min
    else analogWrite (5, TOTAL);              // or about 4AH
    analogWrite (6, 100);                        // CAL PIN FOR 100 AMP MIN
}     // END of the program loop

Bruce S

  • Global Moderator
  • Super Hero Member Plus
  • *****
  • Posts: 4581
  • Country: us
  • USA
« Reply #1 on: December 12, 2016, 10:25:43 AM »
 I was just thinking about a home-based way of finding the "health" on the batteries I have. Most of the time I make use of the department's $6k professional charger/tester.
This will certainly help me when I don't that available.
I'm going to need more of these UNOs

A kind word often goes unsaid BUT never goes unheard


  • Sr. Member
  • ****
  • Posts: 493
  • Country: au
« Reply #2 on: December 12, 2016, 07:09:33 PM »


Thank you for this sketch, it is yet another of the many you have put up that I am interested in and would love to build.
With almost a heady 24Hours since my geek criet kit arrived and having now got that damn LED to blink and played with the timing AND, made the servo turn with a pot, I'd like to give you some well meaning feedback.

Now it may be because I'm about as ignorant as they come right now. but I find most of your posts like this way over my head.  Specifically after looking at things online and on the arduino site, I can see all the code here which I wouldn't have a hope in hell of doing myself but there is nothing about what components or how to hook it up.

I don't mean to sound like these people I get on my YT channel that look at the simplest of things and then ask for " Plans" ( which ships me to tears when all the "plans" they need is to look at the damn thing) but for twits like me, not having the parts list and the wiring diagram pretty much kills any chance of building the thing and making yuse of the time and effort you have put into writing and working out these things.

Now I think the likelihood is that I'm too ignorant right now and someone more advanced should know how to hook it all up and what they need and I get that.  From MY POV though, If I knew what I needed parts wise and what went where, I have half a chance.  Perhaps it's just that these things need more experience and if so I get that but looking over some of what you have put up, I'm sure even a pelican Newbie like me could have a chance of getting these things working with the back end filled in.

Now this may bore you to tears, you may not have time or desire for it or other factors I am ignorant to. You seem to go out of your way so much to share with others though and give people so much help, I thought I'd give you the dummies POV though in case there is something else in it for you.

In any case, thanks for putting up all you do. I'm sure in time I'll get a better understanding of it and be able to figure things out to let me build so many of the things you have shown.


  • Hero Member
  • *****
  • Posts: 1088
  • Country: us
« Reply #3 on: December 13, 2016, 01:32:28 AM »
These actually take a long time to create and edit right. In the many years I've been here I only know of one person that ever tried to build one thing posted.  This initial program is a demonstrator of the basic process  I am just starting to do testing with the program and a picture and schematic hasn't been posted yet.  Already I am seeing initial assumptions are not correct.  I like junkbox projects. At camp I go out to the garage and whatever I have available, I use.   FETs, resistors, pots are all subject to change.  That makes for some really interesting designs sometimes.  I want to demystify electronics.
Pictures will often have pieces of circuit boards cut up with an angle grinder with a thin cutoff wheel.  These are then hot melt glued to a piece of wood. The world is a cornucopia of  little useful sub assemblies free for the taking.  I get some really nasty comments on some boards.  I've put many boards into production.  I used to write the UL files for products and send them to the inspectors.  They would change a couple words just to keep some self respect.  I just got things through quicker and frankly many of them were morons. I suspect many of them were student work study.  And people feel safe when they see UL.  I digress.  The point is I've been around and know how to do it right. Right takes a lot of effort.  Getting things close is empirically just as good often.  I really have little interest in turnkey systems unless I am getting paid.  This is bar talk and I will keep working with someone as long as they are trying to learn something.  Ideas should be free.  There is no one solution and I want people to be able to adapt these ideas to their situation.  I like my camp and knowing that I created a very efficient system for next to nothing. That solved a need.  If the beer was cold and the milk didn't stink that quick hack was just fine with me. Frankly, I don't care much about electronics anymore. I really need to be managed to get anything productive out of me these days.

Every five years I look back and think, I knew nothing then.  So I know how daunting this can be starting out.  Just break down everything into small parts.  So if you have questions, ask.  I can be motivated enough to put down a few sentences. 


  • Hero Member
  • *****
  • Posts: 1088
  • Country: us
« Reply #4 on: December 13, 2016, 12:49:32 PM »
                        FIRST TESTING

This is the first time I've been intimate with a lithium battery in a Cole Porter
sense. Testing was with a pair of 18650 batteries welded in parallel. I have had
these several years and not used them.  A number of months ago I had charged them
when I got those little TP4056 charger boards. They are neat little boards and I
suggest you get 10 of them for $3.50 if you have a number of Li batteries around.
These boards can be left connected to the battery when not being powered, so build
them into your device.  You should also put a resistor in series with the + power
supply lead. Spec sheet recommends .5 ohm. I've used them with 2-5 ohms to lower
maximum charge current when charging unknown batteries.  This puts the heat in the
resistor and not in the chip. I use an old cell phone charger for the 5V.  Most of
these telephone wall warts can supply .6 to 1.2A. For thise under 1A, having the
1 ohm resistor is a must. Anything between 4V and the supply voltage is just turned
into heat. Better heating a resistor than the chip.

With the battery as found, the voltage measured 4.08 at the beginning of the test.
3.700V was the programmed in stop voltage and the test ended after 9 Amp Minute.
Stop was changed to 3.600V and the test ran another 23AM. Voltage recovered and a
short timelater the test was resumed at the same stop. That added another 5AM. The
next day I continued the test and added another 6AM for a total of 43AM.  43 amp
minutes for a free battery isn't bad.  That can run a flashlight a long time before

I wanted to sufficiently discharge it before a charging operation. This was only a
quick test as I changed some numbers in the program. My charge rate was .75A and
mostly limited by the wall wart rated at 700mA. The charger board remained cool to
the touch. The charge limit is 4.2 volts. The TP4056 terminates the charge when the
total charge current drops to 100mA. The TP4056 is set up for a single cell, two
cells works fine

This time I ran the test down to 3.5V. Some manufacturers test down to 2.5V, but
below 3.4 there can be damage to battery chemistry and 90% of the power is used
up at that point.  You can't run many devices through that range and it wouldn't
be useful to me. 33 amp minute is just over .5AH, not much for two parallel cells.
One standard test is at 610mA. With two cells in parallel that is only 500mA each.
I still have a jar of cells somewhere that need to be tested. Not a great cell. 
Still good enough for a LED light. Just shows why these batteries need to be tested
before commiting them to a project. It will be an education.

As I said before this is a demonstrator project for how data logging can be performed
fairly easily. I'm sure you can find one trady made on ebay for less than $10. I just
bought this one for $4. Will report after testing it.

There is no documentation.  Looks like you can read the amps, amp hours, and voltage. 
Not sure if you can adjust the load except externally with a resistor or whether it
will stop on its own.

This is a picture of the test set up with the TP4056 charger.  Only one FET is active.
The pot is to fine tune the voltage read and the large black capacitor is the filter for
the amp minute analog output.  And old VOM with a meter movement will often have a scale
on the meter face that will match up with amp hours or minutes.  I certainly hope everyone
feels they can make a better project than this. Missing resistor value is 62K.


  • Hero Member
  • *****
  • Posts: 1088
  • Country: us
« Reply #5 on: December 25, 2016, 10:48:19 AM »
I've done a little more work with the battery tester. Another battery pair tests at about
30 amp minutes.  With just a few minutes rest, it can just about do that a second time without
recharging.  This same thing happened with the prior battery pair.  Testing was done at only
a half amp load.  Higher currents greatly shorten the total amp minutes.  While these batteries
are junk for any reasonable load, they are still quite good for low current LED lighting.

I changed the menu selection of currents.  1-6 now represent 100ma to 600ma which is more
applicable for these end of life batteries.  I use a cheap cell phone charger for the 5V to
supply power to the micro and the charger board.  The regulation is rather poor and voltage
readings vary significantly when charging  That can change the stop point when in stand alone
operation.  To solve this I changed the voltage divider resistors and used the internal 1.1V
reference instead of the 5V power. A single line in the program changes this.

 analogReference (INTERNAL);    // use with (62K, 5K pot), 15K voltage divider, for .7V input

I usually avoid this for a couple reasons. 1V is really down in the soup.  Any noise that will
be picked up will be significant. Circuits like a charger that have currents of an amp or more.
That can induce small voltages in the common ground circuit.  These could greatly affect the
measured voltage if care isn't taken in the wiring. Finally, most sensors operate on 5V making
the voltage range incomparable without adding circuitry.

analogReference can be dangerous if EXTERNAL mode is used.  It would seem convenient to just
tie the AREF to some other source like 3.3V or other power source that may be used elsewhere
in the circuit. Should you forget to include this EXTERNAL statement in the code, both power
supplies will be connected.  This short will damage the micro and/or one of the power sources.
In fact, if this was hard wired on a circuit board the micro would have to be programed before
inserting it into the board.

There is a work around for this.  Connect a 5K or higher resistor from the external reference
to the AREF pin.  The AREF pin is not a high impedance input.  There is an internal 32K resistor
to common.  The input resistor will will then form a voltage divider making the reference voltage
lower. An advantage to this is the reference voltage can be changed on the fly without damage.
However, the A/D readings will not be stable for a couple readings. Never use a reference higher
than 5V and DO NOT USE THE THE ON BOARD 3V3 as a reference unless that has at least a 2K load
resistor on it.  With insufficient load on the 3V3 power line, the 5K resistor can supply enough
current to raise the 3V3 line to to 5V.


  • Hero Member
  • *****
  • Posts: 1088
  • Country: us
« Reply #6 on: January 09, 2017, 05:49:19 PM »
          ZB2L3 Battery Tester

This is a nice little battery tester I received today.  Just wanted to show you I'm not
married to the UNO. Every thing you want to build can be bought cheaper in CHINA.  Features
are limited to 15V maximum and 3A. 3A is really pushing it for that little FET, limit it to
half that current. Current is selected by external resistor, it comes with two 7.5 ohm 5W
resistors.  One gives you about a half amp with a lithium cell, with two in parallel being
about 1A. On a 12V SLA battery the two resistors in series (15 ohm) would be .83A and dissipate
a hair over 10W. I'd add a little extra resistance to it to be safe. Board is about $4 shipped
and of course there were no instructions.  End voltage can be set from, .5V to 12V.  The
unit was tested with a 121 ohm 1% resistor and it correctly indicated a current of 32ma.

The board is powered by a mini USB connector.  If a 5V wall wart is not available with that
connector, it can be wired directly. On the back of the board just under the USB connector
there is a square solder pad which is the +5V.  The IN- pad is the negative.  Suggest you make
a solder connection here rather than use the screw terminal.  A loose wire could damage the
board.  Load resistor goes to the two R pins, test battery to the + IN - terminals.  You will
want to have an easy way to disconnect power to the board.  Install a switch in the +5V line
if a pull out connector is not used.

This board does not have reverse battery protection.  Connect battery before you program or
error message will be given when button is pressed.

1) When first powered the battery voltage will be given on the display. It will stay in VOLT
mode till + or - is pressed to change the end voltage.  It will then display P 3.0 the default end voltage setting. I do not like to bring Li below 3.4V because it can alter the chemistry and shorten life.

2) Press + till the display indicates P 3.4 for the end voltage.  It can only be adjusted in
.1V steps.

3) Press OK.  This starts the current flowing and the display will keep sequencing through AH,
A, and V.

4) When the battery has reached the end voltage the display will rapidly flash the ending amp
hour. It will remain on AH till display power (USB) is removed or OK is pressed again starting
the test over. Note the last ending voltage will not be kept and the standard setting P 3.0
will be used.
« Last Edit: January 10, 2017, 10:48:14 AM by OperaHouse »

Bruce S

  • Global Moderator
  • Super Hero Member Plus
  • *****
  • Posts: 4581
  • Country: us
  • USA
« Reply #7 on: January 11, 2017, 10:47:15 AM »
How well did this do against your UNO tester?
A kind word often goes unsaid BUT never goes unheard


  • Hero Member
  • *****
  • Posts: 1088
  • Country: us
« Reply #8 on: January 12, 2017, 09:39:34 AM »
I think it is a nice all in one board limited to about 1 1/2 amp and current is selected by resistor. If it ever breaks it can never be fixed. It seems like a fully acceptable option and quite accurate at low currents. It can be used up to 15V, cut out at 12V.  Mine has adjustable current by PWM and pretty much has to be run off a laptop.  Calculated current in software from a fixed voltage.  It could be expanded to any voltage and any current.  It was just to demonstrate how an immediate need could be solved.  I have a later version of software that improves it a little, not something I am going to support.  It could be made to look just like the other one with additional parts.  I will likely turn this into a solar panel tester.