DISCLAIMER: This code was made with the help of AI/LLMs.
A USB inline charge limiter for lithium-ion battery devices. Set the current when you start charging, and the device will cut power when the current has dropped to half.
ChargeCap exploits the Li-ion charge curve: charge current stays high during bulk charge, then tapers linearly as voltage approaches the cell ceiling (~4.2V/cell). By sampling peak current at the start of a charge cycle and cutting off at a set fraction of that peak (default: 50%), the device stops charging at roughly 80–90% — regardless of pack size or cell count.
- Long-press button: samples current peak and sets the cutoff threshold
- Short-press button: toggles output on/off (manual override)
- OLED display: shows live current, cutoff threshold, charge elapsed time, and status
- Auto-cutoff: MOSFET cuts USB power when current drops to threshold; display inverts to signal completion
- MCU: ESP32-C3
- Current sensing: Shunt resistor (0.027 Ω) + ADC (GPIO 0)
- Output control: GPIO 10 driving a MOSFET/relay
- Display: SSD1306 128×32 OLED (I²C: SDA GPIO 7, SCL GPIO 6)
- Button: GPIO 8
- Web dashboard (Highcharts) showing live current + full charge history
- Tiered ring-buffer data storage: 100 recent points (100ms), 60 medium (1 min), 100 long (adaptive)
- EMA smoothing on ADC data. Built in esp32-c3 ADC is not sensitive enough to be precise, but precision is not needed here. Smoothing eliminates large noise spikes.
- Create a
Secrets.hfile alongsideChargeCap.ino:#define MYSSID "your_wifi_ssid" #define MYPSK "your_wifi_password"
- Install libraries:
Adafruit GFX,Adafruit SSD1306,ArduinoOTA,ESPmDNS - Flash to ESP32-C3 via Arduino IDE
- Tune
SHUNT_RESISTANCE,ADC_REFERENCE_VOLTAGE, andCURRENT_OFFSETconstants for your hardware