yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
ESP32 Core Board📖
1. ESP32 Introduction
  • 1. ESP32S3 Overview
  • 2. Environment Setup Introduction
  • 3. Dev Board Introduction
2. ESP-IDF Development Tutorial
1. Before Development
  • 1. Environment Setup (Windows)
  • 2. Environment Setup (Linux)
  • 3. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. DMA
  • 8. ADC
  • 9. PWM Breathing LED
  • 10. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. PSRAM External Memory Experiment
  • 5. Flash Read and Write Experiment
  • 6. USB Emulated Mass Storage Experiment
4. FreeRTOS operating system
  • 1.FreeRTOS Core Concepts Theory
  • 2.Task Creation and Management
  • 3.Queue
  • 4.Binary Semaphore
  • 5.Mutex
  • 6.Event Groups
  • 7.Software Timer
  • 8.Task Scheduler
  • 9.Low Power Mode
  • 10.Deadlock
  • 11.Priority Inversion
5. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. Socket Programming Basics
  • 9. HTTP Client Experiment
  • 10. HTTP Server Experiment
  • 11. MQTT IoT Communication Protocol
  • 12. WIFI OTA Upgrade
  • 13. Bluetooth Fundamentals
  • 14. Bluetooth Slave Mode
  • 15. Bluetooth Master Mode
6. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
7. Xiaozhi AI Voice
  • 1. Burn Xiaozhi Firmware
  • 2. Network and Configuration
  • 3. AI Agent Settings
  • 4. Program Analysis
  • 5. Free Chat
3. Arduino Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. ADC
  • 8. PWM Breathing LED
  • 9. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication
  • 2. I2C OLED Display
  • 3. SPI Bus Communication
  • 4. Flash Read and Write
  • 5. USB Emulated Mass Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Server Experiment
  • 5. UDP Client Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT IoT Communication Protocol
  • 11. WIFI OTA Upgrade
  • 12. Bluetooth Fundamentals
  • 13. Bluetooth Slave Mode
  • 14. Bluetooth Master Mode
5. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
4. MicroPython Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
  • 3. Thonny IDE
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. TIMER_IT
  • 5. Watchdog Timer
  • 6. ADC
  • 7. PWM Breathing LED
  • 8. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. Flash Read and Write Experiment
  • 5. Print Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT
  • 11. Bluetooth Fundamentals
  • 12. Bluetooth Slave Mode
  • 13. Bluetooth Master Mode
5. Audio Applications
  • 1. I2S Audio Playback Experiment
  • 2. Repeater Experiment
Download
  • Code
  • Hardware_Info
  • 3D_Model_File
  • Software_Tools
  • VMware
  • VM_File
Download ZIP

Welcome to ESP32 Core Board repository

5. Watchdog Timer

Watchdog Timer


Watchdog Timer1. Watchdog Overview2. Core Concepts2.1 How the Watchdog Works2.2 The Three-Phase Logic of This Experiment2.3 Core API3. Hardware Dependencies4. Project Architecture and Flow5. Source Code Details5.1 Parameter Configuration and Initialization5.2 Phase 1 — Auto-Feed5.3 Phase 2 — Button Feed + Countdown5.4 Phase 3 — Timeout Reset6. Operation Procedure6.1 Flashing and Running6.2 Normal Phenomenon6.3 Verification Methods7. Common Problems


1. Watchdog Overview

Watchdog is an independently running hardware countdown timer. When the system is normal, the software periodically "feeds the dog" (refreshes the counter); if the program crashes, enters an infinite loop, or deadlocks and fails to feed the dog, the counter reaches zero → the hardware automatically resets the MCU, achieving "unattended self-healing".

This experiment demonstrates the full lifecycle of the watchdog in three phases:

  1. Auto-feed — the program automatically feeds 5 times, proving that normal feeding does not cause a reset

  2. Manual feed + countdown — stop auto-feeding and feed the dog manually via the BOOT button; each button press resets the countdown

  3. Timeout reset — no button press within 5s → countdown reaches zero → stop feeding → the hardware WDT automatically resets the system

Typical application scenarios:

Application TypeExample
Anti-crash protection for productsEssential for mass-produced devices to ensure self-recovery when unattended
Network communicationAutomatically restart when WiFi/BLE connection hangs
Sensor readingReset on peripheral timeout/no response
Low-power wake-upEnter the normal initialization flow after a watchdog reset

2. Core Concepts

