### Voltage Divider Hardware Setup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/examples.md Example circuit configuration for connecting a voltage divider to the ADC pin. ```text VCC | R1 (10kΩ) | +---> A0 | R2 (10kΩ) | GND ``` -------------------------------- ### Attach ADC interrupt examples Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/types.md Examples demonstrating how to trigger ADC interrupts based on specific value ranges. ```cpp // Trigger when value moves outside the range 500-600 LowPower.attachAdcInterrupt(A0, callback, ADC_INT_OUTSIDE, 500, 600); // Trigger when value drops below 300 LowPower.attachAdcInterrupt(A0, callback, ADC_INT_BELOW_MAX, 0, 300); ``` -------------------------------- ### Potentiometer Hardware Setup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/examples.md Alternative hardware setup using a potentiometer to provide a variable voltage to the ADC pin. ```text VCC --+-- R (potentiometer) --+-- GND | | +------- A0 (wiper) ----+ ``` -------------------------------- ### GPIO Wakeup Pattern Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/00-START-HERE.txt Basic setup for GPIO-based wake-up functionality. ```cpp #include "ArduinoLowPower.h" ``` -------------------------------- ### Wake MCU with ADC Interrupt Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Example demonstrating how to set up an ADC window comparator to trigger a wakeup from sleep. ```cpp int adcValue = analogRead(A0); uint16_t lo = max(adcValue - 10, 0); uint16_t hi = min(adcValue + 10, 1023); void adcWakeup() { // ADC threshold crossed } LowPower.attachAdcInterrupt(A0, adcWakeup, ADC_INT_OUTSIDE, lo, hi); LowPower.sleep(); ``` -------------------------------- ### Timed Wakeup Pattern Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/00-START-HERE.txt Example of putting the device into a deep sleep state for a fixed duration. ```cpp #include "ArduinoLowPower.h" void loop() { doWork(); LowPower.sleep(5000); // Sleep 5 seconds } ``` -------------------------------- ### Configure RTC alarm wakeup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/types.md Example usage of the RTC_ALARM_WAKEUP constant with the attachInterruptWakeup method. ```cpp LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, NULL, CHANGE); LowPower.sleep(2000); // Wake after 2 seconds ``` -------------------------------- ### Battery Sensor Node Implementation Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.md Example of using ADC interrupts to wake a node for data transmission based on sensor thresholds. ```cpp void setup() { LowPower.attachAdcInterrupt(A0, sensorAlarm, ADC_INT_OUTSIDE, lo, hi); } void loop() { LowPower.sleep(); // Wait for sensor change int value = analogRead(A0); sendData(value); // Transmit data } ``` -------------------------------- ### Initialize and Use Library Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/00-START-HERE.txt Basic inclusion and instantiation of the ArduinoLowPower library. ```cpp #include "ArduinoLowPower.h" extern ArduinoLowPowerClass LowPower; LowPower.sleep(2000); // Sleep for 2 seconds ``` -------------------------------- ### void enableWakeupFrom(wakeup_reason peripheral, uint32_t pin, uint32_t event, uint32_t option) Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Configures wakeup from specific peripheral sources on nRF52 boards. ```APIDOC ## void enableWakeupFrom(wakeup_reason peripheral, uint32_t pin, uint32_t event, uint32_t option) ### Description Configures wakeup from specific peripheral sources. This method is available for nRF52 boards only. ### Parameters - **peripheral** (wakeup_reason) - Required - Wakeup source: GPIO_WAKEUP, NFC_WAKEUP, ANALOG_COMPARATOR_WAKEUP, or OTHER_WAKEUP. - **pin** (uint32_t) - Optional - Pin number for GPIO wakeup (default: 0xFF). - **event** (uint32_t) - Optional - Event type (peripheral-specific) (default: 0xFF). - **option** (uint32_t) - Optional - Additional options (peripheral-specific) (default: 0xFF). ### Return Value void ``` -------------------------------- ### Wakeup Configuration (All Platforms) Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/INDEX.md Methods for configuring wakeup sources common to all supported platforms. ```APIDOC ## void attachInterruptWakeup(pin, callback, mode) ### Description Registers a GPIO or RTC wakeup source. When the specified interrupt condition occurs, the device wakes up and executes the provided callback function. ``` -------------------------------- ### ArduinoLowPowerClass Methods Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/OVERVIEW.md Overview of available sleep, wakeup, and companion chip methods. ```text ArduinoLowPowerClass ├── Sleep Methods │ ├── idle() // CPU sleep, peripherals active │ ├── idle(uint32_t ms) // CPU sleep for duration │ ├── sleep() // Deep sleep, minimal peripherals │ ├── sleep(uint32_t ms) // Deep sleep for duration │ ├── deepSleep() // Equivalent to sleep() │ └── deepSleep(uint32_t ms) // Equivalent to sleep(ms) ├── Wakeup Configuration │ ├── attachInterruptWakeup() // Configure GPIO/RTC wakeup │ ├── attachAdcInterrupt() // Configure ADC wakeup (SAMD) │ ├── detachAdcInterrupt() // Cleanup ADC (SAMD) │ ├── enableWakeupFrom() // Configure wakeup (nRF52) │ └── wakeupReason() // Query wakeup source (nRF52) └── Companion Chip (conditional) ├── companionLowPowerCallback() // Register handler ├── companionSleep() // Put companion to sleep └── companionWakeup() // Wake companion ``` -------------------------------- ### Display Project File Structure Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/MANIFEST.md Visual representation of the documentation directory layout. ```text /output/ ├── README.md (Entry point) ├── INDEX.md (Quick reference) ├── OVERVIEW.md (Architecture) ├── MANIFEST.md (This file) ├── api-reference/ │ └── arduinolowpowerclass.md (API reference) ├── types.md (Types & constants) ├── configuration.md (Setup & config) ├── errors.md (Error handling) └── examples.md (Code patterns) ``` -------------------------------- ### 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 ``` -------------------------------- ### Companion Chip Methods Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/INDEX.md Methods for managing power states of companion chips on specific boards like Tian or Primo. ```APIDOC ## void companionLowPowerCallback(callback) ## void companionSleep() ## void companionWakeup() ### Description These methods provide control over companion chips. `companionLowPowerCallback` registers a handler for power events, while `companionSleep` and `companionWakeup` explicitly manage the power state of the companion hardware. ``` -------------------------------- ### wakeupReason() Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Queries the reason the MCU woke from sleep. This method is specific to nRF52-based boards. ```APIDOC ## wakeupReason() ### Description Queries the reason the MCU woke from sleep. This method is available for nRF52-based boards and returns an enumerated value indicating the source of the wakeup. ### Signature `wakeup_reason wakeupReason(void);` ### Parameters None ### Return Value `wakeup_reason` — Enumerated value indicating wakeup source: - `GPIO_WAKEUP` — GPIO interrupt triggered wakeup - `NFC_WAKEUP` — NFC event triggered wakeup - `ANALOG_COMPARATOR_WAKEUP` — Analog comparator event triggered wakeup - `OTHER_WAKEUP` — Other source or reason unknown ### Example ```cpp LowPower.sleep(); wakeup_reason reason = LowPower.wakeupReason(); if (reason == GPIO_WAKEUP) { // Handle GPIO wakeup } ``` ``` -------------------------------- ### Wakeup Configuration (nRF52 Only) Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/INDEX.md Methods for configuring peripheral-based wakeup triggers specific to nRF52 platforms. ```APIDOC ## void enableWakeupFrom(peripheral, pin, event, option) ## uint32_t wakeupReason() ### Description `enableWakeupFrom` configures specific peripheral events to trigger a device wakeup. `wakeupReason` allows the application to query the source that caused the last wakeup. ``` -------------------------------- ### Timed Wakeup Implementation Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/examples.md Demonstrates using LowPower.sleep() to pause execution for 2000 milliseconds. The RTC alarm is configured automatically to trigger the wakeup. ```cpp #include "ArduinoLowPower.h" void setup() { pinMode(LED_BUILTIN, OUTPUT); // Optionally register RTC callback: // LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, rtcCallback, CHANGE); } void loop() { // Perform activity digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); // Sleep for 2000 milliseconds (2 seconds) // RTC alarm automatically configured and triggers wakeup LowPower.sleep(2000); // Execution resumes here after 2 seconds // Loop repeats } void rtcCallback() { // Optional: Called when RTC alarm fires (if registered) // Typically not needed; sleep(ms) handles timing internally } ``` -------------------------------- ### Library Architecture Structure Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/OVERVIEW.md File structure of the ArduinoLowPower library. ```text src/ ├── ArduinoLowPower.h # Main header; class definition and types └── samd/ └── ArduinoLowPower.cpp # SAMD implementation ``` -------------------------------- ### void companionWakeup(void) Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Wakes the companion chip from low-power mode. This method is only applicable to boards with a companion chip, such as the SAMD Tian and nRF52 Primo. ```APIDOC ## void companionWakeup(void) ### Description Wakes companion chip from low-power mode. Enables synchronized operation where the companion chip wakes simultaneously with the main MCU. ### Parameters None ### Return Value void ``` -------------------------------- ### Implement Timed Sleep Wakeup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/OVERVIEW.md Uses the sleep method with a duration parameter to wake the processor after a set time. ```cpp void setup() { pinMode(LED_BUILTIN, OUTPUT); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(100); digitalWrite(LED_BUILTIN, LOW); LowPower.sleep(5000); // Wake after 5 seconds } ``` -------------------------------- ### Global LowPower Instance Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/OVERVIEW.md Declaration of the global singleton instance used for library operations. ```cpp extern ArduinoLowPowerClass LowPower; ``` -------------------------------- ### Querying MCU Wakeup Reason Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Use this method after a sleep cycle to determine the source of the wakeup. It is specifically designed for nRF52-based boards. ```cpp LowPower.sleep(); wakeup_reason reason = LowPower.wakeupReason(); if (reason == GPIO_WAKEUP) { // Handle GPIO wakeup } ``` -------------------------------- ### 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. ``` -------------------------------- ### Handle API Versioning Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/configuration.md Alias types based on the presence of the ARDUINO_API_VERSION macro to maintain compatibility between legacy and modern Arduino APIs. ```cpp #ifdef ARDUINO_API_VERSION using irq_mode = PinStatus; // Newer API #else using irq_mode = uint32_t; // Legacy API #endif ``` -------------------------------- ### Idle mode usage Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Demonstrates putting the MCU into idle mode indefinitely or for a specified duration. ```cpp // Infinite idle until interrupt LowPower.idle(); // Idle for 1000 milliseconds LowPower.idle(1000); ``` -------------------------------- ### 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); ``` -------------------------------- ### LowPower.idle() Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Places the MCU in idle mode, stopping the CPU while keeping most peripherals active. Optional duration allows for automatic wakeup via RTC. ```APIDOC ## void idle(void) ## void idle(uint32_t millis) ## void idle(int millis) ### Description Places the MCU in idle mode with optional timeout. Idle mode stops the CPU but keeps most peripherals active, reducing power consumption. ### Parameters - **millis** (uint32_t/int) - Optional - Duration in milliseconds. If provided, the MCU wakes after this duration via RTC alarm. ### Return Value void ### Example // Infinite idle until interrupt LowPower.idle(); // Idle for 1000 milliseconds LowPower.idle(1000); ``` -------------------------------- ### Detect Companion Chip Features Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/configuration.md Define custom macros based on specific board models to enable companion chip functionality. ```cpp #if defined(ARDUINO_SAMD_TIAN) || defined(ARDUINO_NRF52_PRIMO) #define BOARD_HAS_COMPANION_CHIP // Companion chip methods available: companionLowPowerCallback(), companionSleep(), companionWakeup() #endif ``` -------------------------------- ### Check RTC Configuration Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/errors.md Verify if the RTC is already configured before attaching an interrupt to avoid redundant initialization. ```cpp if (!rtc.isConfigured()) { attachInterruptWakeup(RTC_ALARM_WAKEUP, NULL, (irq_mode)0); } ``` -------------------------------- ### companionLowPowerCallback Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Registers a callback function to manage the low-power state of a companion chip. This is applicable only to boards with a companion chip, such as the SAMD Tian or nRF52 Primo. ```APIDOC ## void companionLowPowerCallback(onOffFuncPtr callback) ### Description Registers a callback to control companion chip low-power state. Stores the callback function pointer for later use by companionSleep() and companionWakeup(). ### Parameters - **callback** (onOffFuncPtr) - Required - Function pointer with signature void callback(bool state). Called with true to put companion chip to sleep, false to wake it. ### Return Value void ### Example ```cpp void companionSleepHandler(bool isSleeping) { if (isSleeping) { // Put companion chip to low-power state } else { // Wake companion chip } } LowPower.companionLowPowerCallback(companionSleepHandler); ``` ``` -------------------------------- ### Implementing Timed Wakeup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.md Uses the internal RTC to wake the processor after a specified duration. ```cpp void loop() { doWork(); LowPower.sleep(5000); // Sleep 5 seconds } ``` -------------------------------- ### Register Companion Low-Power Callback Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Registers a function to handle the power state of a companion chip. The callback is triggered by companionSleep and companionWakeup methods. ```cpp void companionSleepHandler(bool isSleeping) { if (isSleeping) { // Put companion chip to low-power state } else { // Wake companion chip } } LowPower.companionLowPowerCallback(companionSleepHandler); ``` -------------------------------- ### Wakeup Configuration (SAMD Only) Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/INDEX.md Methods for configuring ADC-based wakeup triggers specific to SAMD platforms. ```APIDOC ## void attachAdcInterrupt(pin, callback, mode, lo, hi) ## void detachAdcInterrupt() ### Description `attachAdcInterrupt` configures the ADC window comparator to trigger a wakeup based on voltage thresholds. `detachAdcInterrupt` performs cleanup and restores the ADC to its default state. ``` -------------------------------- ### 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(); ``` -------------------------------- ### Query Wakeup Reasons (nRF52) Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.md Methods specific to nRF52 boards for enabling peripheral-based wakeups and querying the cause of the last wakeup. ```cpp LowPower.enableWakeupFrom(peripheral, pin, event, option); wakeup_reason reason = LowPower.wakeupReason(); ``` -------------------------------- ### Test GPIO Wakeup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.md Configures a pin interrupt to trigger a wakeup event and execute a callback function. ```cpp void onWakeup() { digitalWrite(LED_BUILTIN, HIGH); // Visual confirmation } void setup() { LowPower.attachInterruptWakeup(8, onWakeup, CHANGE); } void loop() { LowPower.sleep(); } ``` -------------------------------- ### void companionSleep(void) Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Puts the companion chip into low-power mode. This method is only applicable to boards with a companion chip, such as the SAMD Tian and nRF52 Primo. ```APIDOC ## void companionSleep(void) ### Description Puts companion chip into low-power mode. Enables synchronized low-power operation where the companion chip enters a sleep state simultaneously with the main MCU. ### Parameters None ### Return Value void ``` -------------------------------- ### Implement GPIO Pin Wakeup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/OVERVIEW.md Uses attachInterruptWakeup to trigger a wake event on a specific pin state change. ```cpp void wakeupISR() { // Minimal code; executes in interrupt context } void setup() { LowPower.attachInterruptWakeup(8, wakeupISR, CHANGE); } void loop() { // Do work LowPower.sleep(); // Sleep until pin 8 changes state } ``` -------------------------------- ### 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. ``` -------------------------------- ### Sleep and Idle Methods Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/INDEX.md Methods to put the device into various power-saving states, either indefinitely or for a specified duration. ```APIDOC ## void idle() ## void idle(uint32_t ms) ## void sleep() ## void sleep(uint32_t ms) ## void deepSleep() ## void deepSleep(uint32_t ms) ### Description These methods control the power state of the device. `idle` puts the device into a shallow sleep with peripherals active, while `sleep` and `deepSleep` enter a deep sleep mode with minimal peripherals. Methods accepting a `ms` parameter will wake the device automatically after the specified duration. ``` -------------------------------- ### Interrupt-Driven Wakeup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/OVERVIEW.md Registering a GPIO interrupt as a wakeup source before entering sleep. ```cpp LowPower.attachInterruptWakeup(pin, callback, mode); // GPIO LowPower.sleep(); // MCU sleeps until interrupt ``` -------------------------------- ### Detect Features via Preprocessor Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.md Use preprocessor directives to check for board-specific capabilities or API versions. ```cpp #ifdef BOARD_HAS_COMPANION_CHIP // Companion chip methods available #endif #ifdef ARDUINO_API_VERSION // Modern API (irq_mode = PinStatus) #endif ``` -------------------------------- ### Manage Companion Chip Power Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.md Methods for handling power states on boards with companion chips like the Tian or Primo. ```cpp LowPower.companionLowPowerCallback(callback); LowPower.companionSleep(); LowPower.companionWakeup(); ``` -------------------------------- ### Configure GPIO wakeup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Enables wakeup from a specific GPIO pin on nRF52 boards. ```cpp // Wake from GPIO LowPower.enableWakeupFrom(GPIO_WAKEUP, 8); LowPower.sleep(); ``` -------------------------------- ### Configure Sleep and Idle Modes Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.md Methods for entering shallow or deep sleep states, with optional duration parameters in milliseconds. ```cpp LowPower.idle(); // Shallow sleep LowPower.idle(1000); // Shallow sleep for 1000 ms LowPower.sleep(); // Deep sleep LowPower.sleep(2000); // Deep sleep for 2000 ms LowPower.deepSleep(); // Equivalent to sleep() LowPower.deepSleep(2000); // Equivalent to sleep(2000) ``` -------------------------------- ### ADC Window Mode Wakeup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/OVERVIEW.md Configuring the ADC to wake the MCU when analog input thresholds are crossed. ```cpp LowPower.attachAdcInterrupt(A0, callback, ADC_INT_OUTSIDE, 100, 200); LowPower.sleep(); // ADC monitors A0; wakes if voltage leaves 100-200 range LowPower.detachAdcInterrupt(); // Restore ADC to normal state ``` -------------------------------- ### 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); ``` -------------------------------- ### Periodic Reporter Implementation Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/OVERVIEW.md Uses timed sleep to put the device into a low-power state for a specific duration between reporting intervals. ```cpp void loop() { reportStatus(); LowPower.sleep(30000); // Sleep 30 seconds } ``` -------------------------------- ### Architecture Detection Symbols Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/INDEX.md Conditional compilation macros for targeting specific hardware architectures. ```cpp #ifdef ARDUINO_ARCH_SAMD // SAMD-specific code (ADC, RTC details) #endif #ifdef ARDUINO_ARCH_NRF52 // nRF52-specific code (wakeup reason, enableWakeupFrom) #endif ``` -------------------------------- ### 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); ``` -------------------------------- ### External GPIO Wakeup Implementation Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/examples.md Configures a GPIO pin to trigger an interrupt that wakes the MCU from sleep. Ensure the ISR remains minimal to avoid blocking or unexpected behavior. ```cpp #include "ArduinoLowPower.h" volatile int wakeupCount = 1; // Count wakeups; use volatile for ISR access const int WAKEUP_PIN = 8; void wakeupISR() { // Executes when pin 8 changes state // Keep this function minimal — no delay(), Serial, or long operations wakeupCount++; } void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(WAKEUP_PIN, INPUT_PULLUP); // Pull-up prevents spurious wakeups // Register GPIO pin 8 as wakeup source on state change (CHANGE) LowPower.attachInterruptWakeup(WAKEUP_PIN, wakeupISR, CHANGE); } void loop() { // Flash LED N times based on wakeup count for (int i = 0; i < wakeupCount; i++) { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); } // Enter sleep mode // MCU wakes when pin 8 changes state (short to GND or floating to HIGH) LowPower.sleep(); // Infinite sleep until GPIO interrupt } ``` -------------------------------- ### Sleep mode usage Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Demonstrates putting the MCU into sleep mode indefinitely or for a specified duration. ```cpp // Infinite sleep until wakeup source triggers LowPower.sleep(); // Sleep for 2000 milliseconds LowPower.sleep(2000); ``` -------------------------------- ### Configure Multiple Wakeup Sources in ArduinoLowPower Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/examples.md Combines GPIO interrupts and RTC timeouts to wake the MCU. Use this for event-driven applications requiring periodic tasks. ```cpp #include "ArduinoLowPower.h" volatile bool gpioWakeup = false; void gpioISR() { gpioWakeup = true; } void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(8, INPUT_PULLUP); // Register GPIO as wakeup source LowPower.attachInterruptWakeup(8, gpioISR, FALLING); } void loop() { gpioWakeup = false; // Sleep for up to 10 seconds, but wake earlier if GPIO triggers LowPower.sleep(10000); if (gpioWakeup) { // Woke from GPIO interrupt digitalWrite(LED_BUILTIN, HIGH); delay(100); digitalWrite(LED_BUILTIN, LOW); } else { // Woke from RTC timeout (10 seconds elapsed) digitalWrite(LED_BUILTIN, HIGH); delay(50); digitalWrite(LED_BUILTIN, LOW); } } ``` -------------------------------- ### Configure Wakeup Interrupts Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.md Methods to attach and detach interrupts for GPIO, RTC, or ADC-based wakeups. ```cpp LowPower.attachInterruptWakeup(pin, callback, mode); // GPIO/RTC wakeup LowPower.attachAdcInterrupt(pin, callback, mode, lo, hi); // ADC (SAMD) LowPower.detachAdcInterrupt(); // Cleanup ADC (SAMD) ``` -------------------------------- ### Interrupt-Driven Application Pattern Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/INDEX.md Uses an interrupt service routine to wake the device from an infinite sleep state. ```cpp void wakeupISR() { /* Minimal code */ } void setup() { LowPower.attachInterruptWakeup(PIN, wakeupISR, CHANGE); } void loop() { // Do work LowPower.sleep(); // Infinite sleep until interrupt } ``` -------------------------------- ### Multi-Source Wakeup Pattern Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/INDEX.md Handles wakeups triggered by either a GPIO interrupt or a timeout. ```cpp volatile bool gpioWoke = false; void gpioISR() { gpioWoke = true; } void setup() { LowPower.attachInterruptWakeup(PIN, gpioISR, FALLING); } void loop() { LowPower.sleep(TIMEOUT_MS); // Wake on GPIO or timeout if (gpioWoke) { /* Handle GPIO */ } else { /* Handle timeout */ } } ``` -------------------------------- ### Detect Architecture via Preprocessor Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.md Use preprocessor directives to isolate architecture-specific code blocks. ```cpp #ifdef ARDUINO_ARCH_SAMD // SAMD-specific code #endif #ifdef ARDUINO_ARCH_NRF52 // nRF52-specific code #endif ``` -------------------------------- ### Wake companion chip Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Wakes the companion chip from low-power mode on supported boards. ```cpp LowPower.companionWakeup(); ``` -------------------------------- ### Define wakeup_reason enumeration Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/types.md Enumeration indicating the source that caused the MCU to wake from sleep. ```cpp typedef enum { OTHER_WAKEUP = 0, GPIO_WAKEUP = 1, NFC_WAKEUP = 2, ANALOG_COMPARATOR_WAKEUP = 3 } wakeup_reason; ``` -------------------------------- ### Monitoring ADC Thresholds Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.md Configures the ADC to wake the device when an analog input voltage crosses defined thresholds. Requires SAMD architecture. ```cpp LowPower.attachAdcInterrupt(A0, callback, ADC_INT_OUTSIDE, 100, 200); LowPower.sleep(); // Wake if A0 leaves 100-200 range LowPower.detachAdcInterrupt(); // Restore ADC ``` -------------------------------- ### Synchronize companion chip sleep Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Registers a callback and triggers sleep mode for boards with a companion chip. ```cpp LowPower.companionLowPowerCallback(companionCallback); LowPower.companionSleep(); LowPower.sleep(); ``` -------------------------------- ### Motion-Triggered Logger Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.md Uses a GPIO interrupt to wake the system for logging when motion is detected. ```cpp void setup() { LowPower.attachInterruptWakeup(MOTION_PIN, onMotion, RISING); } void loop() { LowPower.sleep(); // Wait for motion logEvent(); // Record event } ``` -------------------------------- ### ADC Window Comparator Wakeup Implementation Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/examples.md Configures the ADC to trigger an interrupt when the voltage on A0 leaves a specified range, allowing the device to sleep until a change is detected. ```cpp #include "ArduinoLowPower.h" volatile int adcWakeupCount = 1; const int ADC_PIN = A0; const int VOLTAGE_MARGIN = 10; // Hysteresis: detect 20-unit voltage swing void adcThresholdCrossed() { // Called when ADC reading leaves the window (goes outside or inside range) // Keep minimal; avoid delay() or blocking calls adcWakeupCount++; } void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(ADC_PIN, INPUT); } void loop() { // Perform activity based on wakeup count for (int i = 0; i < adcWakeupCount; i++) { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); } // Read current ADC value int currentValue = analogRead(ADC_PIN); // 0-1023 for 10-bit ADC // Define window around current value uint16_t lowerThreshold = max(currentValue - VOLTAGE_MARGIN, 0); uint16_t upperThreshold = min(currentValue + VOLTAGE_MARGIN, 1023); // Configure ADC to wake if voltage moves outside this range // CRITICAL: Must be called immediately before sleep() LowPower.attachAdcInterrupt(ADC_PIN, adcThresholdCrossed, ADC_INT_OUTSIDE, lowerThreshold, upperThreshold); // Enter sleep; ADC continues monitoring // Wake when voltage leaves the window or crosses either threshold LowPower.sleep(); // CRITICAL: Restore ADC to normal state immediately after waking LowPower.detachAdcInterrupt(); } ``` -------------------------------- ### Perform Timed Wakeup without Callback Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/errors.md Execute a timed sleep duration without requiring a specific callback function by passing NULL. ```cpp LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, NULL, CHANGE); LowPower.sleep(2000); // Wakes after 2 seconds, no callback ``` -------------------------------- ### Motion-Triggered Logger Implementation Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/OVERVIEW.md Configures an external interrupt to wake the device upon a rising edge signal from a motion sensor. ```cpp void motionDetected() { logEvent(); } void setup() { LowPower.attachInterruptWakeup(MOTION_PIN, motionDetected, RISING); } void loop() { LowPower.sleep(); // Wait for motion } ``` -------------------------------- ### 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(); ``` -------------------------------- ### attachInterruptWakeup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Configures a GPIO pin or internal source as a wakeup trigger while the device is in sleep or idle modes. ```APIDOC ## void attachInterruptWakeup(uint32_t pin, voidFuncPtr callback, irq_mode mode) ### Description Configures a GPIO pin or internal source as a wakeup trigger while in sleep/idle modes. For GPIO pins, it enables the External Interrupt Controller (EIC); for special sources like RTC, it configures the RTC alarm. ### Parameters - **pin** (uint32_t) - Required - Pin number (0-PINS_COUNT) or special wakeup source constant (e.g., RTC_ALARM_WAKEUP). - **callback** (voidFuncPtr) - Required - Function pointer to call when wakeup occurs. Signature: void callback(void). - **mode** (irq_mode) - Required - Interrupt trigger mode: LOW, CHANGE, RISING, FALLING, or HIGH. ### Return Value void ### Example ```cpp // Wake on GPIO pin 8 with CHANGE interrupt void wakeupHandler() { // Called when pin 8 changes state } LowPower.attachInterruptWakeup(8, wakeupHandler, CHANGE); LowPower.sleep(); ``` ``` -------------------------------- ### ADC State Restoration Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/configuration.md Manual restoration steps required after an ADC wakeup to return the peripheral to its default state. ```cpp ADC->CTRLA.bit.ENABLE = 0; // Disable ADC ADC->CTRLB.bit.FREERUN = 0; // Disable continuous mode ADC->CTRLA.bit.RUNSTDBY = 1; // Disable standby mode (inverted logic) ADC->INTENCLR.bit.WINMON = 1; // Disable window interrupt ADC->WINCTRL.reg = ADC_WINCTRL_WINMODE_DISABLE; // Disable window ADC->CTRLB.bit.PRESCALER = ADC_CTRLB_PRESCALER_DIV512_Val; // Restore prescaler // ADC clock restored to GCLK0 ``` -------------------------------- ### Implement Interrupt Callbacks Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.md Use volatile variables to communicate between ISRs and the main loop. Avoid blocking operations like delay or serial communication within callbacks. ```cpp // CORRECT: Only modify volatile variables volatile bool flag = false; void myISR() { flag = true; } // WRONG: Do not do this in callbacks void badISR() { delay(100); // BLOCKS Serial.println("x"); // MAY HANG doWork(); // SHOULD NOT } ``` -------------------------------- ### Deep sleep methods for ArduinoLowPower Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Methods to put the MCU into deep sleep mode either indefinitely or for a specified duration. ```cpp void deepSleep(void); void deepSleep(uint32_t millis); void deepSleep(int millis); ``` ```cpp // Deep sleep indefinitely LowPower.deepSleep(); // Deep sleep for 5000 milliseconds LowPower.deepSleep(5000); ``` -------------------------------- ### Define onOffFuncPtr function pointer Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/types.md Defines a function pointer type for companion chip sleep control callbacks, accepting a boolean state parameter. ```cpp typedef void (*onOffFuncPtr)(bool); ``` ```cpp void callback(bool state) ``` -------------------------------- ### Configure Pin Interrupt Wakeup Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/00-START-HERE.txt Attaches an interrupt to a specific pin to trigger a wakeup from sleep mode. ```cpp void wakeupISR() { } void setup() { LowPower.attachInterruptWakeup(8, wakeupISR, CHANGE); } void loop() { LowPower.sleep(); // Wake when pin 8 changes } ``` -------------------------------- ### attachAdcInterrupt Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Configures the ADC window comparator to wake the MCU from sleep when an analog input crosses a defined threshold. This method is specific to SAMD architectures. ```APIDOC ## void attachAdcInterrupt(uint32_t pin, voidFuncPtr callback, adc_interrupt mode, uint16_t lo, uint16_t hi) ### Description Configures the ADC window comparator to wake the MCU from sleep when an analog input crosses a defined threshold. This method must be called before entering sleep mode. ### Parameters - **pin** (uint32_t) - Required - Analog pin number mapped to the ADC channel. - **callback** (voidFuncPtr) - Required - Function pointer to call on ADC window event (signature: void callback(void)). - **mode** (adc_interrupt) - Required - Window comparison mode (ADC_INT_BETWEEN, ADC_INT_OUTSIDE, ADC_INT_ABOVE_MIN, ADC_INT_BELOW_MAX). - **lo** (uint16_t) - Required - Lower threshold value (0-1023). - **hi** (uint16_t) - Required - Upper threshold value (0-1023). ### Return Value - **void** ### Example ```cpp void adcWakeup() { // ADC threshold crossed } LowPower.attachAdcInterrupt(A0, adcWakeup, ADC_INT_OUTSIDE, 500, 600); LowPower.sleep(); ``` ``` -------------------------------- ### Implement Interrupt Callbacks Safely Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/errors.md Avoid blocking functions like delay() or Serial.print() inside interrupt handlers; use volatile flags instead. ```cpp // WRONG: Do not do this void wakeupHandler() { delay(100); // WRONG Serial.println("Woke up"); // WRONG } // CORRECT: Only set flags volatile bool woken = false; void wakeupHandler() { woken = true; } ``` -------------------------------- ### LowPower.sleep() Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Places the MCU in a low-power sleep state where most peripherals are disabled. This is the primary mode for battery-powered applications. ```APIDOC ## void sleep(void) ## void sleep(uint32_t millis) ## void sleep(int millis) ### Description Places the MCU in sleep mode (deep sleep on SAMD) with optional timeout. Most peripherals are disabled to minimize power consumption. ### Parameters - **millis** (uint32_t/int) - Optional - Duration in milliseconds. If provided, the MCU wakes after this duration via RTC alarm. ### Return Value void ### Example // Infinite sleep until wakeup source triggers LowPower.sleep(); // Sleep for 2000 milliseconds LowPower.sleep(2000); ``` -------------------------------- ### Set MCU to Sleep Mode Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/docs/api.md Puts the MCU in sleep mode with slower wake-up time, keeping only chosen peripherals active. ```cpp LowPower.sleep(); LowPower.sleep(milliseconds); ``` -------------------------------- ### Periodic Task Pattern Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/INDEX.md Executes tasks at fixed intervals by sleeping for a specified duration. ```cpp void setup() { } void loop() { // Do periodic work LowPower.sleep(PERIOD_MS); // Wait before next iteration } ``` -------------------------------- ### API Version Detection Symbol Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/INDEX.md Distinguishes between modern and legacy API modes for interrupt configuration. ```cpp #ifdef ARDUINO_API_VERSION // Modern API (irq_mode = PinStatus) #else // Legacy API (irq_mode = uint32_t) #endif ``` -------------------------------- ### Periodic Status Reporter Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.md Performs a task and enters sleep mode for a fixed interval before repeating. ```cpp void loop() { reportStatus(); LowPower.sleep(30000); // Report every 30 seconds } ``` -------------------------------- ### Companion Chip Detection Symbol Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/INDEX.md Checks for the availability of companion chip power management functions. ```cpp #ifdef BOARD_HAS_COMPANION_CHIP // companionSleep(), companionWakeup() available #endif ``` -------------------------------- ### Configure ADC Interrupt Signature Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md The function signature for attaching an ADC interrupt to wake the MCU. ```cpp void attachAdcInterrupt(uint32_t pin, voidFuncPtr callback, adc_interrupt mode, uint16_t lo, uint16_t hi); ``` -------------------------------- ### Measure Sleep Duration with millis() Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/examples.md Calculates the actual time spent in sleep mode. Note that RTC resolution is 1 second and SysTick is disabled during sleep. ```cpp #include "ArduinoLowPower.h" void setup() { Serial.begin(9600); } void loop() { unsigned long before = millis(); LowPower.sleep(2000); // Request 2-second sleep unsigned long after = millis(); unsigned long actualDuration = after - before; Serial.print("Sleep duration (ms): "); Serial.println(actualDuration); // Output: ~2000 (±1000 due to RTC 1-second resolution) } ``` -------------------------------- ### Verify Sleep Duration Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/README.md Measures the actual time spent in sleep mode using millis() before and after the sleep call. ```cpp unsigned long start = millis(); LowPower.sleep(2000); unsigned long elapsed = millis() - start; Serial.println(elapsed); // ~2000 (±1000 due to RTC resolution) ``` -------------------------------- ### Validate Interrupt Pin Configuration Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/errors.md Checks if a pin is valid for interrupts before proceeding with configuration. ```cpp if (pin > PINS_COUNT) { // Handled as special case (e.g., RTC_ALARM_WAKEUP) return; } EExt_Interrupts in = g_APinDescription[pin].ulExtInt; if (in == NOT_AN_INTERRUPT || in == EXTERNAL_INT_NMI) return; // Silent failure: pin not interrupt-capable ``` -------------------------------- ### deepSleep Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/api-reference/arduinolowpowerclass.md Places the MCU in deep sleep mode, which provides the lowest power consumption by disabling most peripherals. It can be called indefinitely or with a timeout in milliseconds to wake via RTC alarm. ```APIDOC ## void deepSleep(void) ## void deepSleep(uint32_t millis) ## void deepSleep(int millis) ### Description Places the MCU in deep sleep mode with optional timeout. On SAMD architecture, this provides the lowest power consumption with most peripherals disabled. ### Parameters - **millis** (uint32_t/int) - Optional - Duration in milliseconds. If provided, MCU wakes after this duration via RTC alarm. ### Return Value void ### Example ```cpp // Deep sleep indefinitely LowPower.deepSleep(); // Deep sleep for 5000 milliseconds LowPower.deepSleep(5000); ``` ``` -------------------------------- ### Set MCU to Deep Sleep Mode Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/docs/api.md Puts the MCU in deep sleep mode where all but RTC peripherals are stopped. Wake-up is only possible via RTC or interrupt-capable pins. ```cpp LowPower.deepSleep(); LowPower.deepSleep(milliseconds); ``` -------------------------------- ### Define voidFuncPtr function pointer Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/types.md Defines a function pointer type for general-purpose interrupt and wakeup callbacks that take no arguments. ```cpp typedef void (*voidFuncPtr)(void); ``` ```cpp void callback(void) ``` -------------------------------- ### ADC Threshold Monitoring Pattern (SAMD) Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/INDEX.md Monitors an analog pin for threshold crossings. Always detach the interrupt after waking to prevent resource leaks. ```cpp void setup() { } void loop() { int val = analogRead(A0); LowPower.attachAdcInterrupt(A0, callback, ADC_INT_OUTSIDE, val-20, val+20); LowPower.sleep(); LowPower.detachAdcInterrupt(); // Critical! } ``` -------------------------------- ### RTC_ALARM_WAKEUP Source: https://github.com/arduino-libraries/arduinolowpower/blob/master/_autodocs/types.md A constant used to configure the RTC alarm as a wakeup source for the device. ```APIDOC ## RTC_ALARM_WAKEUP ### Description Special constant used to configure the RTC alarm as a wakeup source. It is passed to the `attachInterruptWakeup()` method to enable RTC alarm functionality. ### Value - **Value**: 0xFF - **Type**: uint32_t ### Usage ```cpp LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, NULL, CHANGE); ``` ```