Skip to content

Low Power Sketch

This sketch will put the board and components off or into sleep.

Libraries

Use the following sketch to put the SODAQ SARA into sleepmode. The board will wake every 8 seconds to do a watchdog reset. For your understanding you can enable the LED to flash every 8 seconds in the loop()

For this sketch two libraries are required. Sodaq_wdt and Sodaq_LSM303AGR

Information about how to install can be found on the getting started page.

Code

#include <Sodaq_wdt.h>
#include <Sodaq_LSM303AGR.h>

Sodaq_LSM303AGR accelerometer;

void setup(){

  // Bootup delay to program the board.
  delay(5000);

  Wire.begin();// I2C for the accelerometer

  // Disable the LSM303AGR
  accelerometer.disableAccelerometer();
  accelerometer.disableMagnetometer();

  pinMode(LED_BUILTIN,  OUTPUT);
  pinMode(MAG_INT,  OUTPUT);
  pinMode(GPS_ENABLE,  OUTPUT);
  pinMode(SARA_ENABLE,  OUTPUT);

  digitalWrite(LED_BUILTIN, LOW);   // LED low=off, high=on
  digitalWrite(MAG_INT, LOW);       // we need to make this low otherwise this pin on the LSM303AGR starts leaking current
  digitalWrite(GPS_ENABLE, LOW);    // low=powered-off, high=powered-on
  digitalWrite(SARA_ENABLE, HIGH);  // low=powered-off, high=powered-on

  //this code is needed to setup watchdog timer and to make MCU sleep
  sodaq_wdt_enable(WDT_PERIOD_8X);    // watchdog expires in ~8 seconds
  sodaq_wdt_reset();                  // resetting the watchdog

  initSleep();

  SerialUSB.flush();
  SerialUSB.end();
  USBDevice.detach();
  USB->DEVICE.CTRLA.reg &= ~USB_CTRLA_ENABLE; // Disable USB
}

void loop() {
  sodaq_wdt_reset(); // reset the watchdog
  //digitalWrite(LED_BUILTIN, HIGH);
  //sodaq_wdt_safe_delay(10);
  //digitalWrite(LED_BUILTIN, LOW);

  systemSleep();
}

/**
 * Initializes the CPU sleep mode.
 */
void initSleep()
{
    // Set the sleep mode
    SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
}

/**
  Powers down all devices and puts the system to deep sleep.
*/
void systemSleep()
{
  // Disable systick interrupt
  SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;

  __WFI(); // SAMD sleep

  // Enable systick interrupt
  SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;

}

Note

The board will immediately go into sleep mode, to reprogram you must double press the reset button, the LED will fade blue.

Sleep current

The power consumption in sleep will be ~26uA.