2.1 How the Watchdog Works

Feed                    Timeout
   │   │   │              │
   ▼   ▼   ▼              ▼
  ┌───────────────┐     ┌──────────┐
  │ normal feed   │     │ timeout →│
  │ counter reset │ ──→ │ HW reset │
  └───────────────┘     └──────────┘
        ↑                      │
        └──── restart after reset ──┘

Each call to wdt.feed() resets the WDT counter to the timeout value. If the dog is not fed again before the counter reaches zero → the WDT hardware automatically resets the chip.

2.2 The Three-Phase Logic of This Experiment

xxxxxxxxxx
Phase 1: Auto-feed       Phase 2: Button feed + countdown   Phase 3: Timeout reset
┌─────────────────────┐   ┌─────────────────────────┐       ┌──────────────┐
│ for i in 1..5:      │   │ while remaining > 0:    │       │ while True:  │
│   wdt.feed()        │   │   if BOOT falling edge: │       │              │
│   print("Feed #")   │   │     wdt.feed()          │       │   pass       │
│   sleep(1000ms)     │   │     remaining = 5       │       │              │
│                     │ → │   if 1s elapsed:        │   →   │              │
│                     │   │     remaining -= 1      │       │              │
│                     │   │     print("Countdown")  │       │              │
│                     │   │   sleep(50ms)           │       │              │
└─────────────────────┘   └─────────────────────────┘       └──────────────┘

2.3 Core API

​x
from machine import WDT

wdt = WDT(timeout=7000)  # enable WDT, 7s timeout
wdt.feed()               # feed: reset the counter to timeout

3. Hardware Dependencies

GPIOPeripheralModeDescription
0BOOT buttonPin.IN + Pin.PULL_UPTrigger source for manual feeding in Phase 2
—WDTHardware timerIndependent of the CPU, 7s timeout

4. Project Architecture and Flow

xxxxxxxxxx
Script execution (single-file .py)
  │
  ├─ Parameter config: FEED_COUNT=5, COUNTDOWN_SEC=5, POLL=50ms
  ├─ boot = Pin(0, IN, PULL_UP)                  // BOOT button
  ├─ wdt = WDT(timeout=7000)                     // WDT 7s timeout
  │
  ├─ [Phase 1] for i in 1..5:                    // auto-feed 5 times
  │    ├─ wdt.feed()
  │    ├─ print("Feed #%d")
  │    └─ sleep_ms(1000)
  │
  ├─ [Phase 2] while remaining > 0:              // button feed + countdown
  │    ├─ pressed = boot.value() == 0            // read button
  │    ├─ if falling edge (pressed && !last):     // on the press moment
  │    │    ├─ wdt.feed()                        // feed
  │    │    ├─ remaining = COUNTDOWN_SEC         // reset countdown
  │    │    └─ tick = 0                          // reset tick
  │    ├─ last_state = pressed                   // update state
  │    ├─ if tick % 20 == 0: print("Countdown")  // print every second
  │    ├─ sleep_ms(50)                           // 50ms poll
  │    └─ tick++; if tick >= 20: tick=0, remaining--
  │
  └─ [Phase 3] print("Watchdog timeout!")        // timeout reset
       └─ while True: pass                       // stop feeding, wait for WDT HW reset

5. Source Code Details

Full source code: 5.Source Code → MicroPython → 1.Basic Peripherals → 5.WATCH_DOG → 5.WATCH_DOG.py

5.1 Parameter Configuration and Initialization

xxxxxxxxxx
from machine import WDT, Pin  # WDT + GPIO
import time                    # delay

FEED_COUNT       = 5          # auto-feed count
FEED_INTERVAL_MS = 1000       # feed interval (ms)
COUNTDOWN_SEC    = 5          # countdown after stopping (seconds)
POLL_INTERVAL_MS = 50         # button poll interval (ms)
TICKS_PER_SEC    = 1000 // POLL_INTERVAL_MS  # poll ticks per second

# initialize the BOOT button (GPIO0, pull-up input, press=LOW)
boot = Pin(0, Pin.IN, Pin.PULL_UP)

# enable the hardware watchdog, 7s timeout (2s margin beyond the 5s countdown)
wdt = WDT(timeout=7000)

WDT(timeout=7000) has a 7s timeout, 2s more than the 5s countdown, ensuring that the board is not reset prematurely before the countdown reaches 0. TICKS_PER_SEC = 20 is derived from 1000ms / 50ms.

