2

I want to run an Arduino autonomously - but although I apply a watchdog sometimes the systems hangs and can only be restarted by physically interrupting the power-supply (or hitting reset). So I need an external source to do that either after x hrs or based on a condition.

Instead of using another Arduino with a MOSFET or relay I am asking myself if this couldn't be done smarter with a pure electronic component like a timer or a capacitor. I could set a pin of the Arduino on high while it is active and let this charge a capacitor or something similar - if the time/energy exceeds a certain amount (because it hangs) this would connect the reset-pin of the Arduino to ground.

Any idea how this could be achieved and what component should be used?

  • 1
    You can buy specific watchdog chips that do this job for you. You could also probably craft something with a 555 timer which you start timing with a pulse from the MCU and each pulse resets its timing. – Majenko Jan 12 '15 at 23:37
  • 1
    -1 for not proofreading at all – Adam Haun Jan 13 '15 at 0:13
  • 1
    Best would be to fix the problem that's making the Arduino hang in the first place. Since you didn't post any code nor schematics, I can only guess that this may be a hidden bug in the code. In normal conditions, ATmegas and Arduinos can run for a long time without hanging. I have a ATmega328P based digital clock running for several months without resetting it. – Ricardo Jan 13 '15 at 16:00
  • Sorry for not posting the code - i have it on github link. Besides check on memory consumption etc. what makes me believe it's not a code issue is the fact that i have several of these (identical) devices and the error never seems to show a similar pattern, not in timing and not in shape... But of course i can be wrong, am definitely not an experienced programmer. – Stephan Noller Jan 25 '15 at 22:55
4

The following 555 circuit will produce a 170 ms low-going pulse every 24.2 hours. You can adjust the values of the components using this calculator if you want different timing parameters.

enter image description here

Since the reset line of a microcontroller is configured for open-drain (meaning you can connect several inputs to it, which is a good thing since we are taking advantage of that), we need to drive it with a open-drain output. That is what the buffer on the bottom of the circuit is for, to convert the push-pull output of the 555 to open-drain. A typical buffer is one section of a 7407.

| improve this answer | |
  • Awesome - really works well and exactly what i was looking for! BUT: would like to try another version where the 555 is used rather as a replacement for the "if anything goes wrong reset" watchdog, meaning: if program execution takes longer than 60sec reset (and while sleeping do nothing). My first attempt failed, tried to power the 555 circuit by a Pin of the Arduino and simply switch it off when sleeping. Any idea how this could be implemented as well? – Stephan Noller Jan 25 '15 at 14:09
  • @StephanNoller Good to hear that worked for you. Re your new requirement, I would ask that as a new question. – tcrosley Jan 25 '15 at 14:37
  • Followed your advice - would be so grateful if you could have a look at this 555 question as well... – Stephan Noller Jan 25 '15 at 22:56
4

Here is the circuit I use which depends on a heartbeat from the arduino and resets the arduino if 8 pulses are missed and also enables the arduino code to know that an external watchdog reset occured enter image description here

and the Arduino code which can be incorporated into your sketch

//Watchdog
#define ResetDetect 8       // watchdog detect pin, HIGH if a watchdog reset has occured
#define heartbeat 9         // heartbeat pin
int pulseState = LOW;       // pulseState used to set the heartbeat pin
long lastbeat = 0;          // will store last time the heartbeat pin was updated
long HeartBeatFreq = 500;   // interval at which to blink (milliseconds)
boolean ResetHappened;      // Set to true if a watchdog reset has occurred

void setup(){
  pinMode(ResetDetect, INPUT);    // set Watchdog Reset sensing pin as input
  digitalWrite(ResetDetect, HIGH);// and turn on pullup  
  pinMode(heartbeat, OUTPUT);     // set the heartbeat pin as output:

    // Check if Restarting after Watchdog Reset
    // NB must come before heartbeat resets external counter
    int ResetSet = digitalRead(ResetDetect);
    if (ResetSet == HIGH){
      ResetHappened = true;
    }
    else {
      ResetHappened = false;
    }
}

void loop(){

  // Heartbeat resets external watchdog when pin goes high
  if ((long)( millis() - (lastbeat + HeartBeatFreq)) >= 0) {    
    lastbeat = millis();    
    // if the LED is off turn it on and vice-versa:
    if (pulseState == LOW)
      pulseState = HIGH;
    else
      pulseState = LOW;
    // set the LED with the ledState of the variable:
    digitalWrite(heartbeat, pulseState);
  }
}

If there are long running sections of code the 'heartbeat' could be put into a function and that function called in the appropriate places to avoid unintended timeouts/resets

| improve this answer | |
1

Why not use the in-ship watchdog timer ?

I found in the Atmel ATmega328 (used for example in the official Arduino Uno board) official datasheet some references :

http://www.atmel.com/Images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet.pdf

We can find on page #5 two blocks named "Watchdog Timer" and "Watchdog Oscillator" on the upper left corner. No need of additional circuits, soldering or any connections.

These blocks are bad documented on the Arduino official website but usable (I did it).

Some docs with code examples :

Regards

| improve this answer | |
  • I am indeed using these functions - but unfortunately - after working like intended for days - the device sometimes hangs nevertheless. That's why i am searching for a more safe fallback-solution now. – Stephan Noller Jan 25 '15 at 22:58
  • @StephanNoller What is your Arduino board type ? Mine is legacy Arduino Uno and I've not found any problem. If you watch the sysmagazine link, the author give some explanation about a watchdog specific program hang ...: sysmagazine.com/posts/189744 – tdaget Jan 27 '15 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.