Skip to content

Battery Management System

Battery Management System

This section explains the battery management system (BMS) integrated into the ESP32-XIAO, focusing on the charging circuitry, power path management, and battery protection features. By the end of this section, you will be able to:

  • Understand how the TP4054 charging IC works
  • Describe the power path management between USB and battery
  • Explain charging states and indicator behaviors
  • Troubleshoot common battery charging issues

Before starting this section, please ensure:

  • Familiarity with LiPo battery characteristics (nominal voltage, charge voltage)
  • Completed 04-01. ESP32-XIAO Hardware Overview
  • Basic understanding of current and voltage measurements

The ESP32-XIAO integrates a complete battery management system on a single board, avoiding the need for external modules. The system consists of three functional blocks:

┌──────────────┐
USB Power ──────────┤ │
│ TP4054 ├───── VCC (3.3V regulator)
│ Charging IC │
LiPo Battery ───────┤ │
└──────┬───────┘
┌──────▼───────┐
│ Battery │
│ Voltage │──► ADC Pin (battery level monitoring)
│ Divider │
└──────────────┘

The TP4054 is a standalone linear Li-ion battery charger designed for space-constrained applications.

Key Specifications:

ParameterValue
Charging methodLinear (constant current / constant voltage)
Charge currentProgrammable up to 500 mA (typically 380 mA default on XIAO)
Charge voltage4.2V ± 1% (standard LiPo termination voltage)
Input voltage4.5V - 6.5V (USB 5V compatible)
Standby current< 2 µA when charging complete
Protection featuresThermal regulation, reverse current blocking
PackageSOT-23-5 (ultra-small surface mount)

Charging Profile:

The TP4054 follows the standard Li-ion CC/CV charging curve:

┌─────────────────────────────────┐
4.2V │ ★ │ CV Phase (voltage constant,
│ ★ │ current tapers down)
│ ★ │
│ ★ │
Voltage│ ★ │
│ ★ │
│ ★ CC Phase │
│ ★ (current │
│ ★ constant) │
│ ★ │
│ ★ │
└─────────────────────────────────┘
Charge Current ────────────────►
(Constant) (Tapering)

Three Charging Phases:

  1. Trickle Charge: If battery voltage < 2.9V, charge at 10% of programmed current
  2. Constant Current (CC): Charge at full current (~380 mA) until 4.2V reached
  3. Constant Voltage (CV): Hold at 4.2V while current gradually drops to termination threshold (~10% of CC)

The ESP32-XIAO implements a simple but effective power path scheme:

ConditionPower SourceBattery State
USB connected, battery presentUSB (5V) → 3.3V regulatorBattery charging
USB connected, no batteryUSB (5V) → 3.3V regulatorN/A
USB disconnected, battery presentBattery → 3.3V regulatorDischarging
USB connected + battery fullUSB (5V) → 3.3V regulatorCharging terminated

Important behavior: When USB is connected, the board runs from USB power, and the battery charges independently. This means:

  • The ESP32 sketch continues running normally while charging
  • There is no reset or interruption when USB power is applied or removed
  • The system seamlessly switches between power sources

The XIAO provides a charging status LED (red):

LED StateMeaning
Solid redBattery is charging
OffBattery fully charged, or no battery connected
BlinkingFault condition (battery over-temperature, timeout)

The XIAO includes a resistor voltage divider connected to an ADC pin, allowing the ESP32 to read the current battery voltage:

Battery (+) ──┬── 100kΩ ──┬── ADC Pin (GPIO)
│ │
│ 100kΩ
│ │
Battery (-) ──┴───────────┴── GND

The voltage divider halves the battery voltage, bringing it within the ESP32’s 3.3V ADC range. The conversion formula in code:

float readBatteryVoltage() {
int adcValue = analogRead(BATTERY_PIN);
float voltage = (adcValue / 4095.0) * 3.3 * 2; // ×2 for divider
return voltage;
}

The XIAO has a 2-pin JST connector on the backside:

Pin 1 (+): Battery positive (red wire)
Pin 2 (-): Battery negative (black wire)

Warning: Ensure correct polarity. Reversing the connection can damage the charging IC.

void setup() {
Serial.begin(115200);
pinMode(BATTERY_PIN, INPUT);
}
void loop() {
float batteryVoltage = readBatteryVoltage();
Serial.print("Battery Voltage: ");
Serial.print(batteryVoltage);
Serial.println(" V");
if (batteryVoltage > 4.0) {
Serial.println("Battery is fully charged");
} else if (batteryVoltage > 3.5) {
Serial.println("Battery is charging or in normal range");
} else {
Serial.println("Battery voltage low, consider charging");
}
delay(5000);
}

To verify the charging current, measure the voltage across a known resistor in the charging path, or use a USB power meter. A properly charging XIAO should draw approximately 380-500 mA from USB.

  • Battery charges when USB is connected (red LED on)
  • LED turns off when charge is complete
  • ESP32 continues running when USB is connected/disconnected
  • Battery voltage reading is within expected range (3.7V-4.2V)
  • System switches to battery power when USB is disconnected

Symptom: Red LED stays off when USB is connected with battery present

Possible Causes:

  • Battery already fully charged (check voltage with multimeter)
  • Battery connected with wrong polarity
  • Battery protection circuit triggered (some batteries have built-in protection)

Solution:

  1. Measure battery voltage with multimeter
  2. If voltage > 4.1V, battery is full — normal behavior
  3. If voltage is 0V, check polarity and connections
  4. Some protected batteries need a brief “kick” — connect to USB briefly with battery disconnected, then reconnect battery

Symptom: ADC always reads 0

Possible Causes:

  • ADC pin not configured correctly
  • Battery not connected or connection is loose
  • Voltage divider resistor damaged

Solution:

  1. Verify the ADC pin number matches your board variant
  2. Check JST connector is fully seated
  3. Measure voltage directly at the battery connector pads
  • Use protected LiPo batteries with built-in over-discharge protection
  • Monitor battery voltage in firmware to prevent deep discharge (< 3.0V)
  • Include a low battery warning in MQTT messages
  • Charge at room temperature (0°C to 45°C recommended)
  • Do not exceed the maximum input voltage (6.5V)
  • Do not charge damaged or swollen batteries
  • Do not leave batteries connected to a dead/disconnected XIAO for extended periods — the battery may discharge through the protection circuit
  1. XIAO uses TP4054 — a compact, reliable Li-ion charger with CC/CV profile
  2. Built-in power path management allows simultaneous charging and operation
  3. Charging current is ~380 mA default, sufficient for most LiPo batteries
  4. ADC voltage divider enables battery level monitoring in firmware
  5. No external components needed — the XIAO is a complete BMS solution

Target Audience: Alibaba.com IoT Pre-sales Engineers
Status: ✅ Completed