5.2 Phase 1 — Auto-Feed

xxxxxxxxxx
# Phase 1 — Auto-feed (5 times, 1s interval)
for i in range(1, FEED_COUNT + 1):
    wdt.feed()
    print("Feed #%d" % i)
    time.sleep_ms(FEED_INTERVAL_MS)

The 5 auto-feeds take about 5s in total and demonstrate that the system does not reset during normal feeding.

5.3 Phase 2 — Button Feed + Countdown

xxxxxxxxxx
# Phase 2 — Manual feed + countdown
# Poll BOOT every 50ms; falling edge → feed immediately, reset countdown
print("Auto-feeding stopped! Press BOOT to feed...")

remaining  = COUNTDOWN_SEC
tick       = 0
last_state = False

while remaining > 0:
    pressed = (boot.value() == 0)    # LOW = pressed

    # Falling-edge detection — feed only on release→press, no repeat on hold
    if pressed and not last_state:
        wdt.feed()
        print(">>> Boot feed! WDT reset <<<")
        remaining = COUNTDOWN_SEC
        tick      = 0
    last_state = pressed

    if tick % TICKS_PER_SEC == 0:
        print("Countdown %d ..." % remaining)

    time.sleep_ms(POLL_INTERVAL_MS)

    tick += 1
    if tick >= TICKS_PER_SEC:
        tick = 0
        remaining -= 1

Every button press before the countdown reaches zero resets remaining = 5 and tick = 0, which is equivalent to "extending life". Falling-edge detection ensures that holding the button does not feed repeatedly.

5.4 Phase 3 — Timeout Reset

xxxxxxxxxx
# Phase 3 — Timeout reset
# Countdown reached 0, stop feeding, HW WDT will reset the system
print("Watchdog timeout! Waiting for WDT reset...")
while True:
    pass

After the countdown reaches zero, the program enters an infinite loop and stops feeding; the hardware WDT resets the chip after the timeout.


6. Operation Procedure

6.1 Flashing and Running

Thonny IDE steps: See the 1. Before Development → 3.Thonny IDE chapter.

6.2 Normal Phenomenon

Shell output:

xxxxxxxxxx
==================================
ESP32-S3 Watchdog Test (MicroPython)
==================================
BOOT button (GPIO0) ready — press to feed WDT
WDT timeout: 5s
Feed #1
Feed #2
Feed #3
Feed #4
Feed #5
Auto-feeding stopped! Press BOOT to feed...
Countdown 5 ...
>>> Boot feed! WDT reset <<<                  <<----feed
Countdown 5 ...
Countdown 4 ...
>>> Boot feed! WDT reset <<<                  <<----feed
Countdown 5 ...
Countdown 4 ...
Countdown 3 ...
Countdown 2 ...
>>> Boot feed! WDT reset <<<                  <<----feed
Countdown 5 ...
Countdown 4 ...
Countdown 3 ...
Countdown 2 ...
Countdown 1 ...
Watchdog timeout! Waiting for WDT reset...   <<----timeout restart

Timeout reset/restart:

Timeout reset

Manual feeding:

Manual feeding

6.3 Verification Methods

OperationExpected Result
Power up without pressing any buttonPhase 1 auto-feeds 5 times → Phase 2 countdown → resets after 5s
Press BOOT every 4s during Phase 2The countdown keeps resetting to 5; never resets
Hold BOOT during Phase 2Feeds only once at the press moment; holding does not feed repeatedly
Change timeout=7000 to timeout=4000Countdown shortened to 4s, timeout triggered faster
Comment out wdt.feed() in Phase 2Even pressing the button does not feed; resets when the countdown reaches 0

7. Common Problems

SymptomCauseSolution
Resets before Phase 1 even finishestimeout=7000 too short, FEED_INTERVAL_MS too largeEnsure the feed interval < timeout
No response from the button in Phase 2Falling-edge detection not last_state not effectiveConfirm last_state = pressed at the end of the loop
Shell disconnects mid-printWatchdog reset causes the USB serial port to disconnectNormal phenomenon; Thonny reconnects automatically after the reset
WDT(timeout=7000) reports an errorFirmware version does not support WDT with argumentsUse WDT() without arguments for the default timeout
Countdown gets stucktick increment or reset logic is wrongConfirm tick = 0 and remaining -= 1 after tick >= TICKS_PER_SEC
TIMER_IT
ADC

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文