Simple Watchdog Timer on Attiny85 not working correctly !

Hello,
I try to make a very simple example to try the watchdog timer and after to fit in my project.
I have make a very simple sketch with 2 Leds; it verify the pin input n°3, if this pin is LOW-level, the program entered in infinite loop and the watchdog must be enter in function.
The problem coming after the reset of the watchdog timer; the attiny make reset continually. >:(
Why the not let running the sketch normaly after the watchdog reset ! What's the problem ?

There is my sketch example:

#include <avr/wdt.h>

void setup() 
{
  wdt_reset();                            // Reset WDog

  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  digitalWrite(0, HIGH);
  digitalWrite(1, HIGH);
  delay(2000);
  digitalWrite(0, LOW);
  digitalWrite(1, LOW);
  delay(1000);

  wdt_enable(WDTO_8S);            // Set WDog

}

void loop() 
{
  wdt_reset();                            // Reset WDog
    
  if(digitalRead(3) == 0)
  {
    while(1){}
  }
  
  digitalWrite(0, HIGH);
  delay(500);
  digitalWrite(0, LOW);
  digitalWrite(1, HIGH);
  delay(500);
  digitalWrite(1, LOW);

}

I know there is a problem with the (old) bootloader on Nano's which causes a reset loop after watchdog reset because the watchdog flag is not reset. Your issue sounds like the same issue, so either try to burn a a new bootloader and see if it helps or set the watchdog to trigger an interrupt handler from where you disable the watchdog and then reset the attiny85 manually.

Thanks for your reply :slight_smile:

I have try to reflash with a new bootloader but without result :frowning:

I will try the second solution (with interrupt) later.
I think it is necessary to change the fuses for this solution ?

I have try your second solution.

It working with this sketch:

#include <avr/wdt.h>  // Include the avr-libc Watchdog timer handling library

void setup() 
{
  digitalWrite(2, HIGH);
  pinMode(2, OUTPUT);
  
  SetWatchDog();
  //Enable interrupts
  sei();                                   //interrupts();
  
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  digitalWrite(0, HIGH);
  digitalWrite(1, HIGH);
  delay(2000);
  digitalWrite(0, LOW);
  digitalWrite(1, LOW);
  delay(1000);
}

void loop() 
{
  wdt_reset();                            // reset WDog to parameters
    
  if(digitalRead(3) == 0)
  {
    while(1){}
  }
  digitalWrite(0, HIGH);
  delay(500);
  digitalWrite(0, LOW);
  digitalWrite(1, HIGH);
  delay(500);
  digitalWrite(1, LOW);
}

void SetWatchDog()
{
  MCUSR = 0;
  WDTCR = bit ( WDCE ) | bit ( WDE ) | bit ( WDIF ); // allow changes, disable reset, clear existing interrupt
  WDTCR = bit ( WDIE ) | bit ( WDP3 )| bit ( WDP0 ); // set WDIE ( Interrupt only, no Reset ) and 8s TimeOut
  wdt_reset ();                            // reset WDog to parameters
} // end of SetWatchDog ()

ISR (WDT_vect)
{
  cli();                                    // disable interrupts
  wdt_disable ();                           // disable WDT until next time....
  wdt_reset();
     
  digitalWrite(2, LOW);                     //reset attiny via pin2
} // end of ISR

Do you have an external pull-up on Pin 3? If not, it would be good to set pinMode(3, INPUT_PULLUP);

I have a small WDT example for the UNO that might work for you. It just blinks an LED using delay() slower and slower until it hits the WDT reset.

//  Watchdog Timer Example
#include <avr/wdt.h>


unsigned long ToggleDelay;  //  When this delay grows to longer than the WDT interval the WDT resets the Arduino


void toggle_led()
{
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}


void setup()
{
  wdt_disable();
  ToggleDelay = 10;   // Start with a very short delay
  pinMode(LED_BUILTIN, OUTPUT);
  wdt_enable(WDTO_500MS);  // Set watchdog to 1/2 second
}


void loop()
{
  wdt_reset();
  
  toggle_led();   // Blinking goes slower and slower until the WDT causes reset, then starts over.
  delay(ToggleDelay);
  ToggleDelay += 10;   // Increase the delay by 10 milliseconds.
}

How do you reset the ATtiny with pin2? In setup you define pinmode after writing to the pin which is probably not what you want..

Yes, I reset the attiny85 via pin2 and just a pull-up resistor on the reset pin. :slight_smile:

Normaly I define pinmode first but in this case that not working, the sketch don't start, he reset continiously. :neutral_face:
But, when I write to the pin first that work correctly. :confused:

Thanks johnwasser with your solution.
I have try and work fine. :slight_smile:
I spare a resistor, but I don't need to put in "output" pinmode() again, before the change of the state on this pin.