Project

Create an Arduino Controlled Battery Charger

February 17, 2016 by Jason Poel Smith

In this project, we use an Arduino and an attached charging circuit to control the charging of NiMH rechargeable batteries.

An Arduino and attached charging circuit can be used to monitor and control the charging of NiMH rechargeable batteries, here's how:

The finished device

 

Rechargeable batteries are a great way to power your portable electronics. They can save you a lot of money and when properly recycled, they are much better for the environment. In order to get the most out of your rechargeable batteries, they need to be properly charged. That means that you need a good charger. You could spend a lot of money on a commercial charger, but it's much more fun to build one for yourself. So here is how to build an Arduino controlled battery charger.

First it is important to point out that there is no universal charging method that is appropriate for all rechargeable batteries. Each type of battery uses a different chemical process to make it work. As a result, each type of battery needs to be charged differently. We can’t cover all the battery types and charging methods in this article. So for simplicity, we are going to focus on the most common type of AA rechargeable battery, Nickel-Metal Hydride (NiMH). 

 

A Fritzing diagram of the project

 

Schematic for the project

Materials:

Parts in order from left to right

 

  • Arduino Microcontroller
  • AA Battery Holder
  • NiMH AA Battery
  • 10 ohm Power Resistor (rated for at least 5 watts)
  • 1 Mohm resistor
  • 1 µF Capacitor
  • IRF510 MOSFET
  • TMP36 Temperature Sensor
  • 5V Regulated Power Supply
  • Prototyping Breadboard
  • Jumper Wires

How to Charge NiMH AA Batteries

Increasing the C-rate will charge the battery faster, but will increase the risk of damaging it

 

There are a lot of different ways that you can charge a NiMH battery. The method that you use depends mostly on how fast you want to charge your battery. The Charge rate (or C-rate) is measured relative to the capacity of the battery. If your battery has a capacity of 2500mAh and you charge it with a current of 2500 mA, then you are charging it at a rate of 1C. If you charge it with a current of 250 mA, then you are charging it at a rate of C/10.

When charging your battery quickly (at a rate higher C/10), you need to carefully monitor the battery’s voltage and temperature to make sure that you don’t overcharge it. This can seriously damage your battery. However, when you charge your battery slowly (at a rate of C/10 or less), it is much less likely to damage our battery if you accidentally overcharge it. Because of this, slow charging methods are generally considered to be safer and will help maximize battery life. So for my DIY battery charger, I decided to use a charge rate of C/10.

The Charging Circuit

The circuit design for this charger is a basic Arduino controlled power supply. The circuit is powered by a 5-volt regulated voltage source such as an AC adapter or an ATX computer power supply. Most USB ports would not appropriate for this project because of the current limitations. The 5V source charges the battery through a 10 ohm power resistor and a power MOSFET. The MOSFET sets how much current is allowed to flow into the battery. The resistor is included as an easy way to monitor the current. This is done by connecting each terminal to analog input pins on the Arduino and measuring the voltage on each side. The MOSFET is controlled by a PWM output pin on the Arduino. The pulses of the pulse width modulation signal are smoothed out into a steady voltage signal by a 1M resistor and a 1 µF capacitor. This circuit allows the Arduino to monitor and control the current flowing into the battery.

 

The Temperature Sensor

The temperature sensor prevents the battery from overcharging and causing a safety hazard

 

As an extra precaution, I included a TMP36 temperature sensor to monitor the temperature of a battery. This sensor outputs a signal voltage that directly corresponds to the temperature. So it doesn’t require calibration or balancing like a thermistor does. The sensor is mounted in place by drilling a hole in the back of the battery housing and gluing the sensor in so that it sits against the side of the battery when installed. The pins of the sensor are then connected 5V, GND, and an analog input pin on the Arduino. 

 

The AA battery holder before and after being placed on the breadboard

 

The Code

 

The code for this project is reasonably straight forward. There are variables at the top of the code that allow you to customize your charger by inputting the values of the battery capacity rating, and the exact resistance of your power resistor. There are also variables for the safety thresholds of the charger. The maximum allowable voltage of the battery is set to 1.6 volts. The maximum temperature of the battery is set to 35 degrees Celsius. The maximum charge time is set to 13 hours. If any of these safety thresholds is exceeded, the charger is turned off.

