Brown-out, resets and fault handling
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A battery device has to get itself out of trouble. Follow an ESP32 sensor node on a tired battery: Wi-Fi draws 240 mA, the supply sags from 3.15 V to 2.39 V, and the brown-out detector resets the chip before it misbehaves. Then setup() asks esp_reset_reason() why it restarted and reacts: it rests after a brown-out, and after a crash it counts panics in memory that survives a restart, backing off after 3. Builds on "How batteries work" and "Power supplies: steady rails for every chip" from the Hardware course, and on "Watchdog timers", which covers the other reset you will meet here.
A battery feeds the chip
Our ESP32 sensor node runs on a tired battery. Its ESP32-WROOM-32 module holds 3.3 V flash memory, so it needs 3.0 to 3.6 volts. Awake with the radio off it draws about 50 mA, and the meter reads 3.15 V.
module ESP32-WROOM-32: 3.0 V to 3.6 V
(its 3.3 V flash sets the 3.0 V floor)
chip the bare ESP32 alone: 2.3 V to 3.6 V
battery 3.35 V behind 4 ohms (worn cell, thin wires)
idle 50 mA -> 3.35 - 0.05 x 4 = 3.15 Vاقرأ الدرس خطوة بخطوة
A battery feeds the chip
Our ESP32 sensor node runs on a tired battery. Its ESP32-WROOM-32 module holds 3.3 V flash memory, so it needs 3.0 to 3.6 volts. Awake with the radio off it draws about 50 mA, and the meter reads 3.15 V.
module ESP32-WROOM-32: 3.0 V to 3.6 V (its 3.3 V flash sets the 3.0 V floor) chip the bare ESP32 alone: 2.3 V to 3.6 V battery 3.35 V behind 4 ohms (worn cell, thin wires) idle 50 mA -> 3.35 - 0.05 x 4 = 3.15 VWi-Fi pulls the supply down
Wi-Fi starts sending and the radio draws about 240 mA. A battery has inner resistance, so more current means less voltage: 240 mA through 4 ohms loses 0.96 V. The rail sags to 2.39 V.
transmit 240 mA (802.11b, +19.5 dBm, datasheet typ) drop 0.24 A x 4 ohms = 0.96 V rail 3.35 - 0.96 = 2.39 V dip 3.15 V -> 2.39 V (0.76 V)A brown-out: half power
A brown-out is a dip, not a blackout: the power is low but not gone. At 2.39 V the module is below its 3.0 V minimum, so it may misread flash or store wrong values. Half-working is worse than stopped.
brown-out supply too low, but not zero now 2.39 V < 3.0 V module minimum risk wrong instructions, corrupted values, half-written data better stop cleanly and start againThe detector resets the chip
A brown-out detector is a comparator built into the chip. It checks the supply against a set level, here 2.43 V. 2.39 is below 2.43, so it pulls the reset line and stops the CPU before it misbehaves.
BOD level 2.43 V +/- 0.05 (level 0 of 8: 2.43 to 2.8 V) compare 2.39 < 2.43 -> true console Brownout detector was triggered action reset the chipA reset starts over
A reset stops everything and boots the chip again, like a fresh start. SRAM variables begin from scratch, so our reading count n is gone. But the hardware notes why it reset, and setup() runs first.
reset CPU stops, pins return to their defaults SRAM n = 41 -> set up again from scratch boot bootloader, then setup() from line 1 kept the reset cause: ESP_RST_BROWNOUTAsk why it restarted
Line 2 calls esp_reset_reason(). It returns ESP_RST_BROWNOUT. Other answers include ESP_RST_POWERON for a fresh battery, ESP_RST_PANIC for a crash and watchdog resets. Good firmware checks this first.
line 2 auto r = esp_reset_reason(); r ESP_RST_BROWNOUT others ESP_RST_POWERON power switched on ESP_RST_PANIC a crash (exception) ESP_RST_TASK_WDT the task watchdog ESP_RST_DEEPSLEEP woke from deep sleepBrown-out? Rest, then retry
If we start Wi-Fi again at once, the same dip resets us again, forever. So line 3 calls rest(): deep sleep for 60 seconds. With almost no current drawn, the rail climbs back to about 3.35 V.
line 3 if (r == ESP_RST_BROWNOUT) rest(); rest() esp_deep_sleep(60 s): never returns asleep almost no current -> rail near 3.35 V wake reset cause ESP_RST_DEEPSLEEP, setup() againA bug: reading address 0
Now a software fault. startWiFi() reads cfg->ssid, but cfg is a null pointer, address 0. Nothing lives there, so the CPU raises an exception: LoadProhibited. The panic handler prints the registers, then restarts the chip.
line 7 startWiFi(); -> cfg->ssid cfg nullptr: address 0x00000000 console Guru Meditation Error: Core 1 panic'ed (LoadProhibited) EXCVADDR: 0x00000000 default print registers and backtrace, then restart cause ESP_RST_PANICCount crashes across resets
Line 4 adds 1 to crashes on each panic. crashes lives in .noinit memory, which startup does not clear, so a software restart keeps it. Any other reset sets it back to 0 on line 5.
line 4 if (r == ESP_RST_PANIC) crashes++; line 5 else crashes = 0; .noinit not cleared at startup, kept across a software restart panics crashes = 1, 2, 33 crashes? Back off
After 3 panics in a row, line 6 calls backOff(): deep sleep for an hour. A crash loop would run the radio again and again and empty the battery. Sleeping keeps the node alive until the fault clears or is fixed.
line 6 if (crashes >= 3) backOff(); backOff() esp_deep_sleep(3600 s) without crash, reboot, Wi-Fi, crash ... battery empty with 3 tries, then rest an hour and try again
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01Why does the supply voltage drop when the Wi-Fi radio starts sending?
Voltage lost = current × resistance. In our example 240 mA through 4 ohms loses 0.96 V, so the rail sags from 3.15 V to 2.39 V.
02What does a brown-out detector do?
It is a comparator inside the chip. On the ESP32 the level can be set from 2.43 V to 2.8 V; below it, the chip is reset cleanly instead of running half-powered.
03Why is a clean reset better than letting the chip keep running at a low voltage?
A brown-out is half power, not no power. A half-working CPU can do the wrong thing, such as corrupting stored data. Stopping and starting again is predictable.
04setup() finds esp_reset_reason() returns ESP_RST_BROWNOUT. What is the best next move for a battery node?
Starting Wi-Fi at once makes the same dip and the same reset, again and again. Deep sleep draws almost no current, so the rail climbs back toward 3.35 V before the next try.
05Why is the crash counter declared with __NOINIT_ATTR?
ESP-IDF says .noinit values are not set up at startup and keep their value after a software restart, which is what a panic restart is. After power-on they are random, so setup() sets crashes to 0 for every reset that is not a panic.
المسرد
- Brown-out
- A dip in the supply voltage that is too low for the chip to work properly but is not a full power cut.
- Brown-out detector (BOD)
- A comparator inside the chip that resets it when the supply falls below a set level.
- Internal resistance
- The resistance inside a battery. The more current you draw, the more voltage it loses.
- Reset
- Stopping the CPU and starting the firmware again from the beginning, as at power on.
- Reset cause
- A record the hardware keeps of why the last reset happened; on the ESP32, read it with esp_reset_reason().
- Panic
- What the ESP32 does after a fatal fault such as an illegal memory read: print the registers and a backtrace, then restart.
- Null pointer
- A pointer holding address 0, which points at nothing. Reading through it is a fault.
- .noinit memory
- RAM that startup does not clear, so a value placed there survives a software restart.
الملاحظات والمصادر
- The battery is modelled as 3.35 V behind 4 ohms, an assumed value for a worn cell plus thin wires; a real battery's voltage also recovers slowly. The 240 mA and 30-68 mA figures are the ESP32 datasheet's typical values at 3.3 V.
- The Wi-Fi radio and the brown-out detector are inside the ESP32. The figure draws them as separate blocks so you can see the current and the comparison.
- ESP-IDF offers eight brown-out levels from 2.43 V to 2.8 V, each about +/- 0.05 V, and calls them estimates. All of them sit below the 3.0 V minimum of the ESP32-WROOM-32 module (set by its 3.3 V flash), so the detector catches deep dips, not every out-of-range moment. The real fix is a supply that never sags: a fresh battery, short wires and a big capacitor near the chip.
- ESP-IDF only promises that .noinit keeps its value after a software restart, and a panic restart is one. After power-on its contents are random, which is why every reset other than a panic sets crashes back to 0.
- Simplified: on the ESP32 the brown-out detector first raises an interrupt, which prints "Brownout detector was triggered", and then the chip resets. ESP-IDF calls the result a "software or hardware" brownout reset.
- Line 5 also sets crashes to 0 after a watchdog reset (ESP_RST_INT_WDT, ESP_RST_TASK_WDT), so a loop of watchdog resets is never counted. To count those too, test for them on line 4 as well as ESP_RST_PANIC.
- Arduino-ESP32 runs setup() and loop() on core 1 of the dual-core ESP32 by default, so the panic message names Core 1.
- The card shows only setup(). The full sketch also declares crashes with __NOINIT_ATTR, the reading count n, the buggy cfg pointer, and rest(), backOff() and startWiFi(). Deep sleep never returns: waking up runs setup() again with the reset cause ESP_RST_DEEPSLEEP.
- Espressif ESP32-WROOM-32 datasheet (module on the DevKitC: 3.0 to 3.6 V supply)
- Espressif ESP32 Series datasheet (operating voltage, RF and power-mode currents)
- ESP-IDF Programming Guide: Fatal Errors (panic handler, Guru Meditation, brownout)
- ESP-IDF API reference: Miscellaneous System APIs (esp_reset_reason)
- ESP-IDF Programming Guide: Memory Types (__NOINIT_ATTR and RTC memory)
- ESP-IDF API reference: Sleep Modes (esp_deep_sleep)
- Arduino-ESP32 documentation (the Arduino core for the ESP32)
- Arduino-ESP32 Kconfig: ARDUINO_RUNNING_CORE (setup() and loop() run on core 1)
- ESP-IDF Kconfig reference: brownout detector voltage levels
اختبار يتاح بعد الخطوة الأخيرة.