<header class="entry-header" style="box-sizing: inherit; text-align: justify; margin-bottom: 10px; font-family: poppins, sans-serif; background-color: #ffffff;">
   <div class="entry-box" style="box-sizing: inherit;">
      <header class="entry-header" style="box-sizing: inherit; margin-bottom: 10px;">
         <h1 class="entry-title" style="box-sizing: inherit; overflow-wrap: break-word; font-size: 2.5em; margin: 0px; clear: both; line-height: 36px; color: #313131; letter-spacing: 0.25px; text-align: left;">How to enable hardware WDT on ESP32 using Arduino IDE</h1>
         <span class="entry-meta" style="box-sizing: inherit; margin-bottom: 25px;"><span class="posted-on" style="box-sizing: inherit;"><time class="entry-date published" datetime="2020-05-05T13:38:03+02:00" style="box-sizing: inherit; font-size: 9px; letter-spacing: 1.5px; text-transform: uppercase; color: #999999;">MAY 5, 2020</time></span></span></header>
      <div class="entry-content" style="box-sizing: inherit; font-size: 1.3em; margin: 0px; line-height: 2; letter-spacing: 0.2px; font-family: montserrat, sans-serif; padding-bottom: 7px; text-align: start;">
         <p style="box-sizing: inherit; overflow-wrap: break-word; line-height: 2; letter-spacing: 0.2px; padding-bottom: 7px;">Watchdog timer (WDT) is an important feature for hardware devices like ESP32 or Arduino that need to auto recover from all kind of unexpected failures. On a&nbsp;<a href="https://iotassistant.io/raspberry-pi/how-to-set-watchdog-timer-raspberrypi/" style="box-sizing: inherit; background-color: transparent; color: #000000; transition: color 0.3s ease 0s, background-color 0.3s ease 0s, border-color 0.3s ease 0s;" target="_blank">previous post</a>&nbsp;I wrote about how WDT works and how to set WDT on a Raspberry Pi, to make sure it stays up and running 24/7.</p>
         <p style="box-sizing: inherit; overflow-wrap: break-word; line-height: 2; letter-spacing: 0.2px; padding-bottom: 7px;">If we look at the esp-idf documentation about&nbsp;<a href="https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/wdts.html" style="box-sizing: inherit; background-color: transparent; color: #000000; transition: color 0.3s ease 0s, background-color 0.3s ease 0s, border-color 0.3s ease 0s;" target="_blank">ESP32 WDT</a>&nbsp;we can see that ESP32 comes with an&nbsp;<span style="box-sizing: inherit; font-weight: 700;">interrupt watchdog&nbsp;</span>and a&nbsp;<span style="box-sizing: inherit; font-weight: 700;">task watchdog timer api.&nbsp;</span>What many people are looking for is the task watchdog timer and I will show you in this article how to implement it using Arduino IDE.&nbsp;</p>
         <h2 style="box-sizing: inherit; overflow-wrap: break-word; clear: both; font-size: 1.7em; line-height: 31.824px; margin-block: 0px;">Implementing ESP32 hardware watchdog timer using Arduino IDE</h2>
         <p style="box-sizing: inherit; overflow-wrap: break-word; line-height: 2; letter-spacing: 0.2px; padding-bottom: 7px;">On ESP32, many people implement manually some sort of watchdog timer using flags and loops. This is NOT how it should be done, especially since ESP32 comes with a hardware watchdog timer.</p>
         <p style="box-sizing: inherit; overflow-wrap: break-word; line-height: 2; letter-spacing: 0.2px; padding-bottom: 7px;">This code shows you how to properly implement WDT on ESP32 using Arduino IDE:&nbsp;</p>
         <p style="box-sizing: inherit; overflow-wrap: break-word; line-height: 2; letter-spacing: 0.2px; padding-bottom: 7px;"><span style="font-family: &quot;Source Code Pro&quot;, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, Courier, monospace; font-size: 12px; letter-spacing: 0.2px; white-space: pre-wrap; background-color: #f9f9f9;">#include &lt;esp_task_wdt.h&gt;
