ESP8266 Fatal exception and Wdt Reset

This post will guide you common issues and mistakes that cause Fatal Exception and wdt reset. Fatal exception comes at execution time. program compiles well logically looks correct but at Running suddenly these fatal exceptions come. They are difficult to find out. But I have kept a record of few causes of these fatal exception. That I am sharing here.

First we will simulate these errors with test code and correct it.

Example 1: LED Blink Wdt Reset

LED Blink Program Giving Wdt Reset Cause Error

Now we will look into program that caused wdt reset

wdt reset esp8266
wdt reset esp8266

The program is simple LED Blink but Why this wdt reset error ? can you guess….

This error is caused simply because #define LED 9. This IO 9 of ESP8266 is on bottom side these IOs cannot be used. Its My First Lesson.

Lesson 1: Make sure you are using correct IO numbers.

Example 2: External Interrupt Causing Fatal Exception(9)

The error in serial monitor.

Lets Look at the program that caused fatal exception(9)

esp8266 Fatal exception (9)
Fatal Exception(9)

Look closely to the Serial monitor You can See “You Pressed Flash Button” message that we are sending from interrupt handler.

Behavior of program:

LED Blinking works perfectly No Wdt Reset or any Fatal Exception until we send external interrupt. Right. Whats the problem then? Interrupt is called we can see message also.

Where is our second message “I am after 1 Sec” Yes you are right some thing after first serial out. i.e. delay(1000); This line causes fatal exception(9). Then Why? Logically every thing is correct then this fatal exception(9) from a delay????

Yes its bug in ESP8266 libraries. that are get used while compilation.

Lesson No. 2: Never Use Delays in External Interrupt Handler

How to Solve this Fatal Exception(9)? I want to use delay in interrupt

To solve this issue I use delayMicroseconds in interrupt handler this will not create any problem.

Modified Program with Same Functionality Without Fatal Exception(9) Error

Example 3: ESP as Web Server and AP

This program Looks perfect. But causing  No errors or Doing nothing.

After Uploading This program.

  1. We must see HomeServer wifi network.
  2. But there is no such network created by this code.

Why No WiFi Access point ?

Compilation is ok, No Errors or Exceptions in serial monitor then Why missing HotSpot?

The problem is in Analog = String(analogRead(A0));

By commenting Analog read line and re-uploading the program you will immediately see the HomeServer access point

Lesson No.3: Give Enough Time to server.handleClient();

To solve this problem update loop() with below code

You can use analogRead in hadleRoot() subroutine also.

Example No 4: ESP8266 Trapped in Infinite Loop

This type of error occurs when there is any loop that holding the program. Such as polling of a IO line. Waiting for some event to occur. Such as Wait until Motion Detected. Motion sensor related programs.

Lesson 4: Never Use IO polling in program holding state

Secret Function yield() to get rid of many problems

The yield() function is also implemented inside the ESP8266 libraries:


This is one of the most critical differences between the ESP8266 and a more classical Arduino microcontroller. The ESP8266 runs a lot of utility functions in the background – keeping WiFi connected, managing the TCP/IP stack, and performing other duties. Blocking these functions from running can cause the ESP8266 to crash (fatal exceptions) and reset itself. To avoid these mysterious resets, avoid long, blocking loops in your sketch.

The amazing creators of the ESP8266 Arduino libraries also implemented a yield() function, which calls on the background functions to allow them to do their things.

How to use yield() Function?

Example 1: Causing Hang and WDT reset

while(digitalRead(Key)==HIGH); //This will hold the program until button event is occurs

To solve this issue use yield() function in this way

Now Let’s See List of Exception Codes of ESP8266

ESP8266 Exception cause

The reboot state will not change after software WDT reset or software reset. For example, when the first reset is caused by a power reboot, the rst cause number is 1. After software reset, the rst cause number will still be 1.
EXC-CAUSE Code Cause Name Cause Description Required Option EXC-VADDR Loaded
0 IllegalInstructionCause Illegal instruction Exception No
1 SyscallCause SYSCALL instruction Exception No
2 InstructionFetchErrorCause Processor internal physical address or data error during instruction fetch Exception Yes
3 LoadStoreErrorCause Processor internal physical address or data error during load or store Exception Yes
4 Level1InterruptCause Level-1 interrupt as indicated by set level-1 bits in the INTERRUPT register Interrupt No
5 AllocaCause MOVSP instruction, if caller�s registers are not in the register file Windowed Register No
6 IntegerDivideByZeroCause QUOS, QUOU, REMS, or REMU divisor operand is zero 32-bit Integer Divide No
7 Reserved for Tensilica
8 PrivilegedCause Attempt to execute a privileged operation when CRING ? 0 MMU No
9 LoadStoreAlignmentCause Load or store to an unaligned address Unaligned Exception Yes
10..11 Reserved for Tensilica
12 InstrPIFDataErrorCause PIF data error during instruction fetch Processor Interface Yes
13 LoadStorePIFDataErrorCause Synchronous PIF data error during LoadStore access Processor Interface Yes
14 InstrPIFAddrErrorCause PIF address error during instruction fetch Processor Interface Yes
15 LoadStorePIFAddrErrorCause Synchronous PIF address error during LoadStore access Processor Interface Yes
16 InstTLBMissCause Error during Instruction TLB refill MMU Yes
17 InstTLBMultiHitCause Multiple instruction TLB entries matched MMU Yes
18 InstFetchPrivilegeCause An instruction fetch referenced a virtual address at a ring level less than CRING MMU Yes
19 Reserved for Tensilica
20 InstFetchProhibitedCause An instruction fetch referenced a page mapped with an attribute that does not permit instruction fetch Region Protection or MMU Yes
21..23 Reserved for Tensilica
24 LoadStoreTLBMissCause Error during TLB refill for a load or store MMU Yes
25 LoadStoreTLBMultiHitCause Multiple TLB entries matched for a load or store MMU Yes
26 LoadStorePrivilegeCause A load or store referenced a virtual address at a ring level less than CRING MMU Yes
27 Reserved for Tensilica
28 LoadProhibitedCause A load referenced a page mapped with an attribute that does not permit loads Region Protection or MMU Yes
29 StoreProhibitedCause A store referenced a page mapped with an attribute that does not permit stores Region Protection or MMU Yes
30..31 Reserved for Tensilica
32..39 CoprocessornDisabled Coprocessor n instruction when cpn disabled. n varies 0..7 as the cause varies 32..39 Coprocessor No
40..63 Reserved

Infos from Xtensa Instruction Set Architecture (ISA) Reference Manual



One thought on “ESP8266 Fatal exception and Wdt Reset

  1. Very useful, I was far from the way of a solution. If I should send pictures, I should show that it’s not always so … easy is not the good world. With the same sketch, if you try several times to flash it, following messages can be different.
    At least, thank you very much.

Leave a Reply