I have a Nano 33 BLE. I intend to keep the battery always connected due to the design. I have a tactile switch that i want to trigger a power ON and OFF state. When powered off I want the device to go into a deep sleep with all the sensors off to save power. However I want the switch to also be able to then be pushed to power on the device too.
That i believe I can use to put the device into low power mode, that I and interrupt and turn on via a switch. I'm not sure if this library also turns off all the sensors and bluetooth when in this mode? Or will i need to do this as part of the shutdown routine? I also found the following:
Is there a particular reason you need to use the BLE? I tried to compile the low power library and it isn't working for Nano BLE because it uses a mbed OS.
The library works for Nano IoT's and I have put together a quick version below that works with a timer and a button, however, you need to go into the "header file" of the low power library (ArduinoLowPower.h). and change one thing. In ArduinoLowPower.h there is a class called ArduinoLowPowerClass and you need to move the function void setAlarmIn(uint32_t millis) from private to public so your code can access it. If you don't feel comfortable doing that I have attached the adjusted header but you will need to find it in your Arduino library and rename it. Make sure you make a backup copy of the original header. ArduinoLowPower_Adjusted.h (2.2 KB)
I am not sure what happens to the BLE when you put it to sleep but you can just turn it off before it sleeps. I would imagine that once the sensors are powered and started by the Arduino they will plod at their typical draw along unless told otherwise. Some sensors have sleep modes whilst others should be coded to sleep between readings but this will be a case-by-case basis. You can also investigate using a software-driven latch switch where the Arduino literally turns the sensors on and off through a pin controlling a MOSFET that cuts the power connection.
#include "ArduinoLowPower.h"
#define DEBUG
int buttonInterrupts = 0, timerInterrupts = 0;
// Declare it volatile since it's incremented inside an interrupt
volatile bool buttonFlag = false, timerFlag = false;
// Pin used to trigger a wakeup
const int pin = 2;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(pin, INPUT_PULLUP);
LowPower.attachInterruptWakeup(pin, buttonWakeup, CHANGE); //button interrupt
LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, timerWakeup, CHANGE); //timer interupt
Serial.begin(9600);
#ifdef DEBUG
while (!Serial)
; //wait for serial connect
#endif
Serial.println("Beginning in 10 sec"); //set a large delay at the beginning so you give yourself a window to upload new code without needing to double tap reset.
delay(10000);
Serial.println("Begin!");
}
void loop() {
for (int i = 0; i < (buttonInterrupts + timerInterrupts); i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
}
LowPower.setAlarmIn(10000);
Serial.println("Alarm set");
Serial.end();
LowPower.sleep();
Serial.begin(9600);
#ifdef DEBUG
while (!Serial)
;
#endif
if (buttonFlag) {
buttonInterrupts++;
Serial.println("Button wake up");
buttonFlag = false;
} else if (timerFlag) {
timerInterrupts++;
Serial.println("Timer wake up");
timerFlag = false;
} else {
Serial.println("No reason wake up, somehow");
}
Serial.print("Timer Interrupts: "), Serial.println(timerInterrupts);
Serial.print("Button Interrupts: "), Serial.println(buttonInterrupts);
}
void buttonWakeup() {
buttonFlag = true;
}
void timerWakeup() {
timerFlag = true;
}