In the body of the code, you will observe that system constantly measures the voltages of the terminal of the power resistor. This is used to calculate both the terminal voltage of the battery and the current flowing into the battery. This current is compared to the target current which is set to C/10. If the calculated current is different from the target current by more than 10 mA, the system automatically adjusts the output to correct it.

The Arduino uses the serial monitor tool to display all the current data. If you wish to monitor the performance of your charger, you can connect the Arduino to the USB port on your computer, but this is not necessary as the Arduino is powered by the 5V power supply of the charger. 

 

You can find a downloadable version of the full code below:

Arduino_Controlled_Battery_Charger_Code.zip

Now that you have the knowledge, you can get started on a charger of your own. Be sure to monitor your charge rate and use safety protocols, as excessively charging a battery can be dangerous. 

 

Give this project a try for yourself! Get the BOM.

42 Comments
  • T
    thegladiator February 22, 2016
  • P
    Phil-S February 26, 2016

    Now, if you could just do one for 12-V lead acids?
    I have a CTEK “smart” charger tha goes through about eight stages from de-sulfurisation to maintenance charging.
    I’ve often thought that that would make a nice project.
    Anyhow, nicely done and probably a lot better than many of the cheapo ones.

    Like. Reply
  • S
    Sylvaneupher February 26, 2016

    What changes would have to be made to use this basic circuit for charging 2 AA NiMH batteries and could one use an Arduino Mini for the controller?

    Like. Reply
    • agroom March 02, 2016
      Any MCU would be capable of controlling it, assuming it has PWM, 1 digital and 2 analog pins. For charging 2 batteries, 2 ideas come to mind. First, you could place both batteries in parallel, then add the mAh to get the total capacity, though this would probably not be ideal. For individual charging, I'd assume you'd have to create separate circuits for each battery or multiplex.
      Like. Reply
  • A
    AlgoryThm March 25, 2016

    Hello! Congratulations for the innovative and simple at the same time way of thinking and for sharing this with us. I saw the voltage is 5V at the input. I’d like to charge a bank of 12 AA alkaline batteries all connected in series. Currently I’m using an LM317 custom charger, but I’d like to use yours as it is more sophisticated and also it features an auto cut off function. Mine doesn’t. So what changes should I make to your circuit for a 20V/20mA charging voltage and current? Also I’d like to skip the temperature sensor as for 10-20mA charging current I think it won’t be necessary. Thank you very much again, very nice work!

    Like. Reply
  • Michael Sims August 28, 2016

    Can pretty much any voltage regulation mosfet be used?

    Like. Reply
    • A
      agent006 November 19, 2016
      No, you are only working with a 5 volt supply. So you need a mosfet that is fully on at 5 v, or you will not get the correct current you are aiming for.
      Like. Reply
  • D
    deefstes September 28, 2016

    This is a beautiful project in it’s simplicity. I think I’m gonna give it a go. I’d like to try to extend it to be able to independently charge 4 AA batteries but I just want to make sure I understand everything correctly.

    In your schematic, you’re not indicating where the battery to be charged is wired in. By looking at the wiring of your breadboard, I seem to get the impression that it’s wired between the power resistor and the 5V source. Is this correct? So to duplicate the circuit for 4 batteries I would need 8 analog pins on the Arduino. No can do, obviously. I imagine you could wire the battery rather between the power resistor and the drain of the MOSFET in stead, or am I missing something? In that case, the analog pin above the power resistor could be common to all 4 subcircuits, right? In fact, couldn’t you then leave out that analog pin altogether as you know that it would be at 5V (1023) anyway?

    So here is the circuit that I have in mind:
    http://imgur.com/pGwDjhN

    Each channel can be controlled through digital (PWM) pins 6, 9, 10 & 11 while the current for each channel can be calculated by comparing analog pins 0 to 4 against 5V.

    Would appreciate any comments on this approach. Is there anything obvious that I’m missing?

    Like. Reply
    • A
      agent006 November 19, 2016
      What about the temperature sensors? Or are you going to use a mega 2560
      Like. Reply
  • H
    hjkgfyydi5757 October 14, 2016

    The Material list says “1 Mohm resistor”
    I believe you meant to write “10 Mohm resistor”

    Like. Reply
    • A
      Abufari November 07, 2016
      No, it's meant to be a 1 MOhm resistor. First, look at the image, there's depicted a 1 MOhm. Secondly, more important, the RC-time would be 100 instead of 10 seconds, which would be way too high and wouldn't fit to the 10 seconds delay in the code.
      Like. Reply
  • S
    smatiz October 27, 2016

    In your arduino code:  where read the volt of battery? Where read the temp? Whats ports use for this purpose?

    Like. Reply
  • Bé Petitclerc November 30, 2016

    Hi, thanks for the original idea. I’m currently doing this project for a school assignment combining programmation and physics (circuits). I’d like to add an on/off button (Mini Illuminated Momentary Pushbutton: https://abra-electronics.com/electromechanical/switches/pushbutton-switches/3105-ada-mini-illuminated-momentary-pushbutton-blue-power-symbol.html) which would activate the charging (kind of like a switch). Would it be possible while keeping the micro controller connected to the power source? Also, what would be the additional coding that would be required for this type of function? Finally, I’d also like to add a buzzer that would ring when the battery is fully charged. Again, would this be possible and if so, what would a prototype code look like?
    Thank you in advance.

    Like. Reply
  • A
    adas97adafiti February 15, 2017

    Can anyone give me the full coded? I have download it but the cannot open the file.

    Like. Reply
  • B
    battusreekanth March 01, 2017

    in above pic of circuit in bread broad i have not find how the connection made to mosfet so plz help me to how to give
    the connect to mosfet

    Like. Reply
  • M
    markandy March 16, 2017

    nice project

    Like. Reply
  • K
    kolio April 26, 2017
  • K
    kolio April 26, 2017

    On image “The Charging Circuit”: 10MOhm
    but in list “Materials”: 1 MOhm

    where is truth? 😊

    Like. Reply
  • boroi 1016 May 01, 2017

    if i want to make it fast charger for lithium ion, what i need to modified on this circuit?

    Like. Reply
  • Hr Martin May 30, 2017

    What is up with the circuit diagram? Where is the battery supposed to go?

    Like. Reply
    • K
      Kate Smith June 16, 2017
      Hi Martin, Please have a look at the images we've just added to see if they help. Thanks very much!
      Like. Reply
  • B
    BrandonWhitt May 31, 2017

    can you put the entire circuit diagram ?

    Like. Reply
    • K
      Kate Smith June 16, 2017
      Hi Brandon, Please check out the beginning of the article to see what we've added. Thank you!
      Like. Reply
  • B
    BarryR June 13, 2017

    A proper schematic would have made this much easier.

    Like. Reply
    • K
      Kate Smith June 16, 2017
      Hi Barry, We've added a couple of images to hopefully help out! Thanks for reading.
      Like. Reply
  • geo 2k12 July 01, 2017

    hi ive got the following in mind!

    4x AAA NIMH = ~ 4000mah @ 1,25v = 5V

    can i use my arduino nano (serial, pwm, spi, i2c,...anolog + digital…) already connected to 1” color display + temp sensor to charge its own batteries (4x1,25=5V) using a cheap 5v USB phone charger delivering 1,5 A (1,5x5= 7,5 WATTS) into all batteries in series without a MOSFET?

    theroy:  5v, 4000mah   <==>  5v 1500mah charge , should last about 3 hours at all inlucding voltage cutoff w/o temp monitor w/o mosfet ???

    Like. Reply
    • geo 2k12 July 01, 2017
      sry for double post.. so i found out.. when youve got 4x aa= 5 v theres merely no need for that MOSFET curcuit.. i just connect one PWM - to recharge pack - to GND thy 2x a0 a1 10ohm thingy for measuring the actual voltage, and connect the whole board (NANO) to my phne chrager 1,5 A ..done.. my only conecrn now is (im kinda stupid at late hours) that 4xaa(a) is 5v but not 4000 or 5000 ma.. so i'll gonna go for 8 times AA thats 2x4 in series plus 1x2(x4) in prallel and im in on about 4000 mah again.. and that would fit my need of charging the whole package in about 3h's + volatege control + safety shutdown after the amount of time that the batteries would need to reach 100% again, say if im on 25% it'll automatically shutdown after 2-2,5 hs.. done!
      Like. Reply
    • geo 2k12 July 01, 2017
      sry for double post.. so i found out.. when youve got 4x aa= 5 v theres merely no need for that MOSFET curcuit.. i just connect one PWM - to recharge pack - to GND thy 2x a0 a1 10ohm thingy for measuring the actual voltage, and connect the whole board (NANO) to my phne chrager 1,5 A ..done.. my only conecrn now is (im kinda stupid at late hours) that 4xaa(a) is 5v but not 4000 or 5000 ma.. so i'll gonna go for 8 times AA thats 2x4 in series plus 1x2(x4) in prallel and im in on about 4000 mah again.. and that would fit my need of charging the whole package in about 3h's + volatege control + safety shutdown after the amount of time that the batteries would need to reach 100% again, say if im on 25% it'll automatically shutdown after 2-2,5 hs.. done!
      Like. Reply
  • Antonio Gutierrez Lopez October 09, 2017

    Hi, i would like to use this circuit on order to create a smart solar charger, what modifications must be made if we use a 18v solar paner input to charge for example a 9v nimh battery? it just modifing the code or we need to add some components in the pcb? the aim is to keep the battery permanently in use (discharge) and permanently power supplied (charge) by the solar panel avoid overloading the battery and as a consecuence dannaging it thanks!

    Like. Reply
  • אלון טורם November 12, 2017

    Hi,
    While trying to build the project I encounter few issues and wonder if you can help.
    Things are connected as you designed and looks OK before plugging in the batteries, but when the battery is in place, the analog pins gets “confused” and shows unrealistic data.
    For example, with no battery the TMP36 sensor shows (in the serial screen) the room temperature (~25 c), but when placing the battery in it shows ~50 c or -50 while the battery is in same room temperature.
    Can you help finding the problem?

    Like. Reply
  • ThatAintWorking February 15, 2018

    Someone mentioned in a previous comment that you could charge 2 batteries in parallel by adding the mAh to get the capacity, what if you had multiple batteries in series? I would like to add a charging circuit like this to my robot project (which has 6 5000mAh C-cells connected in series) so I can charge the batteries without having to remove them. I would be using a 12v 1A wall wart for charging.

    Like. Reply
    • RK37 February 17, 2018
      The basic principle is that connecting batteries in series increases the overall battery voltage, whereas connecting batteries in parallel increases the overall battery capacity. If you want specific guidance on a battery-charging circuit, I recommend that you post your question in our forum.
      Like. Reply
  • Alexander Baughman March 23, 2018

    Hi, for this code, is it implementing constant current constant voltage?

    Like. Reply
  • Cristi Someșan April 17, 2018

    Hey, I have a problem and I cannot find any solution to it. I made this project, I checked that every component is working but my the battery charger is not working well. The battery current is always 0 (voltage probe 1 equals voltage probe 2). The voltage reading of the battery is working perfect. Can you help me with a solution? Thanks!

    Like. Reply
    • Cristi Someșan April 17, 2018
      the voltage is sometimes going to raise from 1.4V to 1.6V and then to 2V and then to 2.4 and then drop right back to 1.4V ,where it should normally be,when the battery is not charging.
      Like. Reply
      • Cristi Someșan April 17, 2018
        so i measured with the multimeater, there is current going into the battery only when the voltage is rising
        Like. Reply
    • RK37 April 18, 2018
      If you are having problems with your implementation of the circuit, I recommend that you create a post in the forum. If you provide enough information, our forum members should be able to help you find a solution.
      Like. Reply
  • K
    kirshant July 19, 2018

    sir
      i want a program that control the servo motor with the help of ultra sonic sensor with the arduino software

    Like. Reply
  • Igor G May 16, 2019

    Is it alright to put 5V into VIN-Pin?Shouldn’t it be at least 7V?

    Like. Reply
  • Abhishek Banerji June 11, 2019

    how long will it take to charge a single 1000 mah (1.2 v)  aaa battery using this method?

    Like. Reply
  • su dabakare July 29, 2019

    how Arduino Uno calculate initial charge or voltage preset in battery if battery having some charge or start the charging swich on the second time

    Like. Reply
  • Pedro Núñez October 12, 2019

    Thanks for sharing this information with us.

    Like. Reply