Implementing ESP32 Deep Sleep Mode with GPIO Wakeup for Battery-Efficient Environmental Monitoring IoT Devices
In this tutorial, we will learn how to implement deep sleep mode on the ESP32 microcontroller, allowing for battery-efficient environmental monitoring. By utilizing GPIO wakeup, we can activate the ESP32 to take measurements at specified intervals, conserving energy when not in use.
Prerequisites
- Basic understanding of Arduino IDE
- ESP32 development board
- Sensors for environmental monitoring (e.g., DHT11 for temperature and humidity)
- USB cable for programming the ESP32
- Jumper wires and breadboard (optional, for sensor connections)
Parts/Tools
- ESP32 Development Board
- DHT11 or DHT22 Temperature and Humidity Sensor
- Arduino IDE installed on your computer
- Libraries for the DHT sensor (e.g., DHT.h)
Steps
- Set Up Your Development Environment
- Install the Arduino IDE from the official website.
- Add the ESP32 board to the Arduino IDE:
File > Preferences Add "https://dl.espressif.com/dl/package_esp32_index.json" to the "Additional Board Manager URLs"
- Open the Boards Manager:
Tools > Board > Boards Manager
- Search for “ESP32” and install the latest version.
- Wiring the Components
- Connect the DHT11 sensor to the ESP32:
- VCC to 3.3V
- GND to GND
- Data Pin to a GPIO pin (e.g., GPIO 4)
- Ensure all connections are secure.
- Connect the DHT11 sensor to the ESP32:
- Install Necessary Libraries
- Open the Library Manager:
Sketch > Include Library > Manage Libraries
- Search for “DHT sensor library” and install it.
- Also, install “Adafruit Unified Sensor” library.
- Open the Library Manager:
- Write the Code
- Open a new sketch in Arduino IDE and start coding:
#include "DHT.h" #define DHTPIN 4 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(115200); dht.begin(); // Configure the GPIO pin for wakeup pinMode(DHTPIN, INPUT_PULLUP); } void loop() { // Read sensor data float h = dht.readHumidity(); float t = dht.readTemperature(); // Print data to Serial Monitor Serial.print("Humidity: "); Serial.print(h); Serial.print("% Temperature: "); Serial.print(t); Serial.println("°C"); // Enter deep sleep for 10 seconds esp_sleep_enable_ext0_wakeup(GPIO_NUM_4, 0); // Wake up when GPIO 4 is LOW esp_deep_sleep(10 * 1000000); // Sleep for 10 seconds }
- Upload the code to your ESP32.
- Open a new sketch in Arduino IDE and start coding:
- Testing the Setup
- Open the Serial Monitor (set baud rate to 115200).
- Observe the readings and the deep sleep behavior.
Troubleshooting
- ESP32 not waking up:
Check the GPIO pin connection and ensure the pin is configured correctly in your code.
- Incorrect sensor readings:
Verify sensor wiring and ensure the correct sensor type is defined in the code.
- Code upload issues:
Ensure the correct COM port is selected in Arduino IDE and your ESP32 is in the proper mode for uploading.
Conclusion
By implementing deep sleep mode with GPIO wakeup on the ESP32, we can significantly enhance the battery life of our environmental monitoring devices. This approach is ideal for IoT applications where energy efficiency is crucial. Experiment with different sleep intervals and sensor configurations to optimize your device’s performance.