//3 seconds WDT
#define WDT_TIMEOUT 3
void setup() {
  Serial.begin(115200);
  Serial.println("Configuring WDT...");
  esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts
  esp_task_wdt_add(NULL); //add current thread to WDT watch
}
int i = 0;
int last = millis();
void loop() {
  // resetting WDT every 2s, 5 times only
  if (millis() - last &gt;= 2000 &amp;&amp; i &lt; 5) {
      Serial.println("Resetting WDT...");
      esp_task_wdt_reset();
      last = millis();
      i++;
      if (i == 5) {
        Serial.println("Stopping WDT reset. CPU should reboot in 3s");
      }
  }
}</span></p>
         <p style="box-sizing: inherit; overflow-wrap: break-word; line-height: 2; letter-spacing: 0.2px; padding-bottom: 7px;">Now let’s look at the code. First of all you need to include the&nbsp;<em style="box-sizing: inherit;">esp_task_wdt.h&nbsp;</em>header. This should be available if you have properly installed&nbsp;<a href="https://github.com/espressif/arduino-esp32" style="box-sizing: inherit; background-color: transparent; color: #000000; transition: color 0.3s ease 0s, background-color 0.3s ease 0s, border-color 0.3s ease 0s;" target="_blank">arduino-esp32</a>.</p>
         <p style="box-sizing: inherit; overflow-wrap: break-word; line-height: 2; letter-spacing: 0.2px; padding-bottom: 7px;">In the setup block we need to call two functions:&nbsp;</p>
         <div class="enlighter-default enlighter-v-inline enlighter-t-enlighter " style="box-sizing: inherit; font-family: &quot;Source Code Pro&quot;, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, Courier, monospace; font-size: 12px; line-height: 1.35em; overflow: inherit; white-space: pre-wrap; overflow-wrap: break-word; margin: 0px; padding: 0px; position: relative; direction: ltr; display: inline-block; color: #939393; border-radius: 3px; border: 1px solid rgb(224, 224, 224); background-color: #fafafa;"><span class="enlighter" style="box-sizing: inherit; display: inline-block; padding: 3px 5px 1px; margin: 0px 2px;"><span class="enlighter-m0" style="box-sizing: inherit; margin: 0px; padding: 0px; line-height: inherit; font-size: 1em; font-family: inherit; color: #0086b3;">esp_task_wdt_init</span><span class="enlighter-g1" style="box-sizing: inherit; margin: 0px; padding: 0px; line-height: inherit; font-size: 1em; font-family: inherit; color: #777777;">(</span><span class="enlighter-text" style="box-sizing: inherit; margin: 0px; padding: 0px; line-height: inherit; font-size: 1em; font-family: inherit; color: #000000;">uint32_t timeoutSeconds, bool panic</span><span class="enlighter-g1" style="box-sizing: inherit; margin: 0px; padding: 0px; line-height: inherit; font-size: 1em; font-family: inherit; color: #777777;">)</span></span></div>
         &nbsp;and&nbsp;
         <div class="enlighter-default enlighter-v-inline enlighter-t-enlighter " style="box-sizing: inherit; font-family: &quot;Source Code Pro&quot;, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, Courier, monospace; font-size: 12px; line-height: 1.35em; overflow: inherit; white-space: pre-wrap; overflow-wrap: break-word; margin: 0px; padding: 0px; position: relative; direction: ltr; display: inline-block; color: #939393; border-radius: 3px; border: 1px solid rgb(224, 224, 224); background-color: #fafafa;"><span class="enlighter" style="box-sizing: inherit; display: inline-block; padding: 3px 5px 1px; margin: 0px 2px;"><span class="enlighter-m0" style="box-sizing: inherit; margin: 0px; padding: 0px; line-height: inherit; font-size: 1em; font-family: inherit; color: #0086b3;">esp_task_wdt_add</span><span class="enlighter-g1" style="box-sizing: inherit; margin: 0px; padding: 0px; line-height: inherit; font-size: 1em; font-family: inherit; color: #777777;">(</span><span class="enlighter-text" style="box-sizing: inherit; margin: 0px; padding: 0px; line-height: inherit; font-size: 1em; font-family: inherit; color: #000000;">TaskHandle_t handle</span><span class="enlighter-g1" style="box-sizing: inherit; margin: 0px; padding: 0px; line-height: inherit; font-size: 1em; font-family: inherit; color: #777777;">)</span></span></div>
         .
         <p>&nbsp;</p>
         <ul style="box-sizing: inherit; overflow-wrap: break-word; list-style: none; margin: 0px 0px 12px; padding: 0px 0px 0px 20px; color: #222222;">
            <li style="box-sizing: inherit; margin-bottom: 6px; list-style: disc;"><em style="box-sizing: inherit;">esp_task_wdt_init</em>&nbsp;is used to initialise WDT with a timeout of&nbsp;<em style="box-sizing: inherit;">timeoutSeconds</em>&nbsp;and with a&nbsp;<em style="box-sizing: inherit;">panic&nbsp;</em>mode set. If&nbsp;<em style="box-sizing: inherit;">panic&nbsp;</em>is set to&nbsp;<em style="box-sizing: inherit;">true</em>, when WDT times out, it will throw a hardware panic and reboot.</li>
            <li style="box-sizing: inherit; margin-bottom: 6px; list-style: disc;"><em style="box-sizing: inherit;">esp_task_wdt_add&nbsp;</em>is used to add a task to WDT. If&nbsp;<em style="box-sizing: inherit;">handle&nbsp;</em>is&nbsp;<em style="box-sizing: inherit;">NULL,&nbsp;</em>the current task is used.&nbsp;</li>
            </ul>
         <p style="box-sizing: inherit; overflow-wrap: break-word; line-height: 2; letter-spacing: 0.2px; padding-bottom: 7px;">Now, the watchdog timer need to be reset&nbsp;<em style="box-sizing: inherit;">BEFORE&nbsp;</em>it times out! This is done with&nbsp;</p>
         <div class="enlighter-default enlighter-v-inline enlighter-t-enlighter " style="box-sizing: inherit; font-family: &quot;Source Code Pro&quot;, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, Courier, monospace; font-size: 12px; line-height: 1.35em; overflow: inherit; white-space: pre-wrap; overflow-wrap: break-word; margin: 0px; padding: 0px; position: relative; direction: ltr; display: inline-block; color: #939393; border-radius: 3px; border: 1px solid rgb(224, 224, 224); background-color: #fafafa;"><span class="enlighter" style="box-sizing: inherit; display: inline-block; padding: 3px 5px 1px; margin: 0px 2px;"><span class="enlighter-m0" style="box-sizing: inherit; margin: 0px; padding: 0px; line-height: inherit; font-size: 1em; font-family: inherit; color: #0086b3;">esp_task_wdt_reset</span><span class="enlighter-g1" style="box-sizing: inherit; margin: 0px; padding: 0px; line-height: inherit; font-size: 1em; font-family: inherit; color: #777777;">()</span></span></div>
         &nbsp;executed in the current task.&nbsp;
         <p>&nbsp;</p>
         <p style="box-sizing: inherit; overflow-wrap: break-word; line-height: 2; letter-spacing: 0.2px; padding-bottom: 7px;">You can see that in this example I am initialising the WDT with a timeout of 3 seconds and then inn the main loop I am resetting it every 2 seconds. After five resets, I stop the reset call and let it timeout. As expected, it reboots my ESP32 after 3 seconds.&nbsp;&nbsp;</p>
         <p style="box-sizing: inherit; overflow-wrap: break-word; line-height: 2; letter-spacing: 0.2px; padding-bottom: 7px;"><span style="font-family: &quot;Source Code Pro&quot;, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, Courier, monospace; font-size: 12px; letter-spacing: 0.2px; white-space: pre-wrap; background-color: #f9f9f9;">13:01:49.965 -&gt; Configuring WDT...
