Skip to content

Low Power Sketch

Low power sketch

#include <Wire.h>
#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(MAG_INT, OUTPUT);
  pinMode(GPS_ENABLE, OUTPUT);
  pinMode(SARA_ENABLE, OUTPUT);
  pinMode(SARA_TX_ENABLE, OUTPUT);

  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, LOW); // low=powered-off, high=powered-on
  digitalWrite(SARA_TX_ENABLE, LOW); // 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
  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: When your board is in sleep, or does not react anymore. You can get it back to life to double press the reset button.
*/