Become a subscriber (Free)

Join 29,000 other subscribers to receive subscriber sale discounts and other free resources.
Don't worry -- youre-mail address is totally secure. I promise to use it only to send you MicroZine.

Arduino Battery Charger Project

An Arduino Battery Charger: How to save the planet one battery at a time! Did you know that all those single use batteries (alkaline ones) that have a label on them saying "Do Not Recharge" are in fact chargeable!

Of course some will not be and some will charge a little bit better than others. But even getting on re-use out of a throw-away battery seems like a better way to go.

The criteria for whether or not the battery is re-chargeable or not is first of all give it a visual inspection. If it has powdery residue on it then it is dead so re-cycle it in your local battery re-cycling place.

The next thing to determine re-chargeability is whether or not the battery voltage has fallen below 0.8V. If it is too low then you are unlikely to get anywhere. You could measure this with a volt meter or just use the project below that does this automatically for you (I set the level to 0.75 since the voltage measurement is not absolutely accurate (Voltage regs usually have a ±10% regulation so you don't get exactly 5V out - also the reference voltage I used is using two divider resistors so that's another 10%). It is not absolutely critical for charging alkaline batteries.

Note: This alkaline battery charger project is not suitable for re-charging NimH, NiCd or Li based batteries. Lithium batteries (Li) require a voltage regulation of at least 1% and an absolute exact cut off voltage, otherwise there is a risk of explosion. Just use the project as it stands for alkaline batteries (standard single-use batteries of nominal voltage 1.5V).
Warning: Some batteries will leak, so inspect them first (preferably wearing gloves). if you see white powder on them dispose of them at a battery re-cycling center.

Arduino Battery Charger Schematic

arduionodry cell battery charger circuit diagram

Note: If you don't have a serial LCD just use the parallel connections shown here and adjust the code accordingly. You will only need to adjust the initialiser (for pins) as the libraries are based on identical code and will therefore operate using the same instructions e.g. for lcd.print etc.
Note: You can use any general purpose low volt PNP transistor for this circuit.

Arduino Battery Charger Circuit operation

First of all when CHRG and HCHRG are floating then R1 pulls the transistor base high so no current flows = OFF.

Pulling CHRG low results in a constant current of 21.3mA and then pulling HCRG low results in an increased charging current of 33mA.

Note: These are nominal values and will vary with the tolerance of the resistors used and the ambient temperature. For these dry cells the exact values are not important.

The basic operation is to start charging the battery at low current for a period, and then charge at a high current for another period. If the battery voltage increases, during high charge, then carry on charging, if not, then charging is finished (as long as the battery is within the acceptable voltage limits).

Arduino Battery Charger charging current

When CHRG is pulled low R1, R2 and R3 are enabled forming a resistive divider that sets the base voltage at

3.28V : 5*((1e3+560)/(1e3+560+820))

So the voltage across the base of the transistor, i.e. across R1, is:

1.722 : 5 - 3.26

The base emitter diode drop is 0.7V so that leaves 1V dropped across R4. This therefore allows you to define the charging current i.e. for the 47R resistor shown the charging current will be

21.3mA : 1.0/47

Arduino Battery Charger high current Charging

When HCHRG is pulled low (regardless of CHRG) then resistor R3 is shorted out to ground. So the voltage now dropped across (only) R2 is:

2.74V : 5*((1e3)/(1e3+820))

This the voltage across the base of the transistor, i.e. across R1, is:

2.25V : 5 - 2.74

Now the voltage drop across R1 is:

1.55 : 2.25 - 0.7

Which results in a charging current of:

33mA : 1.55/47

AREF External voltage

The voltage divider formed by R8 and R9 halves the voltage used for the ADC reference so the maximum that the ADC can read is 2.5V which is plenty since we only really need to measure up to about 1.6V. Having a lower AREF voltage also means that each bit of the ADC has a lower value i.e. a finer resolution - instead of 4.88mV it is 2.44mV per bit so the ADC can see smaller voltage changes at the battery.


You can use only the LCD display or only the LEDs. You will get a green (pass) or red (fail) light at the end of charging. The LCD displays more information such as the current voltage level at the battery, the start voltage of the battery and the elapsed time. It also shows the state variable (really for debug) that lets you know where the code is at. In addition the serial port outputs text messages showing you what happened. So the LCD is not really essential for operation but is useful if you make a stand alone unit.

Serial Monitor

You can use the serial monitor (in the Arduino IDE) to observe operation as serial data is also reported showing state machine operation, adc values and voltages.

Software Library and versions

Arduino IDE Version

Version : 1.8.3


Note: You only need the LiquidCyrstal SPI library if you are using a serial SPI LCD.

The liquidCrystal library is replaced with one that allows SPI operation (so you can still use non SPI code with this library).

Download the (SPI) LiquidCrystal library from:

Then goto your library location


Rename directory LiquidCrystal to LiquidCrystal_orig.

Then unzip the downloaded file here which should result in a new folder


Note: This library specifies the pinout of the LCD which must match the schematic:

You can find the schematic here (Wiring diagram):

There 's no facility to change pins (you could change the code though).

Code Operation State Machine

The Arduino Battery Charger code is formed from a simple state machine. These are the main states:


You can more or less read these from left to right and see what the code is doing.


First of all in the IDLE state the system is set up to charge.


Then in the BATFND state the voltage is checked (during low charge) to see if a valid battery is present.

If out of range then the state is set to FAIL.

If in range then the state is set to CHARGE.


In the CHARGE state low charge current continues for 30 seconds. At the end the ADC reading is stored in the variable lowChrgADC.

After 30 seconds the state is set to HIGH_CHARGE and the high current output activated.

The state changes to HIGH_CHARGE after 30s.

Two other tests are made (all the time).

Battery reaches max voltage

1. The state is set to FINISHED.

Battery is out of range

2. The state is set to FAIL.


After 30 seconds the state is set to TEST

The state changes to TEST after 30s.

Two other tests are made (all the time).

Battery reaches max voltage

1. The state is set to FINISHED.

Battery is out of range

2. The state is set to FAIL.


Battery Voltage Increase for High Charge

This is where the decision is made either to terminate the process or continue. First of all, if the current ADC value (at the end of high charge compared to the ADC value at the end of the low current charge period) is not high enough (<9.77mV) then the battery is considered to be charged and the state is set to FINISHED.

The state of the battery (out of range) is not tested here since it has been continuously tested in states CHARGE and HIGH_CHARGE.

Detecting Final Charge : test_avg()

Some batteries hover around a final value but change their voltage output enough during High charge so that the algorithm can't stop. To detect this state (over several charge and high-charge cycles) requires storing ADC values so a rolling shift register containing the last 10 low charging ADC values is used for detection.

In the function test_avg() the array prevADC[] is tested to see if the values are all within 1 value of the average of those 10 readings. If they are, then the state machine goes to the FIINISHED state i.e. this shows there has been no 'real' change in battery voltage over the last few charging cycles.

After each test phase the array is shifted and the latest low charge ADC value is inserted.

If neither of the above cases is true then the state machine goes back to IDLE.

The next state is :

FINISHED (if battery voltage is high enough) or

FINISHED for no average battery change or

IDLE (if the others are not true).


The controls and LEDs are set: (green LED is lit to indicate success) and charging is turned off.

The next state is WAIT_START.


The controls and LEDs are set: (red LED is lit to indicate failure) and charging is turned off.

The next state is WAIT_START.


Here is the only state that allows restart detecting a button press on the input pin. When pressed, various variables are initialised for a complete restart.

When pressed the state is set to IDLE.

Arduino Battery Charger Sketch

// AA and AAA charger
// Could use for others with different charge currents
// i.e. different emitter resistors.
// LiquidCrystal library used here is not the original
// download an enhanced version from
// allows SPI operation.
// V1.00
// Copyright John Main
// Free for non-commercial use.
#include <SPI.h>
#include <LiquidCrystal.h> // Enhanced for SPI operation. 

#define START_PIN 12
#define RLED_PIN A4
#define GLED_PIN A5

#define VBATT_PIN A0
#define CHRG_PIN 2
#define HIGH_PIN 3
#define SPI_SS_PIN 10

#define CHARGE_ON  pinMode(CHRG_PIN,OUTPUT);digitalWrite(CHRG_PIN,LOW);
#define HIGH_ON    pinMode(HIGH_PIN,OUTPUT);digitalWrite(HIGH_PIN,LOW);
#define HIGH_OFF   pinMode(HIGH_PIN,INPUT);
#define RLED_ON    digitalWrite(RLED_PIN,HIGH);
#define RLED_OFF   digitalWrite(RLED_PIN,LOW);
#define GLED_ON    digitalWrite(GLED_PIN,HIGH);
#define GLED_OFF   digitalWrite(GLED_PIN,LOW);
#define SPC        Serial.print(' ');

#define VBATMAX 1.60 // Maximum and minimum acceptable battery.
#define VBATMIN 0.75 // voltage to allow charging to start.
#define VREFANA 2.36 // Reference voltage (AREF). Measure this with DMM.
#define CHRG_ms 30000 // Time period of normal charging time (ms).
#define HIGH_ms 30000 // Time period of normal higher power charging time (ms).

#define PREV_ADC 10  // Number of nearly same ADC readings to declare done.

               FINISHED, FAIL, WAIT_START } state_t;

