Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

Implementing a Watchdog Timer with Arduino

In embedded systems, ensuring the reliability and stability of the system is crucial. One way to achieve this is by using a Watchdog Timer (WDT). A WDT is a hardware timer that resets the system if it becomes unresponsive or enters an unintended state. This article will explore the importance of a Watchdog Timer and how to implement it using an Arduino. The examples provided will demonstrate how to configure and use the WDT to enhance the robustness of your Arduino projects.

Project: In this project, we will create a simple Arduino application that uses a Watchdog Timer to reset the Arduino if it becomes unresponsive. The main objective is to demonstrate how to configure the WDT and integrate it into a typical Arduino sketch. The project will include functionalities such as initializing the WDT, periodically resetting the WDT to prevent a system reset, and simulating a system hang to observe the WDT in action.

Components List:

  • Arduino Uno (1)
  • LED (1)
  • 220-ohm resistor (1)
  • Breadboard (1)
  • Jumper wires

Examples:

// Include necessary libraries
#include <avr/wdt.h>

// Pin definition
const int ledPin = 13;

void setup() {
  // Initialize the LED pin as an output
  pinMode(ledPin, OUTPUT);

  // Turn on the LED to indicate the system is running
  digitalWrite(ledPin, HIGH);

  // Initialize the Watchdog Timer with a timeout of 2 seconds
  wdt_enable(WDTO_2S);

  // Optional: Add a delay to simulate some startup process
  delay(1000);
}

void loop() {
  // Toggle the LED state
  digitalWrite(ledPin, !digitalRead(ledPin));

  // Reset the Watchdog Timer to prevent a system reset
  wdt_reset();

  // Simulate a task by adding a delay
  delay(500);

  // Uncomment the following line to simulate a system hang
  // while(1);
}

Explanation:

  • #include <avr/wdt.h>: This line includes the AVR library for the Watchdog Timer functions.
  • const int ledPin = 13;: Defines the pin connected to the LED.
  • pinMode(ledPin, OUTPUT);: Sets the LED pin as an output.
  • digitalWrite(ledPin, HIGH);: Turns on the LED to indicate the system is running.
  • wdt_enable(WDTO_2S);: Initializes the Watchdog Timer with a 2-second timeout.
  • delay(1000);: Adds a delay to simulate some startup process.
  • digitalWrite(ledPin, !digitalRead(ledPin));: Toggles the LED state.
  • wdt_reset();: Resets the Watchdog Timer to prevent a system reset.
  • delay(500);: Adds a delay to simulate a task.
  • while(1);: Uncommenting this line simulates a system hang, causing the WDT to reset the Arduino.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.