From the task, we understand that,
The Arduino UNO (ATmega328P) supports six sleep modes, each offering different power-saving levels:
For best power efficiency, use Power-down Mode with external interrupts for wake-up.
Here is a comparison of all sleep modes available in the Arduino UNO (ATmega328P):
Before writing the firmware, let’s break down the key functionalities:
SLEEP_MODE_PWR_DOWN
is the lowest power mode, turning off everything except external interrupts.sleep_bod_disable()
function disables the Brown-Out Detector (BOD), further reducing power consumption.#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#define INT0_PIN 2 // External Interrupt (D2)
#define ADC_PIN A0 // ADC Input Pin
#define DEBOUNCE_DELAY 50
bool isPressed = false;
void wakeUpISR() {
// Empty ISR, just needed to wake the microcontroller
}
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, LOW); //Turning off the on board LED for power saving
pinMode(INT0_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INT0_PIN), wakeUpISR, FALLING);
}
void enterSleepMode() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set sleep mode
cli(); // Disable interrupts to avoid race conditions
sleep_enable();
sleep_bod_disable(); // Disable Brown-Out Detector for lower power
sei(); // Re-enable interrupts
sleep_mode(); // Enter sleep mode (Execution stops here)
// Code resumes here after wake-up
sleep_disable();
delay(DEBOUNCE_DELAY);
if (digitalRead(2) == LOW)
isPressed = true;
}
void loop() {
enterSleepMode(); // Sleep until button press
if (isPressed) {
Serial.begin(115200); // Restore Serial after wake-up
Serial.print("ADC value: ");
Serial.println(analogRead(ADC_PIN)); // Print ADC value after waking up
delay(10); // Allow time for stability
isPressed = false;
}
}
Setup Function (setup()
):
INPUT_PULLUP
.INT0
) to wakeUpISR()
with a FALLING edge trigger on Pin D2.Sleep Mode (enterSleepMode()
):
cli()
.sei()
and enter sleep using sleep_mode()
.Debouncing Handling:
delay(DEBOUNCE_DELAY)
: Handles button debounce.digitalRead(2) == LOW
: Checks if button is pressed.isPressed = true
: Flags a valid button press.Loop Function (loop()):
enterSleepMode()
to enter low-power state.