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.

In shortAdd 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.

LevelTime needed
Intermediate1-2 hours

What you need (tools and setup)

ToolWhat it isWhy you need itCost
This course's live C sandboxA 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) — optionalThe 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

A watchdog pet gated on real subsystem health checksC: edit and run

Petting only happens when every tracked subsystem reports healthy, exactly the robust pattern from the Watchdog group's in-depth lesson.

Step by step

  1. Identify the key subsystems whose health should gate the watchdog pet (sensor read success, comms status, etc).
  2. Have each subsystem set a simple health flag each cycle, based on its own actual success/failure.
  3. Pet the watchdog only when every tracked health flag is currently good.
  4. Deliberately simulate a subsystem failure and confirm the watchdog is NOT petted, leading to a reset.
  5. Choose a watchdog timeout long enough for worst-case normal operation but short enough for acceptable recovery time.

Checklist before you call it done

CheckWhy it mattersHow to verify
Watchdog pets only when all tracked subsystems report healthyConfirms this is a genuine health-based pet, not a naive always-petReview the exact condition gating the pet call
A simulated subsystem failure actually results in a resetProves the watchdog genuinely catches unhealthy statesForce a failure flag and confirm the reset occurs
Chosen timeout is long enough to avoid false resets under normal loadPrevents an overly aggressive, falsely-triggering watchdogTest under realistic worst-case timing conditions
Tip:Commit the first working version and every later improvement as separate commits, so the diff itself becomes a teaching example.
Deliberately skipping one pet to confirm the watchdog still worksC: edit and run

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?