The ESP8266 has a very effective deep sleep mode that allows it to consume very little power (down to 20uA or less, depending on the board and external components). When in deep sleep, the CPU is halted, and most peripherals (including WiFi) are powered down.
To wake up from deep sleep, the ESP8266 typically needs an external reset. The easiest way to achieve this automatically for recurring sleep cycles is to connect GPIO16 to the RST (Reset) pin. When the deep sleep timer expires, the ESP8266 pulls GPIO16 low, which resets the chip and causes it to reboot, effectively "waking up."
Here's the code and the hardware connection instructions:
Hardware Connection (Crucial!)
For automatic wake-up from deep sleep, you MUST connect:
-
GPIO16 pin to the RST (Reset) pin.
-
Important Note: Make sure you disconnect this wire when uploading new code, as GPIO16 is also used for flashing. Reconnect it after a successful upload.
ESP8266 Code for Deep Sleep
This code will:
- Initialize serial communication.
- Increment a boot counter (which resets on power loss, but counts reboots from deep sleep).
- Print some messages.
- Enter deep sleep for a specified duration (e.g., 10 seconds).
- When the timer expires, GPIO16 will pull RST low, causing the ESP8266 to reboot and start the process again from
setup().
#include <ESP8266WiFi.h> // Not strictly needed for deep sleep itself, but often included for ESP8266 projects
// Global variable to count boots.
// Note: This variable will reset to 0 if the ESP8266 loses power completely.
// It will retain its value across deep sleep cycles.
int bootCount = 0;
void setup() {
Serial.begin(115200);
// Wait for serial monitor to open, helpful for debugging
while (!Serial) {
delay(10);
}
Serial.println("\n"); // New line for cleaner output
Serial.println("ESP8266 Deep Sleep Example");
// Increment boot count
bootCount++;
Serial.print("Boot #");
Serial.println(bootCount);
// You can perform some tasks here before going to sleep
// For example, connect to WiFi, send data, read sensors, etc.
// For this example, we'll just print messages.
// -----------------------------------------------------------
// Prepare for deep sleep
// -----------------------------------------------------------
// Set the duration for deep sleep in microseconds
// 1 second = 1,000,000 microseconds
long deepSleepSeconds = 10;
long deepSleepMicroseconds = deepSleepSeconds * 1000 * 1000;
Serial.print("I'm awake and performing tasks... Going to deep sleep for ");
Serial.print(deepSleepSeconds);
Serial.println(" seconds.");
Serial.println("Connect GPIO16 to RST for automatic wake-up!");
// Wait a moment for serial output to complete before sleeping
Serial.flush();
delay(100);
// Enter deep sleep
// ESP.deepSleep(microseconds)
// This function takes a single argument, the duration in microseconds.
// After calling this, the ESP8266 will power down most components.
// It will only wake up when the specified time elapses (via GPIO16-RST connection)
// or if an external reset occurs (e.g., manual reset button, power cycle).
ESP.deepSleep(deepSleepMicroseconds);
// Code after ESP.deepSleep() will NOT be executed
Serial.println("This line will not be printed if deep sleep is successful.");
}
void loop() {
// In deep sleep scenarios, the loop() function typically does not run.
// The ESP8266 goes to sleep from setup() and reboots into setup() upon waking.
// If you had conditional deep sleep (e.g., sleep after a certain condition is met),
// then loop() would run until that condition is met.
// For this example, loop() is effectively unused.
}
How to Use
-
Open Arduino IDE:
- Make sure you have the ESP8266 board package installed (Go to
File > Preferences > Additional Boards Manager URLs and add http://arduino.esp8266.com/stable/package_esp8266com_index.json. Then go to Tools > Board > Boards Manager and search for "esp8266" to install it).
- Select your ESP8266 board (
Tools > Board > ESP8266 Boards > NodeMCU 1.0 (ESP-12E Module) or whichever model you have).
- Select the correct COM port.
-
Upload the Code:
- Connect your ESP8266 to your computer via USB.
- IMPORTANT: Before uploading, ensure GPIO16 is NOT connected to RST. If it is, disconnect it temporarily.
- Click the "Upload" button.
-
Connect GPIO16 to RST:
- Once the upload is complete and you see "Done uploading." in the IDE, disconnect the USB cable from the ESP8266 board.
- Now, connect a jumper wire from the GPIO16 pin to the RST pin on your ESP8266 board.
- Reconnect the USB cable to power the board.
-
Open Serial Monitor:
- Open the Serial Monitor in the Arduino IDE (Tools > Serial Monitor).
- Set the baud rate to 115200.
Expected Output in Serial Monitor
You will see something like this repeating every 10 seconds:
ESP8266 Deep Sleep Example
Boot #1
I'm awake and performing tasks... Going to deep sleep for 10 seconds.
Connect GPIO16 to RST for automatic wake-up!
ESP8266 Deep Sleep Example
Boot #2
I'm awake and performing tasks... Going to deep sleep for 10 seconds.
Connect GPIO16 to RST for automatic wake-up!
ESP8266 Deep Sleep Example
Boot #3
I'm awake and performing tasks... Going to deep sleep for 10 seconds.
Connect GPIO16 to RST for automatic wake-up!
...and so on. The Boot # will increment each time it wakes up from deep sleep. If you remove power and reapply it, Boot # will reset to 1.
Considerations
- Power Consumption: Deep sleep drastically reduces power consumption, making it ideal for battery-powered applications.
- No WiFi/CPU: During deep sleep, WiFi is off, and the CPU is halted. No code executes.
- State Preservation: The
bootCount variable in this example only persists across deep sleep cycles as long as power is continuously supplied. If you need to store data across full power cycles (e.g., number of times the device has woken up ever), you would need to use EEPROM or LittleFS/SPIFFS to store data in flash memory.
- Alternative Wake-Up: Besides the timer, the ESP8266 can also be woken up by an external interrupt on the
RST pin (e.g., a button press connected to ground through a pull-up resistor, or another microcontroller).
- Debugging: It can be harder to debug a device in deep sleep because the serial connection is lost during sleep. Ensure your
setup() messages are clear.