// Initialize for SPI with sspin, also known as RCLK or LATCH.
// For the lcd other pins are the standard SPI pins.
LiquidCrystal lcd(SPI_SS_PIN);

void setup() {

   // set up the LCD's number of columns and rows: 
   lcd.begin(16, 2);
   Serial.println("Battery Charger ");

// Print start volts to LCD & serial(for reference).
void showStartVolts(void) {
  float v = getVana( analogRead(VBATT_PIN) ) ;
  lcd.setCursor(11, 0);    
  lcd.print( v,3 );
  Serial.print("\nStart volts: ");
  Serial.println( v,3 );

void reason(uint8_t c) {
   Serial.print("End reason: ");
   switch(c) {
      case BATTFULL       : Serial.println("Max volts."); break;
      case BATTAVGSTABLE  : Serial.println("No avg change."); break;
      case BATTBAD        : Serial.println("Out of range.");
      case BATTNORISE     : Serial.println("No Hchrg rise.");
void loop() {
  static uint32_t loop_time = 0;
  if (millis()-loop_time>250) {  // No need execute too fast
     loop_time = millis(); 

float getVana(uint16_t adc) { 
    return adc * VREFANA / 1024;

uint8_t checkBattBad(float Vana) { 
   if ( Vana<VBATMIN || Vana>VBATMAX ) return 1;
   return 0;

uint8_t checkBattFinished(float Vana) { 
   if ( Vana>=VBATMAX-0.1 ) return 1;
   return 0;

void showSerialElapsedTime(uint32_t r_time) { 
   Serial.println(" mins.");   

void showState(state_t state) {
   // This state has no text, to leave FINISHED or FAIL on LCD.
   if (state == WAIT_START) return; 
   // Show current state 
   lcd.setCursor(0, 0);
   lcd.print("           "); // 11 chars clear debug area.
   lcd.setCursor(0, 0); 
   switch( state) {
     case IDLE : lcd.print("IDLE");break;
     case BATFND : lcd.print("BATFND");break;
     case CHARGE : lcd.print("CHRG");break;
     case HIGH_CHARGE : lcd.print("H-CHRG");break;
     case TEST : lcd.print("TEST");break;
     case FINISHED : lcd.print  ("FINSHD");break;
     case FAIL : lcd.print("FAIL");break;
//     case WAIT_START : lcd.print("WAIT");break;
    default: break;

void showTime(uint32_t timeVal_ms) { // could use sprinf - this = less memory.
  uint8_t lcdpos;
  uint16_t tmin,tsec;
  uint32_t t;
    t = timeVal_ms;
    tmin = (t/1000/60); // Could use sprintf but smaller code using:

    if (tmin >= 1) { // Print minutes since reset
       lcdpos = 12;
       if (tmin>99) lcdpos = 10;
          else if (tmin>9) lcdpos = 11;       
       lcd.setCursor(lcdpos, 1); 
    // Print seconds since reset.
    tsec = (t/1000) % 60;
    lcd.setCursor(14, 1);
    if (tsec<10) 

// -1 resets the store
uint8_t test_avg(uint16_t adc) {
uint8_t i,match,allmatch;
static uint16_t prevADC[PREV_ADC];
uint16_t avg;

   if (adc==-1) {
      for(i=0;i<PREV_ADC;i++) prevADC[i]=0;
   // Get average value 
      avg = 0;
      for (i=0;i<PREV_ADC;i++) {
         Serial.print(prevADC[i]); SPC;
      avg += 5;  // =0.5 after division by 10
      avg /=10;
      Serial.print("Avg ");Serial.println(avg);
      // If any are zero then not filled: set avg zero to stop err. match
      for (i=0;i<PREV_ADC;i++) {
          if (prevADC[i]==0) { 
             avg = 0;
             Serial.println("Avg zet ZERO");
      // Check if has not increased over last n readings - if so exit.      
      allmatch = 1;   
      for (i=0;i<PREV_ADC;i++) {
         match = 0;
         // Detect close matches: lowChrgADC+/-2 (+/-4.88mV)
         if ( avg-1 <= prevADC[i] && prevADC[i] <= avg+1 ) match=1;
         // Here if the value is one of 3: lowChrgADC+/-1, then match is high
         if (!match) { allmatch = 0; break; }
      // Update rolling store. // Array indices 0 ~ (n-1) shift.
      for (i=0;i<PREV_ADC-1;i++) prevADC[i]=prevADC[i+1]; 
      prevADC[PREV_ADC-1] = adc;
      // allmatch is 1 if all have matched for all elements.
      if (allmatch && avg!=0) { // Zero is a special case.
         return 1; // All the same so indicate finished.
      return 0;

void action(void) {
   static uint32_t s_time = millis();
   static uint32_t r_time = s_time; // Relative, start times.
   static uint32_t timewas = 0, timewas_state=r_time;
   static state_t state = IDLE; 
   static uint16_t lowChrgADC=0;
   static uint8_t timer_on=1;
   uint16_t adc=0;
   float Vana;
   static uint8_t done_once = 0;

   if (!done_once) {
      test_avg(-1); // Reset store.
   r_time = millis() - s_time; // Use time relative to start
   if (timer_on) showTime(r_time); // Time since start: to LCD.

   adc = analogRead(VBATT_PIN);
   lcd.setCursor(0, 1);
   switch( state) {
   case IDLE :  
       state = BATFND;

       CHARGE_ON   // Start charge to detect battery.

   case BATFND : // Battery ok?
       if ( checkBattBad(Vana) ) {
          state = FAIL; // Battery is dead or not present.
          Serial.println("FAIL in BATFND"); 
       } else if (Vana>=VBATMIN && Vana<=VBATMAX) {
           state = CHARGE;
   case CHARGE : 
      if ( checkBattBad(Vana) )      { reason(BATTBAD) ; state = FAIL; }   
      if ( checkBattFinished(Vana) ) { reason(BATTFULL); state = FINISHED; }
      else if (r_time - timewas_state>CHRG_ms) {   
         lowChrgADC = adc; // Store value just before high charge time.          
         state = HIGH_CHARGE;   
         timewas_state = r_time; 
   case HIGH_CHARGE : 
      if ( checkBattBad(Vana) )      { reason(BATTBAD) ; state = FAIL; }   
      if ( checkBattFinished(Vana) ) { reason(BATTFULL); state = FINISHED; }    
      else if ( r_time - timewas_state>HIGH_ms) {
         state = TEST;
         timewas_state = r_time; 

   case TEST : 
      Serial.println("Cur adc,lowChrgADC");
      Serial.print(adc);       SPC; Serial.println(getVana(adc),3); 
      if ( adc > (lowChrgADC+4) ) {  // Gone up by > (2.5/1024)*4=9.77mV    
         state= IDLE; 
      } else { // No rise so batery charge maxed out.   
         state = FINISHED;
      if ( test_avg(lowChrgADC) ) state = FINISHED;
   case FAIL : // Error condition so stop charging battery.
      timer_on = 0;
      state = WAIT_START;
   case FINISHED : 
      timer_on = 0;
      state = WAIT_START;

   case WAIT_START :
      if ( digitalRead(START_PIN)==0) {
         state = IDLE;
         s_time = millis();
         r_time  = millis()-s_time;
         timewas_state = r_time;
         timer_on = 1;
         test_avg(-1); // Reset store.
   // Show raw adc value
   lcd.setCursor(7, 0);    
   lcd.print(adc);lcd.print(' ');

New! Comments

Have your say about what you just read! Leave me a comment in the box below.

Privacy Policy | Contact | About Me

Site Map | Terms of Use

Visit our Facebook Page:

   Click Here

Recent Articles

  1. PIC PWM: Easy Pulse width Modulation on the PIC Micro.

    PIC PWM : What its for and how to use it. Including an example showing typical setup of a PIC PWM module with register settings.

    Read more

  2. The Essential I2C Tutorial: All you need to know about I2C...

    How to use I2C. In this tutorial you will learn all about the 2 wire I2C serial protocol. Learn how easy it is to use, how it works and when to use it...

    Read more

  3. PIC LCD volt meter : How to use the ADC for voltage measurement.

    A 0-5V LCD volt meter project using an HD44870 display and one PIC micro ADC.

    Read more

  4. PIC Programming using ICSP

    PIC programming : How to use ICSP for programming a PIC microcontroller giving some ICSP connection diagrams and it also shows how to use MPLAB X and ICPROG.

    Read more

  5. PIC ICSP In Circuit Serial Programming

    How to program PIC ICSP, IN-Circuit, live and still have a working programmer and operational circuit!

    Read more

  6. An Ultrasonic Distance meter project: How to make one using a microcontroller.

    Understand how an Ultrasonic Distance meter works by building one your self – including Free software to drive the microcontroller and display.

    Read more

Sign up for MicroZine
''The'' Microcontroller Newsletter

Enter your first Name and primary email address in the form below:

And receive absolutely FREE a full project for:

"Measuring Analogue Voltages
Without An ADC"

(Using only one pin).

Instant Download:
You Can
Get It Right Now

Warning: This project could be  Removed 
at any time.  

It will  NOT be 
available indefinitely SO
To avoid 
disappointment  get it:


Don't worry -- your e-mail address is totally secure. I promise to use it only to send you MicroZine
Remember this is a project with full description and fully debugged C Source code - and it's not available from the main website.

You can only get it through this newsletter.

To get exclusive access Enter your first name Name and primary email address Now in the form above.:

But wait !

There's more...

You'll receive more
free and exclusive reports as well as site information and site product updates

Scroll up to the form above and sign up NOW. Don't forget it's FREE and if you don't like it, you can unsubscribe at any time.

Click Here Now to use the form above to get your Valuable information absolutely free.

Readers Comments

"I wanted to thank
you so so so much
for all the information
you have provided in
your site it's


- Ranish Pottath

"This site really is
the best and my favorite.
I find here many useful
projects and tips."

- Milan


"Awesome site,
very, very easy and nice
to navigate!"

- Matt

Learn Microcontrollers

"Interested in

Sign up for The
Free 7 day guide:


"I am a newbie to PIC
and I wanted to say
 how great your
site has been for me."

- Dave


"Your site is a great
and perfect work.

- Suresh


"I couldn't find the correct
words to define
yourweb site.

Very useful, uncovered,
honest and clear.

Thanks so much for
your time and works.

- Anon

Back to Top