Arduino TP4056 Li-ion Battery Charger: Wiring, Code and Simulator

To build this Arduino TP4056 Li-ion Battery Charger project you need an Arduino Uno and a TP4056 li-ion charger module. A TP4056 charges a 3.7 V cell from 5 V: the Uno reads the cell voltage on A0 while the red CHRG and blue STDBY LEDs show the stage. Make the 4 connections in the wiring table, upload the Arduino C++ code, and open the Modules tab to see the charge curve: constant current, then constant voltage at 4.2 V. Add a load and take away the input to see it discharge.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1TP4056 Li-ion charger moduleCharges the 3.7 V cell on B+/B− from 5 V on IN+/IN− (USB), up to 1 A, and powers your circuit from OUT+/OUT−. Red CHRG while charging, blue STDBY when full. Watch the curve in the Modules tab; speed time up to see a full charge.

Wiring

#FromTo
1Arduino Uno 5VTP4056 Li-ion charger module IN+
2Arduino Uno GNDTP4056 Li-ion charger module IN-
3Arduino Uno A0TP4056 Li-ion charger module OUT+
4Arduino Uno GNDTP4056 Li-ion charger module OUT-

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add a TP4056 li-ion charger module from the parts palette.
  3. Wire 1. Connect Arduino Uno 5V to TP4056 Li-ion charger module IN+.
  4. Wire 2. Connect Arduino Uno GND to TP4056 Li-ion charger module IN-.
  5. Wire 3. Connect Arduino Uno A0 to TP4056 Li-ion charger module OUT+.
  6. Wire 4. Connect Arduino Uno GND to TP4056 Li-ion charger module OUT-.
  7. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  8. Run it. Press Run. Open the Modules tab to see the charge curve: constant current, then constant voltage at 4.2 V. Add a load and take away the input to see it discharge.

Arduino C++ code

// TP4056 Li-ion charger: watch the cell charge from the Uno's analog pin.
// IN+ / IN- from 5V and GND (the "USB" supply), OUT+ -> A0, OUT- -> GND.
// The red CHRG LED is lit while charging and the blue STDBY LED when it is full.

const float VREF = 5.0;

// Rough state of charge from the resting voltage of a Li-ion cell.
int percentOf(float v) {
  if (v >= 4.15) return 100;
  if (v <= 3.3) return 0;
  return (int)((v - 3.3) / (4.15 - 3.3) * 100);
}

void setup() {
  Serial.begin(9600);
  Serial.println("TP4056: cell voltage every second");
}

void loop() {
  float v = analogRead(A0) * VREF / 1023.0;
  Serial.print("Cell ");
  Serial.print(v, 2);
  Serial.print(" V  (~");
  Serial.print(percentOf(v));
  Serial.println(" %)");
  delay(1000);
}

What to try

Open the Modules tab to see the charge curve: constant current, then constant voltage at 4.2 V. Add a load and take away the input to see it discharge.

Frequently asked questions

How do I wire the TP4056 Li-ion Battery Charger project on an Arduino Uno?

Make 4 connections: Arduino Uno 5V to TP4056 Li-ion charger module IN+; Arduino Uno GND to TP4056 Li-ion charger module IN-; Arduino Uno A0 to TP4056 Li-ion charger module OUT+; Arduino Uno GND to TP4056 Li-ion charger module OUT-.

What parts do I need for this Arduino project?

An Arduino Uno and a TP4056 li-ion charger module.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

A TP4056 charges a 3.7 V cell from 5 V: the Uno reads the cell voltage on A0 while the red CHRG and blue STDBY LEDs show the stage.

How does a TP4056 charge a lithium battery?

Below 2.9 V it trickle-charges at a tenth of the set current, then charges at constant current (1200 / R_PROG amps; 1.2 kΩ gives 1 A) until the cell reaches 4.2 V, then holds 4.2 V while the current tapers, and stops at a tenth of the set current.

What do the red and blue LEDs on a TP4056 mean?

Red (CHRG) is lit while charging; blue (STDBY) when the cell is full. With no cell connected, blue stays on and red flickers.

More Arduino Uno projects