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 world of embedded systems and IoT (Internet of Things), power consumption is a critical factor. Devices often run on batteries, and efficient power management can significantly extend their operational life. This article will explore techniques to reduce power consumption in Arduino projects, ensuring that your devices can run longer on a single charge. We'll focus on practical adjustments and code optimizations tailored for the Arduino environment.
Project: In this example project, we will create a temperature and humidity monitoring system using an Arduino Nano, a DHT22 sensor, and an OLED display. The system will enter a low-power sleep mode when not actively measuring or displaying data, significantly reducing power consumption.
Components List:
Examples:
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <avr/sleep.h>
// Define pin for DHT22 sensor
#define DHTPIN 2
#define DHTTYPE DHT22
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Initialize OLED display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize DHT sensor
dht.begin();
// Initialize OLED display
if (!display.begin(SSD1306_I2C_ADDRESS, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.display();
delay(2000); // Pause for 2 seconds
// Display initial message
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Initializing...");
display.display();
delay(1000);
}
void loop() {
// Measure temperature and humidity
float h = dht.readHumidity();
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Display the readings on the OLED
display.clearDisplay();
display.setCursor(0, 0);
display.print("Humidity: ");
display.print(h);
display.print(" %");
display.setCursor(0, 10);
display.print("Temp: ");
display.print(t);
display.print(" C");
display.display();
// Print the readings to the serial monitor
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F(" % Temperature: "));
Serial.print(t);
Serial.println(F(" *C "));
// Enter sleep mode for 10 seconds
enterSleepMode(10);
}
void enterSleepMode(int sleepTime) {
// Set sleep mode to Power Down
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
// Sleep for the specified time
for (int i = 0; i < sleepTime; i++) {
delay(1000);
}
// Disable sleep
sleep_disable();
}
Explanation:
Common Challenges: