Home / Learn Embedded C / Project: Watchdog-Protected Main Loop
Project: Watchdog-Protected Main Loop
Add a properly-designed watchdog to an existing main loop, following the robust-petting lesson.
What's actually going on
In plain terms
A main loop protected by a watchdog timer that is only petted after verifying genuine, multi-subsystem health — not naively petted on every single loop iteration regardless of actual status.
Why we use it
Real projects are where embedded C patterns become habits instead of theory.
Under the hood
Work through the steps in order; do not skip the checklist.
Real-world use
- Portfolio project
- Interview preparation
- Real hardware practice
What you will build
A main loop protected by a watchdog timer that is only petted after verifying genuine, multi-subsystem health — not naively petted on every single loop iteration regardless of actual status.
| Level | Time needed |
|---|---|
| Intermediate | 1-2 hours |
What you need (tools and setup)
| Tool | What it is | Why you need it | Cost |
|---|---|---|---|
| This course's live C sandbox | A small in-browser C interpreter with simulated GPIO/UART/ADC/PWM. | Every project in this course can be tried here first with zero setup, no board required. | free, built in |
| Code editor (VS Code or similar) | The program you write firmware source files in. | You need somewhere to write and organise the real .c/.h files for the project. | free |
| A real development board (Arduino/AVR, STM32 Nucleo, or ESP32 dev board) — optional | The actual microcontroller hardware. | Lets you move from the simulated board to a real one once the logic works here. | low cost, optional |
Step 1: see it working in the sandbox
Petting only happens when every tracked subsystem reports healthy, exactly the robust pattern from the Watchdog group's in-depth lesson.
Step by step
- Identify the key subsystems whose health should gate the watchdog pet (sensor read success, comms status, etc).
- Have each subsystem set a simple health flag each cycle, based on its own actual success/failure.
- Pet the watchdog only when every tracked health flag is currently good.
- Deliberately simulate a subsystem failure and confirm the watchdog is NOT petted, leading to a reset.
- Choose a watchdog timeout long enough for worst-case normal operation but short enough for acceptable recovery time.
Checklist before you call it done
| Check | Why it matters | How to verify |
|---|---|---|
| Watchdog pets only when all tracked subsystems report healthy | Confirms this is a genuine health-based pet, not a naive always-pet | Review the exact condition gating the pet call |
| A simulated subsystem failure actually results in a reset | Proves the watchdog genuinely catches unhealthy states | Force a failure flag and confirm the reset occurs |
| Chosen timeout is long enough to avoid false resets under normal load | Prevents an overly aggressive, falsely-triggering watchdog | Test under realistic worst-case timing conditions |
Deliberately testing what happens when a pet is skipped is exactly how you validate a watchdog actually protects the system before shipping.
Quick quiz
1. Why is petting the watchdog unconditionally on every loop iteration considered an ineffective watchdog design?
As covered in the Watchdog group, petting unconditionally regardless of actual system health defeats the entire point of having a watchdog at all.