5

Suppose I have a typical setup/loop style sketch, such as this one which turns on an LED when a button is pressed.

How do I modify this so that it enters low power mode, awakes on the button press, and puts itself back into low power mode when its logic has finished?

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  // desired: enter low power mode here
}
void loop() {
  int buttonState = digitalRead(buttonPin);
  // desired: awake from low power mode here
  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
  }
  // desired: back to low power mode here
}
  • For which arduino board? – Jot Sep 3 '18 at 6:43
  • @Jot, I'm open to input about any of them. If some are especially good for low power applications I would be interested in hearing about that. – Mark Harrison Sep 3 '18 at 18:42
  • Some are. For example a "bare bones" atmega328p. See: gammon.com.au/power Some Arduino MKR boards can be powered with Lipo battery. The ARM M0+ processor of the MKR boards can go into sleep mode, but I don't know if there is a library for that which supports all modes. I also don't know which MKR boards use very little current for the rest of the components of the board. – Jot Sep 3 '18 at 18:49
6

This sketch is almost identical to the powerDownWakeExternalInterrupt.ino sketch that comes with the LowPower Library. I'm using INPUT_PULLUP and a N.O. push button switch to trigger it to wake up and turn an LED on.

#include "LowPower.h"
const byte wakeUpPin = 2;

// Just a handler for the pin interrupt.
void wakeUp(){}

void setup(){

  // Configure wake up pin as input.
  // This will consumes few uA of current.
  pinMode(wakeUpPin, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

}

void loop(){

  // Allow wake up pin to trigger interrupt on low.
  attachInterrupt(0, wakeUp, LOW);

  // Enter power down state with ADC and BOD module disabled.
  // Wake up when wake up pin is low.
  LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);

  // Disable external pin interrupt on wake up pin.
  detachInterrupt(0); 

  // Do something here.
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);

}

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.