# Watchdog Timers: Stopping Motors When Code Hangs

> How a watchdog timer resets a hung robot controller, how to set its timeout, and why motor driver pull-downs matter.

Canonical: https://roboticsengineering.in/watchdog-timer-robot-safety/ | Published: 2026-11-21 | Updated: 2026-11-21 | Author: Jignesh S Panchal

**A watchdog timer is a hardware timer that resets the microcontroller if your code does not “feed” it in time, so a hung program cannot leave motors running forever. For watchdog timer robot safety, enable it with a timeout a few times longer than your slowest normal loop, feed it only from healthy code, and make sure the motor driver defaults to off when the chip resets.**

## Key takeaways

- A watchdog resets the chip if it is not fed within its timeout, which recovers from a program hang or an endless loop.
- When the chip resets, its output pins usually become floating inputs, so a motor driver needs pull-down resistors to stay off.
- The timeout should be several times longer than your slowest legitimate loop pass, but short enough that a runaway robot cannot do harm.
- A watchdog does not replace an emergency stop; it only covers the case where the code stops running.

## What a Watchdog Does

A watchdog is a counter that runs independently of your program and counts down. Your code must reset it regularly, which is called feeding or kicking the dog. If the count reaches zero, the watchdog assumes the program has hung and forces a reset. This protects against infinite loops, a blocked wait for a sensor, or a crash caused by memory corruption.

The danger for robots is specific. If the processor freezes while the PWM hardware keeps running, the motors keep spinning at the last speed. A frozen line follower may drive off a table and a frozen arm may push against its end stop. A watchdog turns that into a reset, and a well-designed stop action makes the reset safe.

**Caution:** a watchdog covers software hangs only. It does not cover a stuck relay, a shorted driver or a mechanical fault. Follow the guidance for your robot’s class and add a hardware cut-off where people could be hurt.

## Setting the Timeout

