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



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
  • 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:


  Download Code  

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.




  • thegladiator 2016-02-22

    Can a recovery Rectifier like this ( be used in place of the MOSFET?

  • Phil-S 2016-02-26

    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.

  • Sylvaneupher 2016-02-26

    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?

    • agroom 2016-03-02

      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.

  • AlgoryThm 2016-03-25

    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!

  • Michael Sims 2016-08-28

    Can pretty much any voltage regulation mosfet be used?

    • agent006 2016-11-19

      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.

  • deefstes 2016-09-28

    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:

    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?

    • agent006 2016-11-19

      What about the temperature sensors?
      Or are you going to use a mega 2560

  • hjkgfyydi5757 2016-10-14

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

    • Abufari 2016-11-07

      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.

  • smatiz 2016-10-27

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

  • Bé Petitclerc 2016-11-30

    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: 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.

  • adas97adafiti 2017-02-15

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

  • battusreekanth 2017-03-01

    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

  • markandy 2017-03-16

    nice project

  • kolio 2017-04-26

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

    where is truth? smile

  • boroi 1016 2017-05-01

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

  • Hr Martin 2017-05-30

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

    • Kate Smith 2017-06-16

      Hi Martin,

      Please have a look at the images we’ve just added to see if they help. Thanks very much!

  • BrandonWhitt 2017-05-31

    can you put the entire circuit diagram ?

    • Kate Smith 2017-06-16

      Hi Brandon,

      Please check out the beginning of the article to see what we’ve added. Thank you!

  • BarryR 2017-06-13

    A proper schematic would have made this much easier.

    • Kate Smith 2017-06-16

      Hi Barry,

      We’ve added a couple of images to hopefully help out! Thanks for reading.

  • geo 2k12 2017-07-01

    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 ???

  • 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!

  • אלון טורם 2017-11-12

    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?

  • ThatAintWorking 2018-02-15

    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.

    • RK37 2018-02-17

      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.

  • Alexander Baughman 2018-03-23

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

  • Cristi Someșan 2018-04-17

    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!

    • Cristi Someșan 2018-04-17

      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.

      • Cristi Someșan 2018-04-17

        so i measured with the multimeater, there is current going into the battery only when the voltage is rising

    • RK37 2018-04-18

      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.

  • kirshant 2018-07-19

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

  • Igor G 2019-05-16

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

  • Abhishek Banerji 2019-06-11

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

  • su dabakare 2019-07-29

    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