Build a circuit to automatically reset an Arduino when code hangs

Sometimes an Arduino project runs happily for a long time, and then just hangs, and finding the reason is hard. I find this especially so with WiFi iot projects, where the fault may be in code that I don't have access to. And because the code has hung, it can't send a help message to alert me to the problem.

To solve this problem I built a little circuit that monitors a heartbeat from the Arduino, and if it doesn't receive one after a certain time, the circuit sends a reset signal to the board.

The circuit uses a 74123 monostable chip to start a timer everytime it detects a heartbeat from the Arduino, which keeps the output high. But if the heartbeat is not detected for long enough for the timer to expire, then the output drops which causes a reset of the Arduino. The heartbeat code is simple enough, and the toggleHeartbeatLED() function is called on each iteration of your main loop.

+5V, Ground, and Reset are connected to the appropriate pins on the Arduino, and Trigger is connected to whichever pin you chose for the heartbeat (8 in my example code).

The system has been tested on the Arduino UNO R3 and UNO R4 WiFi, but should work on any model.

The values of R1 and C1 give a timeout of around 5 seconds, which is very short, but is useful for initial testing of the system. For a longer timeout, increase the values of R1 or C1 to taste. The timeout should be at least as long as the startup time of the arduino.
Other component values were chosen mostly for what was lying around on my bench, so you can probably vary these a lot, but I know these values work. I'm not an electronic engineer, so I'm sure there are more elegant solutions, but again, this works, so that that's good enough for me.

If performing a reset is not enough to solve your hanging problems, then the output can be used to trigger a relay that cycles the power to the arduino.

===========================================================
boolean ledHeartBeatToggle = 1;

const int LEDpin = 8; 
pinMode(LEDpin, OUTPUT);

void toggleHeartbeatLED() { // Toggle led as heartbeat

  if (ledHeartbeatToggle) {
    digitalWrite(LEDpin, HIGH);
    ledHeartbeatToggle = false;
  } else {
    digitalWrite(LEDpin, LOW);
    ledHeartbeatToggle = true;
  }
}
===========================================================

Mark Harris Feb 2024

(MRH Home Page)

Visits :