13:01:51.919 -&gt; Resetting WDT...
13:01:53.918 -&gt; Resetting WDT...
13:01:55.916 -&gt; Resetting WDT...
13:01:57.917 -&gt; Resetting WDT...
13:01:59.915 -&gt; Resetting WDT...
13:01:59.915 -&gt; Stopping WDT reset. CPU should reboot in 3s
13:02:02.917 -&gt; E (26113) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
13:02:02.917 -&gt; E (26113) task_wdt:  - loopTask (CPU 1)
13:02:02.951 -&gt; E (26113) task_wdt: Tasks currently running:
13:02:02.951 -&gt; E (26113) task_wdt: CPU 0: IDLE0
13:02:02.951 -&gt; E (26113) task_wdt: CPU 1: loopTask
13:02:02.951 -&gt; E (26113) task_wdt: Aborting.
13:02:02.951 -&gt; abort() was called at PC 0x400d35b3 on core 0
13:02:02.951 -&gt; 
13:02:02.951 -&gt; Backtrace: 0x4008b560:0x3ffbe170 0x4008b78d:0x3ffbe190 0x400d35b3:0x3ffbe1b0 0x400845ed:0x3ffbe1d0 0x400e8f07:0x3ffbbff0 0x400d3d43:0x3ffbc010 0x40089706:0x3ffbc030 0x40088215:0x3ffbc050
13:02:03.008 -&gt; 
13:02:03.008 -&gt; Rebooting...</span></p>
         <p style="box-sizing: inherit; overflow-wrap: break-word; line-height: 2; letter-spacing: 0.2px; padding-bottom: 7px;"><span style="font-family: &quot;Source Code Pro&quot;, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, Courier, monospace; font-size: 12px; letter-spacing: 0.2px; white-space: pre-wrap; background-color: #f9f9f9;">==============================================================================================================</span></p>
         <p style="box-sizing: inherit; overflow-wrap: break-word; line-height: 2; letter-spacing: 0.2px; padding-bottom: 7px;">다른버전 watchdog //2번째 링크 참조</p>
         </div>
      </div>
   </header>