DEV Community

Hedy
Hedy

Posted on

Why is my MCU resetting unexpectedly?

Unexpected MCU (Microcontroller Unit) resets are common debugging challenges in embedded systems. These resets can be caused by a variety of hardware or software issues. Below is a structured way to understand and troubleshoot the root causes:

Image description

Common Causes of Unexpected MCU Resets
1. Watchdog Timer Expiry

  • Description: The watchdog(What is a Watchdog?) expects regular "kicks." If your code hangs or runs too long, the watchdog resets the MCU.
  • Fix: Make sure you're feeding the watchdog regularly, or disable it for testing if not needed.

2. Power Supply Issues
Brown-Out: Sudden voltage drops below a threshold trigger a reset.

Noise or Instability: Spikes or ripple on power lines can cause brown-out or POR (Power-On Reset).

Fix:

  • Use proper decoupling capacitors (0.1 µF close to Vcc/GND).
  • Check your power supply and regulators with an oscilloscope.

3. Stack Overflow or Memory Corruption
Caused by:

  • Recursive calls without a base case
  • Large local variables (especially arrays)
  • Interrupts consuming too much stack

Fix: Monitor stack usage; reduce RAM usage; use FreeRTOS or memory diagnostics if needed.

4. Faulty Code / Infinite Loops

  • A code bug can cause the MCU to hang, triggering the watchdog or system fault handler.
  • Use breakpoints or LED indicators to trace code sections before the reset.

5. Hardware Faults / Floating Pins

  • Unused or input-only pins left floating can pick up noise and trigger unintended behavior.
  • Fix: Pull unused inputs to a known state (e.g., via pull-up/pull-down resistors).

6. Reset Pin Glitching

  • External noise or a faulty reset button can trigger a reset.
  • Fix: Add a capacitor (e.g., 0.1 µF) or pull-up resistor (e.g., 10kΩ) to the reset line.

7. Electromagnetic Interference (EMI)

  • High-speed switching circuits or motors nearby may introduce noise.
  • Fix: Shield critical signal lines, improve PCB layout, or add EMI filters.

8. ESD or Surge Events

  • Electrostatic discharge or power line surges can cause sporadic resets.
  • Fix: Use ESD protection diodes and transient voltage suppressors.

9. Clock Failures

  • If the external or internal oscillator fails or becomes unstable, it may cause a reset or fault.
  • Fix: Check crystal load caps, or fall back to internal oscillator.

How to Debug MCU Resets
1. Check Reset Flags
Many MCUs (like STM32) have registers to indicate the reset source:

c

if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)) {
    // Watchdog caused reset
}
Enter fullscreen mode Exit fullscreen mode

2. Log Data to EEPROM or Flash
Save a reset cause flag before booting to track last state.

3. Use a Debugger (SWD/JTAG)
Step through code, check reset reasons, stack state, and variables.

4. Isolate Modules
Disconnect peripherals or comment out blocks of code to narrow down.

Bonus Tip (for STM32/AVR/ESP)
Many MCUs provide a reset cause register:

  • STM32: RCC_CSR register (check flags like PINRSTF, PORRSTF, IWDGRSTF)
  • AVR: MCUSR register
  • ESP32: esp_reset_reason() function

Top comments (0)