If your project runs on a lithium cell (such as an 18650), you need to charge it properly. Lithium isn't like an ordinary AA cell, and charging it wrongly can shorten its life or be dangerous. The most popular and cheapest solution for projects is the TP4056 module: a small board with a USB input that charges a single cell by itself.
How lithium is charged: CC then CV
- Constant current (CC): the charger supplies a constant current, and the battery voltage rises.
- Constant voltage (CV): when the voltage reaches 4.2 V, the charger holds it, and the current starts falling by itself.
- End: when the current falls to about a tenth of the charge current, charging stops.
The TP4056 does all of this by itself. And if the battery is very flat (below about 2.9 V), it starts with a small trickle current until the voltage rises, then continues at full current.
The module
- Input: 5 V from USB (Micro or Type-C), or from IN+ and IN− pads.
- B+ and B−: the battery.
- LEDs: red = charging, and blue or green = done.
If the two LEDs blink, or blue and red alternate with no battery connected, that's normal: the charger sees an empty terminal. If it happens with a battery connected, the battery or the connection is probably faulty.
Two types: watch out
- Plain (about 4 terminals): a charger only, with no protection against over-discharge or short circuit.
- With protection (6 terminals: IN, B and OUT): it has two extra chips (usually a DW01 and an 8205 dual MOSFET) that disconnect if:
- The battery is over-discharged (about 2.4–2.5 V).
- There's a short or overcurrent (about 3 A on most modules).
- The voltage exceeds about 4.3 V (a second line of protection if the charger itself fails).
The load connects to OUT+ and OUT−, not to B. Choose the protected type if the battery has no protection built in. Protected cells and packs are covered in the 18650 packs and BMS article.
Setting the charge current
The default is usually 1 A, with RPROG = 1.2 kΩ. If the battery is small (say a 500 mAh LiPo), 1 A is too much for it. The general rule is that the charge current shouldn't exceed the capacity (1C), and less is better, around half the capacity.
| RPROG | Approx. charge current |
|---|---|
| 1.2 kΩ | 1000 mA |
| 2 kΩ | 580 mA |
| 3 kΩ | 400 mA |
| 5 kΩ | 250 mA |
| 10 kΩ | 130 mA |
Change the small resistor labelled R3 or RPROG on the module. It's an 0603 or 0805 SMD part, so if you're not used to that, see the SMD hand-soldering article. And the code printed on it (such as 122 = 1.2 kΩ) is explained in the SMD codes article.
How long does charging take?
Roughly: capacity ÷ charge current, plus about 20–30% for the constant-voltage stage where the current tapers. A 2500 mAh cell at 1 A → about 3 hours from empty. The battery charge time calculator works it out.
The module heats up and slows down by itself
The chip has thermal regulation: when it gets hot (about 120 °C inside), it reduces the current by itself. So if the module is in a closed enclosure or the input is above 5 V, you'll find charging slower than calculated. The heat comes from the difference between the input voltage and the battery voltage × the current: from 5 V into a 3.5 V battery at 1 A = 1.5 W on a small chip. In the end it's a linear regulator.
A problem: charging with the load connected
The module decides charging is finished when the current falls to a tenth of the set value. If your project is connected to the battery while it charges, the charger sees the project's current as part of the charge, so it may never stop, and the battery sits at 4.2 V for hours, which shortens its life. The project also steals part of the charge current.
The fix is called load sharing: when USB is connected, the project takes its power directly from it, and the charger charges the battery alone. When USB is removed, the project switches to the battery automatically:
- A Schottky diode from the 5 V to the load.
- A P-channel MOSFET between the battery and the load, with its gate connected to the 5 V, and a 100 kΩ resistor from gate to ground.
- When USB is connected, the gate goes HIGH, the MOSFET turns off, and the battery charges by itself.
If the project draws very little current while charging (such as a sleeping ESP32), the problem won't show, and you can skip this circuit. The P-channel MOSFET is covered in the MOSFET article.
The voltage after the charger
The cell delivers from about 4.2 V down to 3 V depending on its charge, and that's not 5 V:
- For an ESP32 (3.3 V): an LDO with a small dropout. See voltage regulators.
- For 5 V: a boost converter. See buck and boost and boost modules.
To know how much is left in the battery, measure its voltage with the microcontroller through a voltage divider: the method is in the measuring battery voltage article.
Powering it from a solar panel
The TP4056 accepts up to about 8 V as an absolute maximum, and 4.5–6 V is comfortable. A small "5 V" panel usually has an open-circuit voltage higher than its rating, so check the Voc. And the module isn't MPPT, so it takes whatever current the panel can give, and in weak sun it charges slowly. Details are in the solar panels article.
Mistakes that kill the module
- The battery reversed on B+ and B−: the most common cause of a dead module, and the battery can get very hot. Check twice before soldering, especially with battery holder leads.
- The load on B instead of OUT on the protected module: that bypasses the protection.
- More than one cell in series: the module is for a single cell only (4.2 V).
- A LiFePO4 battery: its full voltage is 3.65 V, and the TP4056 will charge it to 4.2 V and damage it. It needs a dedicated charger. The difference is in the battery types article.
Safety tips
- The module gets hot at 1 A, so leave air around it, or reduce the current.
- Don't charge a swollen or dented battery.
- One cell only, or several cells in parallel, at the same voltage before connecting them.
- Don't leave a battery charging unattended for long periods, especially in your first experiments.
Details on types and safety are in the battery types article, and calculating runtime in battery life.