# Battery Component
[](https://components.espressif.com/components/espp/battery)
State of charge (SoC) estimation for a rechargeable battery pack, from its
voltage and, where the board measures it, its current. It needs no fuel gauge
IC: feed it each reading (from an ADC, an INA226 / INA219, ...) and it keeps
the estimate.
A pack's voltage only tells its charge at rest. Charging lifts it, by the
charge current through the pack's resistance plus a polarization that takes
minutes to relax, and a charger's constant-voltage stage holds it at the full
voltage. So a voltage-only estimate reads 95-100 % for a pack that is much
emptier as soon as it starts charging. `espp::Battery` instead combines:
- the rest voltage: the cell's rest (open-circuit) voltage curve at the
measured voltage, corrected by I x R. It learns the pack's resistance from
the voltage step of each current step (a charger switching on or off). At
rest (the charger off and no current), the estimate is this value.
- counting: while current flows, the charge in or out (current x time /
capacity) moves the estimate, which is pulled slowly toward the rest-voltage
estimate. The pull is weaker at high current, where polarization biases the
voltage. So a poor starting point heals, and the voltage still bounds the
estimate: from above while charging, from below while discharging;
- the end of a charge: 100 % once the charger reports it done
(`Reading::charge_complete`, e.g. from its status pin), or once the current
has tapered at the full voltage, but not while the pack discharges. It
holds 100 % while the pack relaxes, until it has discharged 1 % of its
capacity (a device running on it never rests, so counting takes over);
- voltage only: without current sensing, the rest curve, or an optional
charging curve while the charger is on.
The curves are `espp::piecewise_linear` points (cell volts -> SoC), so any cell
can be described. The chemistry presets supply typical curves:
- `espp::BatteryLipo` (`battery_lipo.hpp`): lithium polymer (pouch) cells,
4.2 V full.
- `espp::BatteryLion` (`battery_lion.hpp`): lithium-ion (cylindrical NMC:
18650, 21700, camera packs such as the NP-F series), 4.2 V full.
## Example
The [example](./example) simulates a 2S, 2000 mAh Li-ion pack on a CC / CV
charger. It compares the voltage-only SoC with the estimate, while charging
and after the charge.
## Python and PC
`espp.Battery`, `espp.BatteryLipo` and `espp.BatteryLion` are in the
cross-platform library ([lib](../../lib)) and its Python package, for
desktop tools or for fitting a curve to logged readings. Durations are
`datetime.timedelta`s, curves lists of `(volts, soc)` tuples, and
`update(reading, now)` takes a simulated steady-clock time (a `timedelta`) as
well as `update(reading)` for now:
```python
from datetime import timedelta
import espp
pack = espp.BatteryLipo(espp.BatteryLipo.Config(num_cells=2, capacity_mah=1500))
state = pack.update(espp.Battery.Reading(voltage_v=7.9, charge_current_ma=-250),
timedelta(seconds=1))
print(f"{state.soc_percent:.0f} %")
```
## Tests
[pc/tests/battery.cpp](../../pc/tests/battery.cpp) tests the estimator on the
host against the installed library, on a simulated clock, and runs (under ASan
and UBSan) in the `Battery host test` workflow whenever this component, `math`,
`base_component` or `logger` changes.
[python/battery_test.py](../../python/battery_test.py) tests the Python binding
in every wheel build.
0bb7b005ab1fc333a4ebad1a2ab86ae1d8b9cf95
idf.py add-dependency "espp/battery^1.5.0"