### RTC Wakeup Configuration Example Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/02-types.md Example demonstrating how to configure an RTC-based wakeup using `RTC_ALARM_WAKEUP`. This setup wakes the MCU every 5 seconds. ```cpp // Configure RTC to wake every 5 seconds LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, onRtcWakeup, CHANGE); LowPower.sleep(5000); void onRtcWakeup() { // Called when RTC alarm triggers } ``` -------------------------------- ### Arduino Low Power Library Setup with Companion Chip Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/03-configuration.md This example demonstrates how to set up the Arduino Low Power library to control a companion chip. It registers a callback function for power control and uses companionSleep/companionWakeup before and after the main MCU sleeps. ```cpp #include "ArduinoLowPower.h" #define MIPS_CONTROL_PIN 32 void companionPowerControl(bool sleep) { pinMode(MIPS_CONTROL_PIN, OUTPUT); digitalWrite(MIPS_CONTROL_PIN, sleep ? LOW : HIGH); } void setup() { LowPower.companionLowPowerCallback(companionPowerControl); } void loop() { // Enter companion sleep before main MCU sleep LowPower.companionSleep(); LowPower.sleep(5000); // Exit companion sleep after main MCU wakeup LowPower.companionWakeup(); } ``` -------------------------------- ### Minimal ArduinoLowPower Example Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/00-index.md A basic example demonstrating how to use the ArduinoLowPower library to put the board to sleep for a specified duration. Ensure the ArduinoLowPower library is installed. ```cpp #include "ArduinoLowPower.h" void setup() { pinMode(LED_BUILTIN, OUTPUT); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(100); digitalWrite(LED_BUILTIN, LOW); delay(100); LowPower.sleep(2000); // Sleep for 2 seconds } ``` -------------------------------- ### Example onOffFuncPtr Callback Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/02-types.md Example of an onOffFuncPtr callback function. This handler is called when the MCU enters or exits sleep mode to control companion devices. ```cpp void companionStateControl(bool sleeping) { if (sleeping) { // Co-processor sleep setup } else { // Co-processor wakeup } } ``` -------------------------------- ### Timed Sleep Example Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/07-quick-reference.md Demonstrates how to put the device to sleep for a specified duration using the `LowPower.sleep()` function. Note that the sleep duration is rounded down to the nearest second when milliseconds are provided. ```cpp LowPower.sleep(1500); LowPower.sleep(2000); ``` -------------------------------- ### Example voidFuncPtr Callback Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/02-types.md Example of a voidFuncPtr callback function. This handler executes in interrupt context and should avoid blocking operations. ```cpp void myWakeupCallback(void) { // Interrupt handler — avoid delay(), Serial, locks, etc. volatile int counter; counter++; } ``` -------------------------------- ### Correct ADC Threshold Configuration Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/04-errors.md This example illustrates the correct usage of ADC interrupt thresholds, particularly for the ADC_INT_BETWEEN mode. It shows how to calculate and set the lower (lo) and higher (hi) bounds to ensure the interrupt triggers as expected. ```cpp int value = analogRead(A0); // Reads 512 int margin = 50; uint16_t lo = max(value - margin, 0); // 462 uint16_t hi = min(value + margin, 4095); // 562 // ADC_INT_BETWEEN triggers when value is 462–562 LowPower.attachAdcInterrupt(A0, callback, ADC_INT_BETWEEN, lo, hi); ``` -------------------------------- ### Basic Sleep Usage Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/00-index.md Include the ArduinoLowPower.h header and use the global 'LowPower' instance to put the microcontroller to sleep for a specified duration. This example demonstrates sleeping for 1000 milliseconds. ```cpp #include "ArduinoLowPower.h" void setup() { LowPower.sleep(1000); } ``` -------------------------------- ### Manage ADC Interrupt Callbacks Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/04-errors.md This example shows the correct way to use attachAdcInterrupt and detachAdcInterrupt. It highlights the problem of overwriting previous callbacks and losing configurations if detachAdcInterrupt is not used between calls. ```cpp LowPower.attachAdcInterrupt(A0, callback1, ADC_INT_OUTSIDE, 100, 200); LowPower.sleep(); // After wakeup, if you call attachAdcInterrupt again without detach: LowPower.attachAdcInterrupt(A0, callback2, ADC_INT_BETWEEN, 50, 150); // callback1 is now inaccessible and lost ``` ```cpp LowPower.attachAdcInterrupt(A0, myCallback, ADC_INT_OUTSIDE, 100, 200); LowPower.sleep(); LowPower.detachAdcInterrupt(); // Clean up ``` -------------------------------- ### Event-Driven Wakeup with ArduinoLowPower Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/00-index.md Example of using the ArduinoLowPower library to sleep indefinitely until a change on a specific pin triggers a wakeup. This requires attaching an interrupt to a digital pin. ```cpp #include "ArduinoLowPower.h" void setup() { pinMode(8, INPUT_PULLUP); LowPower.attachInterruptWakeup(8, onWakeup, CHANGE); } void loop() { LowPower.sleep(); // Sleep indefinitely until pin 8 changes } void onWakeup() { // Called when pin 8 transitions } ``` -------------------------------- ### Arduino attachInterruptWakeup() Example Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Configures an external pin or internal source (like RTC) to wake the MCU from sleep. Enables the External Interrupt Controller (EIC) clock and configures GCLK6 if a regular GPIO pin is specified. The callback function executes in interrupt context. ```cpp void attachInterruptWakeup(uint32_t pin, voidFuncPtr callback, irq_mode mode); ``` ```cpp volatile int wakeupCount = 0; void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(8, INPUT_PULLUP); LowPower.attachInterruptWakeup(8, onExternalWakeup, CHANGE); } void loop() { for (int i = 0; i < wakeupCount; i++) { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); } LowPower.sleep(); } void onExternalWakeup() { wakeupCount++; } ``` -------------------------------- ### Temperature-Triggered Alert using ADC Interrupt Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/06-examples.md This example demonstrates how to use ADC window mode to trigger an interrupt when the temperature sensor reading falls outside a defined safe range. It configures the ADC to wake the MCU from sleep if the temperature deviates, ideal for environmental monitoring where power efficiency is critical. ```cpp #include "ArduinoLowPower.h" const int TEMP_SENSOR = A1; volatile bool tempAlertTriggered = false; void setup() { Serial.begin(9600); pinMode(LED_BUILTIN, OUTPUT); pinMode(TEMP_SENSOR, INPUT); } void loop() { // Read current temperature int tempRaw = analogRead(TEMP_SENSOR); float tempC = (tempRaw / 1023.0) * 100.0; // 0-100°C scale Serial.print("Current temp: "); Serial.println(tempC); // Define safe range: 20-30°C // Assuming 10-bit ADC: 1023/100°C = ~102 units per °C uint16_t lo = (uint16_t)(20 * 102); // ~204 uint16_t hi = (uint16_t)(30 * 102); // ~306 // Configure ADC to alert if temperature leaves safe range LowPower.attachAdcInterrupt(TEMP_SENSOR, tempAlert, ADC_INT_OUTSIDE, lo, hi); // Sleep for up to 5 minutes (300000 ms) // Wakes if temperature goes out of bounds LowPower.sleep(300000); LowPower.detachAdcInterrupt(); if (tempAlertTriggered) { // Handle out-of-range temperature Serial.println("TEMPERATURE ALERT!"); digitalWrite(LED_BUILTIN, HIGH); delay(1000); digitalWrite(LED_BUILTIN, LOW); tempAlertTriggered = false; } } void tempAlert() { tempAlertTriggered = true; } ``` -------------------------------- ### ADC Battery Low Alarm Example Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/06-examples.md Configures an ADC interrupt to trigger when battery voltage drops below a specified threshold. This is useful for implementing low-power warnings or shutdown procedures. ```cpp // Trigger when battery voltage drops below 3.0V uint16_t lo = 0; uint16_t hi = 600; // ~3.0V at 12-bit resolution LowPower.attachAdcInterrupt(A_BATT, batteryLow, ADC_INT_BELOW_MAX, lo, hi); ``` -------------------------------- ### ArduinoLowPower Initialization Checklist Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/07-quick-reference.md Follow these steps to initialize the ArduinoLowPower library and configure sleep modes. Ensure pins are configured and interrupts are attached before the first sleep. ```cpp #include "ArduinoLowPower.h" void setup() { // 1. Configure pins if using GPIO wakeup pinMode(WAKEUP_PIN, INPUT_PULLUP); // 2. Attach interrupt before first sleep LowPower.attachInterruptWakeup(WAKEUP_PIN, callback, CHANGE); // 3. For companion chips, register callback // LowPower.companionLowPowerCallback(companionControl); // 4. Optional: Pre-configure RTC // LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, rtcCallback, CHANGE); } void loop() { // 5. Use sleep modes as needed LowPower.sleep(2000); // 6. If using ADC, attach/detach around sleep // LowPower.attachAdcInterrupt(...); // LowPower.sleep(); // LowPower.detachAdcInterrupt(); } ``` -------------------------------- ### Declare Global Instance Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/03-configuration.md The Arduino Low Power library provides a global instance named 'LowPower' for immediate use. Include the header file to access it. ```cpp extern ArduinoLowPowerClass LowPower; ``` -------------------------------- ### GPIO External Wakeup with Blink Counter (ExternalWakeup) Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/06-examples.md Demonstrates event-driven wakeup on GPIO pin state change without a pre-defined timeout. The LED blinks a number of times based on the wakeup count. ```cpp #include "ArduinoLowPower.h" volatile int repetitions = 1; const int pin = 8; void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(pin, INPUT_PULLUP); // Attach wakeup to pin 8, trigger on any edge (HIGH→LOW or LOW→HIGH) LowPower.attachInterruptWakeup(pin, repetitionsIncrease, CHANGE); } void loop() { // Blink LED a number of times based on wakeup count for (int i = 0; i < repetitions; i++) { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); } // Sleep indefinitely until pin 8 changes LowPower.sleep(); } void repetitionsIncrease() { // Called in interrupt context when pin 8 transitions repetitions++; } ``` -------------------------------- ### enableWakeupFrom() Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Configures a peripheral as a wakeup source on NRF52 boards. This function allows specifying the wakeup source, pin, event, and option for NRF52 architecture. ```APIDOC ## enableWakeupFrom() ### Description Configures a peripheral as a wakeup source on NRF52 boards. ### Signature ```cpp void enableWakeupFrom(wakeup_reason peripheral, uint32_t pin = 0xFF, uint32_t event = 0xFF, uint32_t option = 0xFF); ``` ### Parameters #### Parameters | Parameter | Type | Required | Default | Description | |-----------|------|----------|---------|-------------| | peripheral | wakeup_reason | Yes | — | Wakeup source type: `GPIO_WAKEUP`, `NFC_WAKEUP`, `ANALOG_COMPARATOR_WAKEUP`, or `OTHER_WAKEUP` | | pin | uint32_t | No | 0xFF | Pin number for GPIO wakeup | | event | uint32_t | No | 0xFF | Event identifier (peripheral-specific) | | option | uint32_t | No | 0xFF | Additional option flags (peripheral-specific) | ### Return `void` ### Note This method is defined in the header but implementation details are not provided in the source code reviewed. ``` -------------------------------- ### Include ArduinoLowPower Library Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/docs/readme.md Include the ArduinoLowPower library in your sketch to utilize its low power functionalities. ```cpp #include ``` -------------------------------- ### Include Header and Use Global Instance Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/03-configuration.md Include the 'ArduinoLowPower.h' header to make the global 'LowPower' object available. This allows direct calls to its methods like 'sleep()'. ```cpp #include "ArduinoLowPower.h" void setup() { // LowPower object is immediately available LowPower.sleep(1000); } ``` -------------------------------- ### Timed Sleep with Blink (TimedWakeup) Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/06-examples.md Demonstrates periodic RTC-based wakeup with a visual LED indicator. Use for periodic monitoring tasks. ```cpp #include "ArduinoLowPower.h" void setup() { pinMode(LED_BUILTIN, OUTPUT); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); // Sleep for 2 seconds, then automatically wake via RTC LowPower.sleep(2000); } ``` -------------------------------- ### NRF52 Methods Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.txt Specific methods for NRF52-based boards to manage peripheral wakeups and query wakeup reasons. ```APIDOC ## NRF52 Methods ### Description NRF52-specific functions for controlling and querying wakeup sources. ### Methods - `enableWakeupFrom(peripheral)` - `wakeupReason()` ### Parameters - `peripheral` - The peripheral to enable wakeup from. ``` -------------------------------- ### Basic Idle Mode Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Use this snippet to put the MCU into a basic idle mode where the CPU is halted but other systems remain active. This offers minimal power reduction with the fastest wake-up time. ```cpp #include "ArduinoLowPower.h" void setup() { pinMode(LED_BUILTIN, OUTPUT); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(100); digitalWrite(LED_BUILTIN, LOW); delay(100); LowPower.idle(); } ``` -------------------------------- ### Enable Wakeup From Peripheral (NRF52) Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Configures a peripheral as a wakeup source on NRF52 boards. Use this to set up GPIO, NFC, or analog comparator wakeups. ```cpp void enableWakeupFrom(wakeup_reason peripheral, uint32_t pin = 0xFF, uint32_t event = 0xFF, uint32_t option = 0xFF); ``` -------------------------------- ### companionLowPowerCallback() Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Registers a callback function to be invoked before the main MCU enters sleep mode. This allows a companion co-processor to enter a corresponding low-power state. ```APIDOC ## companionLowPowerCallback(onOffFuncPtr callback) ### Description Registers a callback function to be invoked before the main MCU enters sleep mode, allowing the companion co-processor to enter a corresponding low-power state. ### Signature ```cpp void companionLowPowerCallback(onOffFuncPtr callback); ``` ### Parameters - **callback** (onOffFuncPtr) - Required - Function pointer invoked with boolean argument. Signature: `void callback(bool sleeping)`. Receives `true` before sleep, `false` on wakeup. ### Return `void` ### Example ```cpp #include "ArduinoLowPower.h" #define MIPS_PIN 32 void companionControl(bool sleep) { pinMode(MIPS_PIN, OUTPUT); digitalWrite(MIPS_PIN, sleep ? LOW : HIGH); } void setup() { pinMode(LED_BUILTIN, OUTPUT); LowPower.companionLowPowerCallback(companionControl); LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, onWakeup, CHANGE); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); LowPower.companionSleep(); LowPower.sleep(20000); } void onWakeup() { LowPower.companionWakeup(); } ``` ``` -------------------------------- ### Wakeup Configuration Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.txt Configures various mechanisms to wake the microcontroller from a low-power state, including GPIO interrupts, RTC alarms, and ADC monitoring. ```APIDOC ## Wakeup Configuration ### Description Methods for setting up wakeup sources from sleep modes. ### Methods - `attachInterruptWakeup(pin, mode)` - `attachAdcInterrupt(adc_channel, threshold)` (SAMD only) - `detachAdcInterrupt()` (SAMD only) ### Parameters - `pin` - The GPIO pin to monitor for wakeup. - `mode` - The interrupt mode (e.g., CHANGE, RISING, FALLING). - `adc_channel` (SAMD only) - The ADC channel to monitor. - `threshold` (SAMD only) - The ADC value threshold for wakeup. ``` -------------------------------- ### NRF52 Specific Methods Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/07-quick-reference.md Provides methods specific to the NRF52 microcontroller family for enabling wakeups from peripherals and querying the reason for wakeup. ```APIDOC ## NRF52 Methods ### `LowPower.enableWakeupFrom(peripheral, pin, event, option)` **Description**: Enables wakeup from a specific peripheral on the NRF52 microcontroller. **Parameters**: - `peripheral` (int) - The peripheral to enable wakeup from. - `pin` (int) - The associated GPIO pin. - `event` (int) - The event that triggers the wakeup. - `option` (int) - Additional options for the wakeup configuration. ### `LowPower.wakeupReason()` **Description**: Returns the reason why the microcontroller woke up. **Returns**: - `wakeup_reason` - An enum or value indicating the wakeup source. ``` -------------------------------- ### Power Management Methods Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/docs/api.md Methods for controlling the power state of the MCU. ```APIDOC ## LowPower.idle() ### Description Puts the MCU in IDLE mode for power optimization with fast wake-up. The CPU is stopped. ### Parameters - **milliseconds** (int) - Optional - The number of milliseconds to stay in idle mode. If omitted, it stays until a wakeup event. ## LowPower.sleep() ### Description Puts the MCU in sleep mode for power optimization with slower wake-up. Only chosen peripherals remain active. ### Parameters - **milliseconds** (int) - Optional - The number of milliseconds to stay in sleep mode. If omitted, it stays until a wakeup event. ## LowPower.deepSleep() ### Description Puts the MCU in deep sleep mode for maximum power optimization. All but RTC peripherals are stopped. ### Parameters - **milliseconds** (int) - Optional - The number of milliseconds to stay in deep sleep mode. If omitted, it stays until a wakeup event. ``` -------------------------------- ### Set Companion Low Power Callback Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/docs/api.md Defines the callback function for the on-board co-processor (Tian only) to execute before entering sleep. ```cpp LowPower.CompanionLowPowerCallback(callback); ``` -------------------------------- ### sleep() - No arguments Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Puts the MCU into a sleep mode with moderate power reduction. USB is placed in standby and the SysTick interrupt is disabled during sleep. Wake-up latency is moderate. ```APIDOC ## sleep() ### Description Puts the MCU in SLEEP mode with moderate power reduction. USB is placed in standby (if available) and the SysTick interrupt is disabled during sleep to prevent spurious wakeups. Wake-up latency is moderate. ### Method void ### Parameters None ### Behavior: - Invokes `USBDevice.standby()` if a virtual USB port is available; otherwise detaches USB and restores it on wakeup - Disables SysTick interrupt before entering sleep and re-enables it after wakeup - Sets `SCB->SCR.SLEEPDEEP` bit and invokes `__WFI()` ### Example: ```cpp #include "ArduinoLowPower.h" void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(8, INPUT_PULLUP); LowPower.attachInterruptWakeup(8, onWakeup, CHANGE); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); LowPower.sleep(); // Sleep indefinitely until wakeup event } void onWakeup() { // Called when pin 8 triggers } ``` ``` -------------------------------- ### Companion Chip Support Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.txt Functions to coordinate low-power states and callbacks with a companion co-processor chip. ```APIDOC ## Companion Chip Support ### Description APIs for managing low-power states in conjunction with a companion chip. ### Methods - `companionLowPowerCallback()` - `companionSleep()` - `companionWakeup()` ``` -------------------------------- ### ADC Window Monitoring with Wakeup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/06-examples.md Demonstrates autonomous analog threshold monitoring. The MCU wakes when an analog input voltage drifts outside a configured window. This is useful for detecting changes in sensor readings or potentiometer positions. ```cpp #include "ArduinoLowPower.h" volatile int repetitions = 1; const int pin = A0; const int margin = 10; void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(pin, INPUT); } void loop() { // Blink LED repetitions times for (int i = 0; i < repetitions; i++) { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); } // Read current analog value int value = analogRead(pin); // Returns 0–1023 // Define window around that value uint16_t lo = max(value - margin, 0); uint16_t hi = min(value + margin, UINT16_MAX); // Attach ADC window interrupt // This reconfigures the ADC, so must be called before sleep LowPower.attachAdcInterrupt(pin, repetitionsIncrease, ADC_INT_OUTSIDE, lo, hi); // Sleep indefinitely; MCU wakes when analog value leaves [lo, hi] LowPower.sleep(); // Detach to restore ADC for normal use // This must be called immediately after sleep LowPower.detachAdcInterrupt(); } void repetitionsIncrease() { // Called when ADC value falls outside the threshold window repetitions++; } ``` -------------------------------- ### Companion Sleep/Wakeup Without Callback Registration Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/04-errors.md Calling companionSleep() or companionWakeup() without first registering a callback via companionLowPowerCallback() results in no action. Ensure a valid function pointer is provided. ```cpp // Example of companionSleep() usage (requires prior callback registration) // companionSleep(); ``` -------------------------------- ### Automatic RTC Initialization in setAlarmIn Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/04-errors.md This code demonstrates that the library automatically initializes the RTCZero when setAlarmIn is called before explicit RTC configuration. This ensures the RTC is ready for timed sleep operations. ```cpp void ArduinoLowPowerClass::setAlarmIn(uint32_t millis) { if (!rtc.isConfigured()) { attachInterruptWakeup(RTC_ALARM_WAKEUP, NULL, (irq_mode)0); } // RTC now guaranteed to be initialized } ``` -------------------------------- ### Arduino Low Power NRF52 Wakeup Methods Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/07-quick-reference.md Specific methods for NRF52 microcontrollers to enable wakeups from peripherals and query the reason for waking up. ```cpp LowPower.enableWakeupFrom(peripheral, pin, event, option); ``` ```cpp wakeup_reason reason = LowPower.wakeupReason(); ``` -------------------------------- ### RTC Configuration for Timed Wakeups Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/03-configuration.md This code snippet shows how the Arduino Low Power library automatically initializes and configures the RTCZero for timed wakeups if it's not already set up. It sets an alarm to trigger after a specified duration. ```cpp void ArduinoLowPowerClass::setAlarmIn(uint32_t millis) { if (!rtc.isConfigured()) { attachInterruptWakeup(RTC_ALARM_WAKEUP, NULL, (irq_mode)0); } uint32_t now = rtc.getEpoch(); rtc.setAlarmEpoch(now + millis/1000); rtc.enableAlarm(rtc.MATCH_YYMMDDHHMMSS); } ``` -------------------------------- ### Companion Wakeup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/docs/api.md Forces the on-board co-processor (Tian only) to wake up from sleep mode. ```cpp LowPower.companionWakeup(); ``` -------------------------------- ### idle() Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Puts the MCU in IDLE mode. The CPU is halted via `__WFI()` but other systems remain active. Can be called with or without a duration. ```APIDOC ## idle() ### Description Puts the MCU in IDLE mode with minimal power reduction and fastest wake-up time. The CPU is halted via `__WFI()` but other systems remain active. ### Method POST ### Endpoint /lowpower/idle ### Parameters #### Query Parameters - **millis** (uint32_t) - Optional - Duration in milliseconds to remain in idle mode. Sets an RTC alarm to wake after this duration. ### Request Example ```json { "millis": 1000 } ``` ### Response #### Success Response (200) - **status** (string) - Indicates successful execution. #### Response Example ```json { "status": "OK" } ``` ``` -------------------------------- ### companionWakeup() Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Invokes the registered companion callback to notify the co-processor to wake up from sleep mode. This function is available only on boards with a companion chip. ```APIDOC ## companionWakeup() ### Description Invokes the registered companion callback with `false` to notify the co-processor to wake up from sleep mode. This function is available only on boards with a companion chip. ### Signature ```cpp void companionWakeup(); ``` ### Parameters None ### Return `void` ### Behavior Calls `companionSleepCB(false)` if a callback has been registered via `companionLowPowerCallback()`. ``` -------------------------------- ### Interrupt and Co-processor Methods Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/docs/api.md Methods for configuring wakeup interrupts and managing the co-processor on supported boards. ```APIDOC ## LowPower.attachInterruptWakeup() ### Description Configures the pin and condition for a wakeup event. ### Parameters - **pin** (int) - Required - The pin used as external wakeup. - **callback** (function) - Required - The function to call on wakeup. - **mode** (enum) - Required - The transition to sense (FALLING, RISING, CHANGE). ## LowPower.CompanionLowPowerCallback() ### Description Sets the function for the on-board co-processor (Tian only) to call before sleeping. ### Parameters - **callback** (function) - Required - The function to call before going to sleep. ## LowPower.companionSleep() ### Description Puts the on-board co-processor (Tian only) in sleep mode. ## LowPower.companionWakeup() ### Description Forces the on-board co-processor (Tian only) to wake up from sleep mode. ``` -------------------------------- ### Resuming Execution After Sleep Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/04-errors.md Demonstrates the point of execution resumption after a LowPower.sleep() call. The MCU state is preserved upon waking. ```cpp void loop() { // ... work before sleep LowPower.sleep(2000); // Execution resumes here after 2000ms or external wakeup // ... work after sleep } ``` -------------------------------- ### AVR Architecture Incompatibility Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/03-configuration.md This code snippet demonstrates how the library explicitly rejects AVR-based boards by raising a compilation error. Ensure your target board is not AVR-based. ```cpp #ifdef ARDUINO_ARCH_AVR #error The library is not compatible with AVR boards #endif ``` -------------------------------- ### Set MCU to Idle Mode Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/docs/api.md Puts the MCU in IDLE mode for power optimization with fast wake-up. Optionally accepts a duration in milliseconds. ```cpp LowPower.idle(); LowPower.idle(milliseconds); ``` -------------------------------- ### Attach Interrupt Wakeup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/docs/api.md Configures a pin and callback function to trigger a wake-up event based on specified signal transitions. ```cpp LowPower.attachInterruptWakeup(pin, callback, mode); ``` -------------------------------- ### Wake Companion Chip from Sleep Mode Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Call companionWakeup() to notify the companion chip to wake up from sleep mode. This function is only available on boards with a companion chip and requires a callback to be registered using companionLowPowerCallback(). ```cpp void companionWakeup(); ``` -------------------------------- ### ADC Configuration for Low-Power Operation Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/03-configuration.md The ADC is reconfigured for minimal power consumption when attachAdcInterrupt() is called. This includes setting a low-power clock source, prescaler, enabling RUNSTDBY and FREERUN, and configuring window monitoring with interrupts. ```cpp #ifdef ARDUINO_API_VERSION using irq_mode = PinStatus; // New API: enum (LOW, HIGH, CHANGE) #else using irq_mode = uint32_t; // Old API: raw register values #endif ``` -------------------------------- ### Arduino Low Power Wakeup Configuration Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/07-quick-reference.md Configure how the microcontroller wakes up from a low-power state. Supports GPIO interrupts, RTC alarms, and ADC monitoring. ```cpp // GPIO-based wakeup LowPower.attachInterruptWakeup(pin, callback, mode); // Modes: RISING, FALLING, CHANGE ``` ```cpp // RTC-based wakeup (automatic with timed sleep) LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, callback, CHANGE); ``` ```cpp // ADC window monitoring (SAMD only) LowPower.attachAdcInterrupt(pin, callback, mode, lo, hi); // Modes: ADC_INT_BETWEEN, ADC_INT_OUTSIDE, ADC_INT_ABOVE_MIN, ADC_INT_BELOW_MAX ``` ```cpp LowPower.detachAdcInterrupt(); // Call after sleep returns ``` -------------------------------- ### Register Companion Chip Low Power Callback Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Registers a callback function to manage the companion co-processor's power state before the main MCU enters sleep. The callback receives a boolean indicating whether the MCU is entering sleep. ```cpp #include "ArduinoLowPower.h" #define MIPS_PIN 32 void companionControl(bool sleep) { pinMode(MIPS_PIN, OUTPUT); digitalWrite(MIPS_PIN, sleep ? LOW : HIGH); } void setup() { pinMode(LED_BUILTIN, OUTPUT); LowPower.companionLowPowerCallback(companionControl); LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, onWakeup, CHANGE); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); LowPower.companionSleep(); LowPower.sleep(20000); } void onWakeup() { LowPower.companionWakeup(); } ``` -------------------------------- ### Coordinate Companion Chip Sleep Modes Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/06-examples.md Demonstrates coordinating low-power modes between the main SAMD MCU and a companion co-processor using a GPIO pin. The companionLowPowerCallback function is registered to control the co-processor's sleep state. ```cpp #include "ArduinoLowPower.h" #define MIPS_PIN 32 // GPIO controlling MIPS co-processor void MipsPM(bool sleep) { pinMode(MIPS_PIN, OUTPUT); digitalWrite(MIPS_PIN, sleep ? LOW : HIGH); // LOW signal tells MIPS to enter sleep // HIGH signal tells MIPS to wake } void setup() { pinMode(LED_BUILTIN, OUTPUT); // Register the companion control callback LowPower.companionLowPowerCallback(MipsPM); // Configure RTC wakeup LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, onWakeup, CHANGE); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); // Signal companion to sleep (calls MipsPM(true)) LowPower.companionSleep(); // Main MCU sleeps for 20 seconds LowPower.sleep(20000); // Note: Wakeup callback executes here, which signals companion to wake } void onWakeup() { // Called when RTC alarm fires (every 20 seconds) // Signal companion to wake (calls MipsPM(false)) LowPower.companionWakeup(); } ``` -------------------------------- ### Configure GCLK6 for Low Power Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/03-configuration.md Configures GCLK6 to use OSCULP32K for low-power operation of EIC and ADC during sleep. Prevents NVMCTRL from powering down completely due to an errata. Auto-invoked by attachInterruptWakeup() or attachAdcInterrupt(). ```cpp static void configGCLK6() { // Enable EIC on GCLK6 GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK6 | GCLK_CLKCTRL_ID(GCM_EIC); // Source GCLK6 from OSCULP32K (32 kHz) GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(6); GCLK->GENCTRL.bit.RUNSTDBY = 1; // Run during standby // Prevent Flash from powering down completely (errata) NVMCTRL->CTRLB.bit.SLEEPPRM = NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val; } ``` -------------------------------- ### wakeupReason() Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Retrieves the reason for the most recent wakeup event on NRF52 architecture boards. It returns a value from the `wakeup_reason` enum. ```APIDOC ## wakeupReason() ### Description Returns the reason for the most recent wakeup event on NRF52 boards. ### Signature ```cpp wakeup_reason wakeupReason(); ``` ### Parameters None ### Return Values - **OTHER_WAKEUP** (0): Unspecified wakeup reason. - **GPIO_WAKEUP** (1): Wakeup triggered by GPIO interrupt. - **NFC_WAKEUP** (2): Wakeup triggered by NFC event. - **ANALOG_COMPARATOR_WAKEUP** (3): Wakeup triggered by analog comparator. ``` -------------------------------- ### Basic MCU Sleep Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Use this snippet to put the MCU into a general sleep mode. It disables the SysTick interrupt and can optionally put USB in standby. The MCU will wake up on the next event. ```cpp #include "ArduinoLowPower.h" void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(8, INPUT_PULLUP); LowPower.attachInterruptWakeup(8, onWakeup, CHANGE); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); LowPower.sleep(); // Sleep indefinitely until wakeup event } void onWakeup() { // Called when pin 8 triggers } ``` -------------------------------- ### attachInterruptWakeup() Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Configures an external pin or internal source (like RTC) to wake the MCU from sleep. Enables the External Interrupt Controller (EIC) clock and configures GCLK6 if a regular GPIO pin is specified. ```APIDOC ## attachInterruptWakeup() ### Description Configures an external pin or internal source (like RTC) to wake the MCU from sleep. Enables the External Interrupt Controller (EIC) clock and configures GCLK6 if a regular GPIO pin is specified. ### Signature `void attachInterruptWakeup(uint32_t pin, voidFuncPtr callback, irq_mode mode);` ### Parameters - **pin** (uint32_t) - Required - Pin number for external wakeup, or special value `RTC_ALARM_WAKEUP` (0xFF) to configure RTC-based wakeup - **callback** (voidFuncPtr) - Required - Function pointer invoked when wakeup event occurs. Signature: `void callback(void)`. Executes in interrupt context. - **mode** (irq_mode) - Required - Interrupt trigger mode: `RISING`, `FALLING`, or `CHANGE` (or `0` for RTC_ALARM_WAKEUP) ### Return `void` ### Behavior - If `pin > PINS_COUNT`: Treated as special wakeup source. For `RTC_ALARM_WAKEUP`, initializes RTCZero and attaches callback. - If `pin` is a valid GPIO: Calls `attachInterrupt()`, configures GCLK6 for EIC, and enables wakeup capability via `EIC->WAKEUP`. - Returns silently if pin is not an interrupt-capable external interrupt. ### Example ```cpp volatile int wakeupCount = 0; void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(8, INPUT_PULLUP); LowPower.attachInterruptWakeup(8, onExternalWakeup, CHANGE); } void loop() { for (int i = 0; i < wakeupCount; i++) { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); } LowPower.sleep(); } void onExternalWakeup() { wakeupCount++; } ``` ``` -------------------------------- ### Companion Chip Control Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/07-quick-reference.md Functions for managing a companion low-power chip, allowing it to sleep or wake up independently. ```APIDOC ## Companion Chip Control ### `LowPower.companionLowPowerCallback(callback)` **Description**: Registers a callback function to control the companion chip's power state during system sleep. **Parameters**: - `callback` (void (*)(bool)) - A function that takes a boolean argument indicating whether the companion chip should sleep. ### `LowPower.companionSleep()` **Description**: Puts the companion chip into a low-power sleep state. ### `LowPower.companionWakeup()` **Description**: Wakes up the companion chip from a low-power sleep state. ``` -------------------------------- ### RTC Alarm Precision Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/07-quick-reference.md Illustrates how RTC alarms are set using epoch time. Integer division when calculating seconds from milliseconds can lead to a loss of sub-second precision. ```cpp rtc.setAlarmEpoch(now + millis/1000); ``` -------------------------------- ### companionSleep() Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Invokes the registered companion callback to notify the co-processor to enter sleep mode. This function is available only on boards with a companion chip. ```APIDOC ## companionSleep() ### Description Invokes the registered companion callback with `true` to notify the co-processor to enter sleep mode. This function is available only on boards with a companion chip. ### Signature ```cpp void companionSleep(); ``` ### Parameters None ### Return `void` ### Behavior Calls `companionSleepCB(true)` if a callback has been registered via `companionLowPowerCallback()`. ``` -------------------------------- ### Define Companion Chip Support Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/03-configuration.md This preprocessor directive defines support for a companion chip based on the board's architecture. It is used to conditionally compile code related to companion chip interaction. ```cpp #if defined(ARDUINO_SAMD_TIAN) || defined(ARDUINO_NRF52_PRIMO) #define BOARD_HAS_COMPANION_CHIP #endif ``` -------------------------------- ### Registering RTC Wakeup with Null Callback Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/04-errors.md Registering an RTC wakeup with a NULL callback is valid if no specific interrupt action is needed. The device will wake, but no callback will execute. Handle wakeup logic in the main loop. ```cpp LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, NULL, CHANGE); ``` -------------------------------- ### Hybrid Timed and Event-Driven Wakeup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/06-examples.md Combines an RTC timeout with an external GPIO interrupt. The MCU will wake either when the specified time elapses or when the sensor pin triggers an event. ```cpp #include "ArduinoLowPower.h" volatile int eventCount = 0; const int SENSOR_PIN = 8; void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(SENSOR_PIN, INPUT_PULLUP); // Register both RTC and external interrupt LowPower.attachInterruptWakeup(SENSOR_PIN, onSensorEvent, FALLING); LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, onTimeout, CHANGE); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(100); digitalWrite(LED_BUILTIN, LOW); delay(100); if (eventCount > 0) { Serial.println("Event detected!"); eventCount = 0; } // Sleep for max 10 seconds // - Wakes on sensor event (FALLING edge) before timeout // - OR wakes on 10-second RTC timeout LowPower.sleep(10000); } void onSensorEvent() { eventCount++; } void onTimeout() { // Periodic wakeup for housekeeping (data logging, status check, etc.) } ``` -------------------------------- ### deepSleep() Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Puts the MCU in DEEP SLEEP mode with maximum power reduction. All peripherals except the RTC are stopped. Wake-up latency is highest but power consumption is lowest. ```APIDOC ## deepSleep() ### Description Puts the MCU in DEEP SLEEP mode with maximum power reduction. All peripherals except the RTC are stopped. Wake-up latency is highest but power consumption is lowest. ### Overloads #### `void deepSleep(void)` ##### Parameters None ##### Return `void` ##### Example ```cpp void loop() { LowPower.deepSleep(); // Sleep indefinitely with maximum power savings } ``` #### `void deepSleep(uint32_t millis)` ##### Parameters - **millis** (uint32_t) - Required - Duration in milliseconds. ##### Return `void` ##### Example ```cpp void loop() { LowPower.deepSleep(5000); // Sleep for 5 seconds } ``` #### `void deepSleep(int millis)` ##### Parameters - **millis** (int) - Required - Duration in milliseconds. Implicitly cast to `uint32_t`. ##### Return `void` ``` -------------------------------- ### Hybrid Sleep: Timeout or Event Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/07-quick-reference.md This pattern allows the system to wake up either after a specified timeout or in response to an interrupt. It provides flexibility for scenarios where a task needs to be performed periodically but also needs to react to immediate events. ```cpp void loop() { LowPower.sleep(30000); // Wake on timeout OR interrupt } ``` -------------------------------- ### sleep(uint32_t millis) Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Puts the MCU into a sleep mode for a specified duration in milliseconds. The RTC alarm triggers the wake-up. ```APIDOC ## sleep(uint32_t millis) ### Description Sleeps for a specified duration in milliseconds, with wake-up triggered by an RTC alarm. ### Method void ### Parameters #### Path Parameters - **millis** (uint32_t) - Required - Duration in milliseconds to sleep. RTC alarm triggers wakeup. ### Example: ```cpp void loop() { LowPower.sleep(2000); // Sleep for 2 seconds } ``` ``` -------------------------------- ### GPIO-Based Wakeup Configuration in Arduino Low Power Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/05-architecture.md Configures an external interrupt on a specified pin to wake the microcontroller. This function sets up the interrupt mode (RISING, FALLING, CHANGE) and registers a callback function to be executed upon wakeup. It also configures the necessary clock and interrupt controller settings. ```cpp LowPower.attachInterruptWakeup(8, myCallback, CHANGE) ``` -------------------------------- ### onOffFuncPtr Type Definition Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/02-types.md Function pointer type for companion device callbacks that receive a boolean sleep state. Used to coordinate low-power entry/exit with on-board co-processors. ```cpp typedef void (*onOffFuncPtr)( bool ); ``` -------------------------------- ### Companion Sleep Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/docs/api.md Puts the on-board co-processor (Tian only) into sleep mode. ```cpp LowPower.companionSleep(); ``` -------------------------------- ### Wakeup Reason Enumeration Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/02-types.md Enumerates the possible sources of MCU wakeup events on NRF52 boards. Used as the return type of `wakeupReason()` and parameter type for `enableWakeupFrom()`. ```cpp typedef enum { OTHER_WAKEUP = 0, GPIO_WAKEUP = 1, NFC_WAKEUP = 2, ANALOG_COMPARATOR_WAKEUP = 3 } wakeup_reason; ``` -------------------------------- ### Arduino deepSleep() - Milliseconds Parameter Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Puts the MCU in DEEP SLEEP mode for a specified duration in milliseconds. All peripherals except the RTC are stopped. Wake-up latency is highest but power consumption is lowest. ```cpp void deepSleep(uint32_t millis); ``` ```cpp void loop() { LowPower.deepSleep(5000); // Sleep for 5 seconds } ``` -------------------------------- ### Sleep for a Specific Duration (uint32_t) Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Use this snippet to sleep for a precise duration in milliseconds. The RTC alarm will trigger the wakeup. ```cpp void loop() { LowPower.sleep(2000); // Sleep for 2 seconds } ``` -------------------------------- ### attachAdcInterrupt Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/01-api-reference.md Configures the ADC to monitor an analog input pin and wake the MCU when the voltage crosses a threshold. This function is only available on SAMD architecture. ```APIDOC ## attachAdcInterrupt() ### Description Configures the ADC to monitor an analog input pin and wake the MCU when the voltage crosses a threshold defined by a lower and upper bound. Uses ADC window mode for autonomous monitoring during sleep. ### Signature ```cpp void attachAdcInterrupt(uint32_t pin, voidFuncPtr callback, adc_interrupt mode, uint16_t lo, uint16_t hi); ``` ### Parameters #### Path Parameters - **pin** (uint32_t) - Yes - Analog input pin (e.g., A0). Must be ADC-capable. - **callback** (voidFuncPtr) - Yes - Function pointer invoked on ADC window interrupt. Signature: `void callback(void)`. Executes in interrupt context. - **mode** (adc_interrupt) - Yes - Window interrupt mode: `ADC_INT_BETWEEN` (value between lo and hi), `ADC_INT_OUTSIDE` (value outside [lo, hi]), `ADC_INT_ABOVE_MIN` (value >= lo), or `ADC_INT_BELOW_MAX` (value <= hi) - **lo** (uint16_t) - Yes - Lower threshold value (0–4095 for 12-bit ADC) - **hi** (uint16_t) - Yes - Upper threshold value (0–4095 for 12-bit ADC) ### Request Example ```cpp #include "ArduinoLowPower.h" volatile int readings = 0; const int pin = A0; const int margin = 10; void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(pin, INPUT); } void loop() { for (int i = 0; i < readings; i++) { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); } int currentValue = analogRead(pin); uint16_t lo = max(currentValue - margin, 0); uint16_t hi = min(currentValue + margin, 4095); // Setup ADC interrupt before sleep LowPower.attachAdcInterrupt(pin, onAdcWakeup, ADC_INT_OUTSIDE, lo, hi); LowPower.sleep(); // Cleanup after wakeup LowPower.detachAdcInterrupt(); } void onAdcWakeup() { readings++; } ``` ```