ENع
←Brown-out and faults
Lesson 46 · Low power and reliability

Brown-out, resets and fault handling

14 min10 steps5 questionsAdvanced
About this lesson

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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 46 · Interactive
01 / 10

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. Wi-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)
  3. 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 again
  4. The 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 chip
  5. A 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_BROWNOUT
  6. Ask 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 sleep
  7. Brown-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() again
  8. A 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_PANIC
  9. Count 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, 3
  10. 3 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

Quiz Unlocks after the last step.