What is the TP4056?
The TP4056 module is the standard way to add rechargeable power to a project. Plug in a 5 V USB cable, connect a single 3.7 V lithium-ion cell (an 18650 or a LiPo pouch), and it charges the cell safely to 4.2 V at up to 1 A, then stops. The red LED shows charging, the blue or green LED shows full.
There are two versions and the difference matters. The basic board has only the TP4056 charger chip. The more common protected board adds a DW01A protection chip and an FS8205A dual MOSFET that disconnect the battery if it is over-discharged, over-charged or shorted. This page explains both, how the charge cycle works, how to change the charge current for small batteries, and how to power an Arduino or ESP32 from the cell.
Specifications
- Charger chip
- TP4056 linear Li-ion charger (NanJing Top Power)
- Input voltage
- 4.5–5.5 V (USB 5 V); absolute maximum 8 V
- Charge voltage
- 4.2 V ±1.5 %
- Charge current
- 1 A with the stock R3 = 1.2 kΩ; adjustable to about 130 mA
- Charge method
- Trickle below 2.9 V, then constant current, then constant voltage
- Charge termination
- When current falls to 1/10 of the set value (about 100 mA)
- Over-discharge cut-off
- about 2.4–2.5 V (DW01A, protected boards only)
- Over-charge cut-off
- about 4.3 V (DW01A)
- Over-current trip
- about 3 A load (DW01A + FS8205A)
- Input connector
- Micro-USB or USB-C, plus IN+/IN− pads
- Board size
- about 26 × 17 mm
Charger figures from the TP4056 datasheet; protection thresholds from the DW01A datasheet. Exact values vary slightly between module makers.
TP4056 module pinout
| Pad | Connect to | Notes |
|---|---|---|
| IN+ | 5 V input | Same as the USB connector; use one or the other |
| IN− | Input ground | Shared with B− and OUT− |
| B+ | Battery + | Directly to the cell's positive terminal |
| B− | Battery − | Through the protection MOSFETs on the protected board |
| OUT+ | Load + | Same node as B+; feed your boost converter or regulator |
| OUT− | Load − | Protected negative: switched off when the cell is empty or shorted |
Wiring & code
Setting up TP4056 with ESP32
Library
None needed by ESP32 Arduino core
No library required: the sketch uses analogReadMilliVolts() from the ESP32 Arduino core.
An ESP32 and an 18650 cell make a good portable or outdoor node, but the obvious wiring, TP4056 OUT+ straight to the board's VIN, does not work well. This guide shows why, the two wirings that do, and how to read the battery level on an ADC pin so the ESP32 can warn you before it dies. Read the TP4056 overview first if the pads are new to you.
Warning
Why OUT+ to VIN browns out. Most ESP32 DevKits regulate VIN with an AMS1117, which needs about 4.4 V in to give 3.3 V out. A lithium cell is 4.2 V when full and falls below 3.7 V soon after, so the ESP32 starts resetting, especially when Wi-Fi transmits, with most of the charge still in the cell.
Two ways to power an ESP32 from a TP4056
Boost to 5 V
- Wiring
- OUT+/OUT− → MT3608 set to 5.0 V → ESP32 VIN/5V
- Trade-off
- Easiest and works with any DevKit; the boost and AMS1117 waste about 25 %
LDO to 3.3 V
- Wiring
- OUT+/OUT− → HT7333 or ME6211 → ESP32 3V3
- Trade-off
- Best battery life; needs a low-dropout regulator that handles 500 mA Wi-Fi peaks
Wiring (boost option, with a battery monitor)
The ESP32's ADC pins only accept up to about 3.1 V, and the cell reaches 4.2 V, so the battery voltage goes through a 100 kΩ / 100 kΩ divider that halves it. With 200 kΩ in total, the divider draws only about 20 µA from the cell.
Wiring: TP4056, MT3608 and ESP32
| From | To | Note |
|---|---|---|
| 18650 + / − | TP4056 B+ / B− | Battery |
| TP4056 OUT+ / OUT− | MT3608 VIN+ / VIN− | Protected output into the boost |
| MT3608 VOUT+ | ESP32 VIN (5V) | Set to 5.0 V before connecting |
| MT3608 VOUT− | ESP32 GND | Common ground |
| TP4056 OUT+ | 100 kΩ → GPIO34 | Top of the divider |
| GPIO34 | 100 kΩ → GND | Bottom of the divider: GPIO34 sees half the cell voltage |
Tip
Why GPIO34? It is on ADC1, which keeps working while Wi-Fi is on. ADC2 pins (GPIO 0, 2, 4, 12–15, 25–27) stop reading when Wi-Fi is active.
Battery monitor sketch
analogReadMilliVolts() uses the ESP32's factory calibration, so it is far more accurate than converting analogRead() yourself. Doubling the result undoes the divider.
// ESP32 battery monitor: cell -> 100k -> GPIO34 -> 100k -> GND
const int BATTERY_PIN = 34;
const float DIVIDER = 2.0; // two equal resistors
const float VOLTS[] = {4.20, 4.05, 3.95, 3.85, 3.78, 3.72, 3.65, 3.50, 3.30};
const int PERCENT[] = { 100, 85, 70, 55, 40, 30, 20, 5, 0};
float readBatteryVolts() {
uint32_t mv = 0;
for (int i = 0; i < 16; i++) mv += analogReadMilliVolts(BATTERY_PIN);
return (mv / 16.0) * DIVIDER / 1000.0;
}
int toPercent(float v) {
if (v >= VOLTS[0]) return 100;
for (int i = 1; i < 9; i++) {
if (v >= VOLTS[i]) {
float f = (v - VOLTS[i]) / (VOLTS[i - 1] - VOLTS[i]);
return PERCENT[i] + f * (PERCENT[i - 1] - PERCENT[i]);
}
}
return 0;
}
void setup() {
Serial.begin(115200);
analogSetPinAttenuation(BATTERY_PIN, ADC_11db); // full 0-3.1 V range
}
void loop() {
float v = readBatteryVolts();
Serial.printf("Battery %.2f V ~%d %%\n", v, toPercent(v));
if (v < 3.40) Serial.println("Low battery: charge soon");
delay(10000);
}Compare the printed voltage with a multimeter on OUT+. If it is consistently off by a few percent, the resistors are not exactly equal: adjust DIVIDER (for example 2.04) until they agree.
Making it last
An ESP32 with Wi-Fi on draws 80–160 mA, so a 2000 mAh cell lasts about a day. Waking every few minutes, sending a reading and returning to deep sleep cuts the average to a few milliamps and the run time to weeks. The ESP32 deep sleep guide shows the pattern. With the LDO wiring and a bare module, deep sleep reaches microamps; a DevKit's USB chip and LED keep it higher.
Troubleshooting
Resets when Wi-Fi connects
- Likely cause
- Supply sags on transmit peaks
- Fix
- Boost wiring, or a 500 mA LDO; add 470 µF at 3V3
Battery reads 0 V
- Likely cause
- Divider on an ADC2 pin with Wi-Fi on
- Fix
- Use GPIO32–39 (ADC1)
Reading jumps around
- Likely cause
- Noise on the high-impedance divider
- Fix
- Average more samples, or add 100 nF from GPIO34 to GND
Dies at 3.6–3.7 V
- Likely cause
- OUT+ wired straight to VIN through the AMS1117
- Fix
- Use the boost or LDO wiring above








