ESP32: supercapacitor backup for graceful power-off
Add a supercapacitor to your ESP32 project so it can detect a power loss and shut down gracefully. The minimum circuit for industrial-grade deployments.
A supercapacitor gives your ESP32 project the ability to detect a power loss and shut down gracefully. The cap holds enough charge for the ESP32 to detect the loss, save its state, and enter deep sleep or shut down cleanly.
This is the minimum hardware for projects that need to survive power cuts without corrupting state, missing data, or losing configuration.
What you need
- ESP32 dev board
- 1F to 10F supercapacitor (5.5V rated; about $2-5)
- Schottky diode (1N5817 or similar)
- 10k ohm resistor (for the voltage divider)
- Optional: 100uF electrolytic capacitor
How it works
Power supply -- Schottky diode -- ESP32 3.3V
|
+-- Supercapacitor
|
GND
When power is applied, the supercapacitor charges through the diode. When power is removed, the diode prevents the capacitor from discharging back into the power supply. The capacitor discharges into the ESP32, which detects the voltage drop and has a few hundred milliseconds to shut down gracefully.
The math
A 1F supercapacitor at 5V holds:
E = 0.5 * C * V^2 = 0.5 * 1F * 5^2 = 12.5 J
The ESP32 in active mode consumes about 0.1W (30 mA at 3.3V). The cap can power the ESP32 for:
t = E / P = 12.5 J / 0.1 W = 125 seconds
But you only need a few hundred milliseconds for graceful shutdown. A 1F cap gives you 100x more than needed. A 100mF cap (about $1) is enough.
For longer hold-up times (e.g. 30 seconds for a proper shutdown sequence), use a 1F to 10F cap.
Wiring
5V power supply + -- diode anode
diode cathode -- ESP32 5V pin (or 3.3V via regulator)
ESP32 5V pin -- Supercapacitor +
Supercapacitor - -- ESP32 GND
ESP32 3.3V -- 10k resistor -- Supercapacitor + (voltage divider tap)
ESP32 GND -- Supercapacitor -
The voltage divider (10k to 3.3V, 10k to ground) lets you monitor the capacitor voltage through the ADC.
The supercapacitor voltage can exceed 5V if the supply is 5V. Use a 5.5V or higher rated supercapacitor. Common ratings: 5.5V (most common), 2.7V (lower voltage, smaller size).
The detection code
const int V_MONITOR_PIN = 34;
float readCapVoltage() {
int raw = analogRead(V_MONITOR_PIN);
return raw * 3.3 / 4095.0 * 2.0; // voltage divider halves the voltage
}
float lastVoltage;
void setup() {
Serial.begin(115200);
delay(1000);
lastVoltage = readCapVoltage();
Serial.print("Cap voltage: ");
Serial.println(lastVoltage);
}
void loop() {
float v = readCapVoltage();
// Power lost: voltage dropped more than 0.5V
if (v < lastVoltage - 0.5) {
Serial.println("POWER LOST! Saving state...");
saveState();
Serial.println("State saved, sleeping");
esp_deep_sleep_start();
}
lastVoltage = v;
delay(100);
}
void saveState() {
// Save important state to EEPROM or RTC memory
// This runs within the few hundred milliseconds the supercap gives you
}
The threshold (0.5V drop) is the key. A small drop is normal as the capacitor charges/discharges slightly. A drop of more than 0.5V in a single loop iteration means power was lost.
The deep sleep wake on power
After detecting power loss and saving state, the ESP32 should enter deep sleep. When power returns, the ESP32 wakes from deep sleep and resumes.
For projects that should be off until power returns, use a GPIO interrupt to wake:
esp_sleep_enable_ext0_wakeup(POWER_DETECT_PIN, 1); // wake when power returns
// Before sleep:
esp_deep_sleep_start();
The “RTC memory” pattern
RTC memory is the ESP32’s special memory that survives deep sleep. Use it to save the most critical state during graceful shutdown:
RTC_DATA_ATTR int lastBootCount = 0;
RTC_DATA_ATTR float lastSensorReading = 0.0;
void setup() {
lastBootCount++;
Serial.print("Boot count: ");
Serial.println(lastBootCount);
}
RTC memory survives deep sleep without needing the supercapacitor.
The supercapacitor only needs to last long enough for saveState()
to run, which writes to RTC memory directly.
The 1N5817 diode
The diode prevents the supercapacitor from discharging back into the power supply. Without it, the cap’s charge flows back through the power supply when it loses power.
The 1N5817 (1A Schottky) is the standard pick. Its low forward drop (0.3V) means less voltage loss during normal operation.
For high-current projects (above 1A), use a larger diode like the MBR20100 (20A Schottky).
What you learned
- A supercapacitor gives the ESP32 time to detect power loss and shut down gracefully.
- A 1F cap provides 100x more hold-up time than needed for most projects.
- The Schottky diode prevents reverse discharge.
- RTC memory preserves state through the shutdown.
When something breaks
- ESP32 does not detect power loss. Voltage divider wrong, or the threshold is too high.
- ESP32 does not get enough hold-up time. Capacitor too small, or the shutdown code is too slow.
- Capacitor voltage drops too fast. Load is too heavy (something other than the ESP32 is drawing current).
- Capacitor voltage does not recover. Power supply is weak (cannot source enough current to charge the cap).
What to build next
- The 18650 + TP4056 tutorial covers longer-duration battery backup.
- The deep sleep tutorial covers the code patterns for low-power ESP32.
- The book ESP32 Industrial IoT covers supercapacitor sizing for various hold-up time requirements.