Question.4
Consider the following Arduino UNO code and circuit diagram. What will be the behavior of the LED connected to Pin 12?

Code:
#include <avr/wdt.h>
#define LED_PIN 12
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
delay(3000);
wdt_enable(WDTO_2S);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(2000);
wdt_reset();
}Watchdog Timer (WDT) is a countdown timer that resets the microcontrollers if not cleared (or "kicked") within a preset time.
The software must regularly reset the WDT to show it's working correctly.
If a fault causes the code to hang or misbehave, and the WDT isn't cleared in time, it triggers a system reset, helping recover from software crashes or infinite loops.
Why its used?
Watchdog is a safety mechanism—if your code hangs or crashes, the watchdog can auto-reset the microcontroller to recover.
Critical Systems: If the WDT is used in safety-critical applications (e.g., medical, automotive), multiple external WDTs are also used.
In most microcontrollers, WDT is disabled by default; we can enable it :
The watchdog timer can be configured in one of the following modes (in most microcontrollers).
NOTE: Atmega328p doesn't support Windowed Watchdog Mode.
In AVR microcontrollers, the Watchdog Timer (WDT) is controlled using the following key register:
| Register | Description |
|---|---|
| WDTCSR |
|
| MCUSR → WDRF bit | WDRF bit shows if the last reset was due to the watchdog. |
NOTE : Similar Registers are present in other microcontrollers for controlling WDT. Few microcontrollers, like ARM, have more registers for advanced control and modes (Windowed mode).
Some MCUs require BOD to be enabled for WDT to work reliably during power dips.
NOTE: Some microcontrollers allow enabling or disabling the WDT via fuses or a bootloader. Others keep it always enabled for safety.
Also Some MCUs require BOD to be enabled for WDT to work reliably during power dips.
| Category | Name | Description |
|---|---|---|
| Include Header | <avr/wdt.h> | Required to access all WDT functions/constants |
Functions | wdt_enable(x) | Enables WDT with a specified timeout. e.g.WDTO_15MS, WDTO_500MS, WDTO_8S, etc |
wdt_disable() | Disables the Watchdog Timer | |
wdt_reset() | Resets (or "kicks") the WDT |
1. Basic WDT Reset Example
This example shows how to use the WDT to automatically reset the Arduino if it gets stuck and fails to reset the watchdog in time.
Code
#include <avr/wdt.h>
void setup() {
Serial.begin(115200);
Serial.println("Starting...");
// Enable WDT with 2-second timeout
wdt_enable(WDTO_2S);
}
void loop() {
Serial.println("Running normally...");
delay(1000);
// Reset the WDT timer to prevent reset
wdt_reset();
// Uncomment the next line to simulate a crash (WDT reset)
// while(1); // Infinite loop, no wdt_reset(), causes reset
}
2. WDT Interrupt Mode (Wake from Sleep)
This example puts Arduino to sleep and wakes it up periodically using the WDT interrupt.
Code
#include <avr/sleep.h>
#include <avr/wdt.h>
volatile bool wdtWakeup = false;
ISR(WDT_vect) {
// WDT interrupt triggered, wake up MCU
wdtWakeup = true;
}
void setup() {
Serial.begin(115200);
Serial.println("WDT Interrupt Wakeup Example");
// Setup WDT interrupt mode with ~1 second timeout
cli(); // Disable interrupts
wdt_reset();
// Enable change of WDT settings
WDTCSR |= (1 << WDCE) | (1 << WDE);
// Set WDT interrupt mode, timeout ~1s
WDTCSR = (1 << WDIE) | (1 << WDP2) | (1 << WDP1);
sei(); // Enable interrupts
}
void loop() {
Serial.println("Going to sleep...");
delay(100); // Give time to print before sleep
wdtWakeup = false;
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu(); // Go to sleep here
sleep_disable();
if (wdtWakeup) {
Serial.println("Woke up by WDT!");
}
}