The timeout is the longest time the watchdog waits before resetting. Choose it from your own loop: measure the slowest normal pass (see [control loop timing and jitter](https://roboticsengineering.in/control-loop-timing-jitter-arduino/)), multiply by a safety factor of about 3 to 5, and round up to the nearest value the chip supports.

For example, if your loop normally takes 5 ms with an occasional 40 ms pass, a timeout of 40 x 3 = 120 ms or more is sensible, so a choice of 250 ms or 500 ms leaves a margin. Too short and normal slow passes cause surprise resets; too long and a hung robot travels far before the reset. On AVR the available values come from the library, for example `WDTO_250MS` and `WDTO_500MS` in `avr/wdt.h`. The ESP32 task watchdog is configured in seconds or milliseconds through its own API, which changed between Arduino-ESP32 core versions, so read the documentation for the version you installed.

## Feeding the Dog Safely

Feeding safely means resetting the watchdog only when the whole control path has proven it is healthy. Feed once per pass of the main loop, at the end, after sensors are read and motor commands are updated.

```
// Untested AVR pattern (ATmega328P boards)
#include <avr/wdt.h>
void setup() {
  // motor pins: set outputs LOW first, then enable the watchdog
  wdt_enable(WDTO_500MS);
}
void loop() {
  readSensors();
  updateControl();
  setMotors();
  wdt_reset();          // fed only if every step above returned
}
```

Never feed from a timer interrupt, because the interrupt can keep running while the main loop is stuck, and the watchdog would never fire. The same logic applies on an ESP32 with several tasks: each task that matters should feed or be monitored, not just one. See [ESP32 FreeRTOS robot tasks](https://roboticsengineering.in/esp32-freertos-robot-tasks/) for task layout, and the [non-blocking state machine guide](https://roboticsengineering.in/arduino-millis-non-blocking-robot-state-machine/) to keep the loop quick so feeding stays regular.

## Stop Action on Timeout

The stop action is what the motors do while the chip is resetting and restarting. Understand what your hardware does here, because the watchdog only restarts the code. On reset, the output pins of AVR and ESP32 chips typically go to a high-impedance input state (check your chip’s documentation, since some pins on the ESP32 have boot-time pull-ups or pull-downs). A floating input on a motor driver can read as high or low, so the motor may run anyway.

- Add a pull-down resistor on each driver input and enable pin, so floating means off. A value in the range of 10 kilohms is commonly used; check your driver’s input specification.
- At the start of `setup()`, set motor pins to the stopped state before anything else.
- Use the driver’s enable or standby pin as the primary off switch.
- On start, read the reset-cause register (on AVR the MCUSR register) and report a watchdog reset, for example by flashing an LED, so a hidden reset does not go unnoticed.

A related protection is a command heartbeat: if remote commands stop arriving for a set time, stop the motors. This covers a lost Bluetooth or Wi-Fi link even when the code itself is healthy. See the [Bluetooth robot car](https://roboticsengineering.in/projects/bluetooth-robot-car-hc05-arduino/) and the [ESP32 Wi-Fi robot car](https://roboticsengineering.in/projects/esp32-wifi-robot-car-web-control/) for control links where this matters. The [ESP32 DevKit V1 page](https://roboticsengineering.in/components/esp32-devkit-v1/) lists the board these projects use.

## Testing a Hang

Testing a hang means causing one on purpose, with the wheels off the ground, and watching what happens. Add a debug command that enters an endless loop, such as `while (true) {}`, and trigger it from the serial monitor. Expected result: the motors stop within your timeout, the board restarts, and the reset-cause report shows a watchdog reset.

Repeat with the loop stuck in different places: inside a sensor read, inside a long `delay()`, and inside the motor update. If the motors keep running in any case, the stop action is incomplete. Test with the robot lifted on a stand, never on the floor or the bench edge, and keep a hand near the power switch.

## Mistakes That Defeat It

- Feeding the watchdog from an interrupt or a separate timer, so it is fed even when the main code is dead.
- Feeding inside a helper function that is called from many places, so a stuck loop that calls it still feeds.
- Setting a timeout so long that the robot does harm before the reset.
- Enabling the watchdog on an old bootloader that does not clear it after a reset, which can trap the board in a reset loop. This has been reported with older Arduino bootloaders, so check yours before depending on it.
- Forgetting the pull-downs, so a reset leaves the motor driver inputs floating.

If a robot resets by itself for no clear reason, a watchdog is not always the cause. Power dips from motor start can also reset the chip; compare with the advice in [Arduino robot not working: troubleshooting](https://roboticsengineering.in/arduino-robot-not-working-troubleshooting/). More safety and programming topics are in the [Learn hub](https://roboticsengineering.in/learn/).

## How to check this yourself

1. Read the watchdog section of your chip’s datasheet or your core’s documentation, and note the available timeouts.
2. Measure your slowest loop pass over several minutes and set the timeout to 3 to 5 times that value.
3. Lift the robot, add the deliberate hang command and confirm the motors stop and the board restarts.
4. With the board held in reset, measure the voltage on each motor-driver input. It should sit at the off level because of the pull-downs.
5. Check that your reset-cause report prints a watchdog reset after the test.

## Common mistakes

- Treating a watchdog as an emergency stop.
- Testing only with the robot on the floor.
- Not logging that a watchdog reset occurred.
- Using the same timeout value from a tutorial without measuring your own loop.

## Frequently asked questions

### What is a watchdog timer in Arduino?

It is a built-in timer that resets the board if your sketch does not call `wdt_reset()` within the chosen timeout. It exists to recover from hangs and endless loops. On Uno-class boards you use the `avr/wdt.h` library; the ESP32 has task and interrupt watchdogs configured through its own API.

### How do I stop a robot if the code freezes?

Enable a watchdog so that the chip resets, and add pull-down resistors on the motor driver inputs so a reset means stop. Also stop the motors if commands from the remote stop arriving. For anything that can injure people, add a hardware cut-off as well.

### What timeout should I set for a watchdog?

Measure your slowest normal loop pass and use roughly three to five times that, rounded up to a value your chip supports. Common choices on small robots fall in the few hundred millisecond range. Shorter risks false resets; longer lets a hung robot move further.

## Quick checklist

- Set motor pins to the off state at the very start of setup().
- Choose the timeout from your measured slowest loop pass.
- Feed the watchdog once per healthy main-loop pass only.
- Add pull-downs so a reset leaves the motor driver off.
- Test a deliberate hang with the wheels off the ground.

## Related reading

- [Wiring checker](https://roboticsengineering.in/wiring-checker/) to confirm which pins are safe to use on ESP32 and Arduino Uno.
- [Brownout resets explained](https://roboticsengineering.in/robot-brownout-reset-explained/) for resets caused by power instead of code.
- [Arduino IDE setup](https://roboticsengineering.in/arduino-ide-setup/) for installing the board packages used here.
- [Stepper Resonance: Why Motors Shake at One Speed](https://roboticsengineering.in/stepper-resonance-explained/)

## About the author and how this is kept accurate

Written, checked and maintained by [Jignesh Panchal](https://www.linkedin.com/in/jignesh-s-panchal/) of [Jaadu Digital](https://jaadudigital.com/). Figures in worked examples come from the stated assumptions, not from a bench test of our own. Read the [editorial standard](https://roboticsengineering.in/editorial-standard/), follow the author on [LinkedIn](https://www.linkedin.com/in/jignesh-s-panchal/), or [send a correction](https://roboticsengineering.in/contact/). Last reviewed: November 2026.
