Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
In the realm of electronics and embedded systems, power efficiency is a critical concern, especially for battery-operated devices. Sleep modes in microcontrollers, like those used in Arduino boards, offer a way to significantly reduce power consumption by putting the microcontroller into a low-power state when it's not actively performing tasks. This article delves into the various sleep modes available in Arduino, their importance, and how to implement them effectively in your projects.
Project: In this example project, we will create a simple temperature monitoring system using an Arduino board and a temperature sensor. The system will periodically wake up from sleep mode, read the temperature, and then return to sleep mode to conserve power. The objective is to demonstrate how to utilize sleep modes to extend the battery life of the project.
Components List:
Examples:
// Include necessary libraries
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <DHT.h>
// Define pin for DHT11 sensor
#define DHTPIN 2
#define DHTTYPE DHT11
// Create DHT object
DHT dht(DHTPIN, DHTTYPE);
// Function to configure watchdog timer for sleep
void setup_watchdog(int prescalar) {
byte bb;
if (prescalar > 9 ) prescalar = 9; // Limit prescalar to 9
bb = prescalar & 7; // Set lower three bits
if (prescalar > 7) bb |= (1<<5); // Set bit 5 if prescalar > 7
bb |= (1<<WDCE) | (1<<WDE); // Enable configuration change
MCUSR &= ~(1<<WDRF); // Clear WDRF
WDTCSR |= (1<<WDCE) | (1<<WDE); // Start timed sequence
WDTCSR = bb; // Set new watchdog timeout value
WDTCSR |= _BV(WDIE); // Enable watchdog interrupt
}
// Watchdog interrupt service routine
ISR(WDT_vect) {
// Do nothing, just wake up from sleep
}
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize DHT sensor
dht.begin();
// Setup watchdog timer with 8s timeout
setup_watchdog(6);
}
void loop() {
// Read temperature from DHT11 sensor
float temperature = dht.readTemperature();
// Print temperature to serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
// Put the Arduino to sleep
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set sleep mode to power down
sleep_enable(); // Enable sleep mode
sleep_mode(); // Go to sleep
// The program continues here after waking up
sleep_disable(); // Disable sleep mode
}
Explanation:
setup_watchdog
function configures the watchdog timer to wake up the Arduino from sleep mode after a specified interval.