A previous article in the series introduced the concept of the Arduino Watchdog Timer and its value in developing more reliable embedded systems. When developing systems based on the Sleepy Pi platform, we make extensive use of an Arduino library to put the Arduino to sleep. In some cases, this library uses the watchdog internally for timing. So the question is:
- Can we still use the watchdog for our own code?
- When could there be conflicts?
This article looks at the implications and nuances of using sleep mode on the watchdog.
TLDR – 95% of the time there is no conflict.
Sleepy Pi Sleep
Before looking at the Watchdog and Sleep Modes it’s worth reviewing how we sleep and wake. The heart of any Sleepy Pi system is the concept of putting both itself and any attached Raspberry Pi into low power modes. For the Sleepy Pi and its Arduino processor, this involves “Sleep” modes. For the Raspberry Pi a “zero power” mode aka power off. – there are no RPi Sleep modes.
The Sleepy Pi Arduino code makes use of the Low Power library to put the processor to sleep. This article on low power modes gives a deeper dive into the various modes, but for the purposes of this article we’ll broadly consider the 3 modes:
- Sleep Forever
- Periodic and Alarm Sleep using Real Time Clock (RTC) facilities
- Timed Sleep using the WDT
Sleep Forever
This is the mode where the Arduino pricks its finger on a spinning wheel and falls into a deep, deep sleep, for example:
SleepyPi.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
If you don’t have a handy Handsome Prince, you can awaken the Arduino with a transition on an interrupt pin – for example a button.
In Sleep Forever mode the watchdog is NOT used by the sleep code and you are free to do with it as you please – there are no conflicts.
Periodic and Alarm Sleep Using RTC facilities
This is very similar in concept to Sleep Forever because we DO still Sleep Forever except we actually don’t actually mean Forever, we mean until our Alarm goes off. There is a further mode that uses a timer.
We commonly do this with either a button or use the Sleepy Pi’s onboard Real Time Clock (RTC). This can be set up to trigger an alarm at some future time and when it goes off interrupts the Arduino via the Alarm line.
In Sleep Forever with Periodic and Alarm modes, the watchdog is NOT used by the sleep code and you are free to do with it as you please – there are no conflicts.
WDT Timed Sleep
This is where we put the Arduino for a repeating periodic interval, such as:
#include "SleepyPi2.h"
#include <TimeLib.h>
#include <LowPower.h>
#include <Wire.h>
const int LED_PIN = 13;
void setup() {
// Configure "Standard" LED pin
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN,LOW); // Switch off LED
// initialize serial communication: In Arduino IDE use "Serial Monitor"
Serial.begin(9600);
}
void loop() {
// Enter power down state for 8 s with the rest of peripherals turned off
SleepyPi.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
// Do something here
// Example: Read sensor, data logging, data transmission.
Serial.println("I've Just woken up");
digitalWrite(LED_PIN,HIGH); // Switch on LED
delay(250);
digitalWrite(LED_PIN,LOW); // Switch off LED
}
If you use one of the timed sleep modes, they ALL use the watchdog.
This doesn’t mean we can’t use it for our own nefarious purposes, it just means we need to be aware. Fortunately for us, the use of the watchdog by the Low Power lab is solely confined to the Sleep period. The Library will enable the watchdog as the Arduino goes to sleep and will disable it again on wake.
What this means for us is that however we were using the watchdog and the configuration we had will be lost when we sleep. Thus we should re-enable and reconfigure the watchdog again immediately on wake from sleep.
Pingback: Reliable Embedded Systems: Using The Arduino Watchdog With Crash Tracking - Spell Foundry