This guide shows how to use deep sleep with the ESP8266 (NodeMCU) using Arduino IDE. We’ll cover deep sleep with timer wake up and deep sleep with external wake up using the reset (RST) pin.
To put the ESP8266 in deep sleep mode, use ESP.deepSleep(uS) and pass as argument sleep time in microseconds. GPIO 16 must be connected to reset (RST) pin so the ESP8266 is able to wake up.
To put the ESP8266 in deep sleep mode for an indefinite period of time use ESP.deepSleep(0). The ESP8266 will wake up when the RST pin receives a LOW signal.
Watch the Video Tutorial
This guide is available in video format (watch below) and in written format (continue reading this page).
Related Content: ESP32 Deep Sleep with Arduino IDE and Wake Up Sources
Introducing Deep Sleep Mode
If you’ve made a project with an ESP8266 board that is battery powered, or if you just connected your ESP8266 NodeMCU board to a power bank. After running it for a while, you realize the battery doesn’t last long, specially if you’re using Wi-Fi.
If you put your ESP8266 in deep sleep mode, it reduces power consumption and your batteries will last longer.
Having the ESP8266 in deep sleep mode means cutting with the activities that consume more power while operating (like Wi-Fi) but leave just enough activity to wake up the processor when something interesting happens.
Types of Sleep
There are three different types of sleep mode: modem sleep, light sleep, and deep sleep. The table below shows the differences between each mode (information from the ESP8266 datasheet).
Item | Modem-sleep | Light-sleep | Deep-sleep |
Wi-Fi | OFF | OFF | OFF |
System clock | ON | OFF | OFF |
RTC | ON | ON | ON |
CPU | ON | Pending | OFF |
Substrate current | 15 mA | 0.4 mA | ~20 uA |
Average current (DTIM = 1) | 16.2 mA | 1.8 mA | – |
Average current (DTIM = 3) | 15.4 mA | 0.9 mA | – |
Average current (DTIM = 10) | 15.2 mA | 0.55 mA | – |
Note: the power consumption on the table refers to the ESP8266 as a standalone chip. If you’re using a development board, they have passive components that use more current.
They all have different purposes and they should be used in different applications.
In this article, we’ll cover deep sleep mode. Everything is always off, except the Real Time Clock (RTC), which is how the ESP8266 keeps track of time.
This is the most power efficient option and the ESP chip only draws approximately 20uA. However, if you use a full-feature development board with built-in programmer, LEDs, and so on, you won’t be able to achieve such a low power state.
Deep Sleep Sketch
With deep sleep, an example application looks like this:
- The ESP8266 connects to Wi-Fi
- The ESP8266 performs a task (reads a sensor, publishes an MQTT message, etc)
- Sleeps for a predefined period of time
- The ESP8266 wakes up
- The process is repeated over and over again
Wake up Sources
After putting the ESP8266 in deep sleep mode, there are different ways to wake it up:
- #1 timer wake up: the ESP8266 wakes itself up after a predefined period of time
- #2 external wake up: the ESP8266 wakes up when you press the RST button (the ESP8266 restarts)
For low-power projects, you might consider using the ESP32 board which offers more deep sleep modes and wake up sources.
#1 ESP8266 Deep Sleep with Timer Wake Up
To use timer wake up with ESP8266, you need to connect the RST pin to GPIO 16 which is labeled as D0, in a NodeMCU board. Simply follow the next schematic diagram:
Connect the RST pin to GPIO 16 only after uploading the code.
If you take a look at the NodeMCU pinout, you can see that GPIO 16 is a special pin and it has a WAKE feature.
Recommended reading: ESP8266 Pinout Reference Guide
The RST pin of the ESP8266 is always HIGH while the ESP8266 is running. However, when the RST pin receives a LOW signal, it restarts the microcontroller.
If you set a deep sleep timer with the ESP8266, once the timer ends, GPIO 16 sends a LOW signal. That means that GPIO 16, when connected to the RST pin, can wake up the ESP8266 after a set period of time.
ESP8266 NodeMCU Timer Wake Up Sketch
Having the ESP8266 add-on for Arduino IDE installed (how to Install the ESP8266 Board in Arduino IDE), go to Tools and select “NodeMCU (ESP-12E Module)”. Here’s the code that you need to upload to your ESP:
/*
* ESP8266 Deep sleep mode example
* Rui Santos
* Complete Project Details https://randomnerdtutorials.com
*/
void setup() {
Serial.begin(115200);
Serial.setTimeout(2000);
// Wait for serial to initialize.
while(!Serial) { }
// Deep sleep mode for 30 seconds, the ESP8266 wakes up by itself when GPIO 16 (D0 in NodeMCU board) is connected to the RESET pin
Serial.println("I'm awake, but I'm going into deep sleep mode for 30 seconds");
ESP.deepSleep(30e6);
// Deep sleep mode until RESET pin is connected to a LOW signal (for example pushbutton or magnetic reed switch)
//Serial.println("I'm awake, but I'm going into deep sleep mode until RESET pin is connected to a LOW signal");
//ESP.deepSleep(0);
}
void loop() {
}
In this example, we print a message in the Serial Monitor:
Serial.println("I'm awake, but I'm going into deep sleep mode until RESET pin is connected to a LOW signal");
After that, the ESP8266 goes to sleep for 30 seconds.
ESP.deepSleep(30e6);
To put the ESP8266 in deep sleep, you use ESP.deepsleep(uS) and pass as argument the sleep time in microseconds.
In this case, 30e6 corresponds to 30000000 microseconds which is equal to 30 seconds.
After uploading the code, press the RST button to start running the code, and then connect RST to GPIO 16. The ESP8266 should wake up every 30 seconds and print a message in the Serial Monitor as shown below.
This example simply prints a message in the Serial Monitor, but in a real world application, you’ll perform a useful task like making a request, publish sensor readings, etc.
ESP-01 Timer Wake Up Circuit
If you want to make a similar setup with an ESP-01 board, you need to solder a wire as shown below. That tiny pin is GPIO 16 and it needs to be connected to RST pin.
However, the pins are so tiny that it is really hard to solder a wire like that to GPIO 16 on the ESP-01… So, for this wake up mode you should use the NodeMCU board or a bare ESP12-E chip.
Recommended reading: ESP8266 Pinout Reference
#2 ESP8266 Deep Sleep with External Wake Up
You can also wake up the ESP8266 with an external wake up, like the press of a button or a reed switch. You just need to put the ESP8266 in deep sleep mode for an indefinite period of time, and then set the RST pin to LOW to wake it up.
To test this setup, wire a pushbutton to your ESP8266 board as shown in the following schematic diagram. When you press the pushbutton, the RST pin goes LOW.
If you’re using an ESP-01, follow the next diagram instead.
ESP8266 External Wake Up Sketch
Then, upload the following code to your ESP8266 board.
/*
* ESP8266 Deep sleep mode example
* Rui Santos
* Complete Project Details https://randomnerdtutorials.com
*/
void setup() {
Serial.begin(115200);
Serial.setTimeout(2000);
// Wait for serial to initialize.
while(!Serial) { }
// Deep sleep mode for 30 seconds, the ESP8266 wakes up by itself when GPIO 16 (D0 in NodeMCU board) is connected to the RESET pin
//Serial.println("I'm awake, but I'm going into deep sleep mode for 30 seconds");
//ESP.deepSleep(30e6);
// Deep sleep mode until RESET pin is connected to a LOW signal (for example pushbutton or magnetic reed switch)
Serial.println("I'm awake, but I'm going into deep sleep mode until RESET pin is connected to a LOW signal");
ESP.deepSleep(0);
}
void loop() {
}
This code puts the ESP8266 in deep sleep mode for an indefinite period of time. For that, you just need to pass 0 as an argument to the deepSleep() function:
ESP.deepSleep(0);
The ESP will only wake up when something resets the board. In this case, it’s the press of a pushbutton that pulls the RST pin to GND.
When you press the pushbutton, the ESP8266 wakes up, does the programmed task and goes back to sleep until a new reset event is triggered.
Measuring current
When the board is in deep sleep mode, measure the current consumption with a multimeter to see how much power it is draining.
Here’s how you should place your multimeter probes.
When the ESP-01 is in deep sleep mode it’s only using 0.3mA which is approximately 300uA.
Keep in mind that during normal usage with Wi-Fi the ESP8266 can consume between 50mA and 170mA.
Wrapping up
Now that you know how to use the deepSleep() function, your battery powered project can last longer.
You may also like other ESP8266 projects:
- ESP8266 Wi-Fi Button – DIY Amazon Dash Button Clone
- ESP8266: DHT Temperature and Humidity Readings in OLED Display
- Hack a PIR Motion Sensor with an ESP8266
- How to Use a Multimeter – Beginner’s Guide
We have other guides about deep sleep that you might be interested in:
- MicroPython: ESP32 Deep Sleep and Wake Up Sources
- MicroPython: ESP8266 Deep Sleep and Wake Up Sources
- ESP32 Deep Sleep with Arduino IDE and Wake Up Sources
This is an excerpt from my Home Automation using ESP8266 eBook. If you like ESP8266 and you want to learn more about it. I recommend downloading my course: Home Automation using ESP8266.
I hope this guide was useful. Thanks for reading!
Updated July 29, 2019
This is best information from you.
Thank you RNT.
Many thanks from Russia 🙂
You’re welcome 🙂
Great articles. Thanks for sharing!
Steve
Hi Steve.
Thanks 🙂
Hai nice tutoring. I have questions.Can I use deep sleep for 5 minute?
Hi. Yes, you can use deep sleep for 5 minutes.
Regards,
Sara
You state the to upload the code to the board the wire to the RST pin has to be disconnected. But if you’re using OTA to upload the code then you don’t have access to the board to manually disconnect the wire to RST. Is there a way to implement recovery from deep sleep when using OTA?
Thanks 🙂
Thank you for wonderful explanation.
In your explanation, you mentioned ESP-01 its hard to solder the GPIO16 to Reset.
However in Live example you have used ESP-01 to show connect circuit and, I did not figure out how you managed to solder the GPIO to Reset? (I notice you had a Green PCB below the ESP-01 though, not sure if that did the trick)
Question is: Can you advise how one can use ESP-01 with deepSleep()? If it really needs the Solder then what is trick to do it?
I managed to solder a very thin wire to GPIO 16, and that’s the only way to do it… At least I don’t know any other tricks to do it. (The green PCB only made the ESP-01 breadboard friendly..)
Hi, I am using the code with a NodeMCU board, and I have a consumption of 11m [A] in deep-sleep mode and I do not know why I can´t have 20 u [A].
Hello Luciano,
The ESP8266 chip might consume 20 uA, but the NodeMCU board has many passive components that consume a lot of current, so it has a bad performance. For a real world solution, you should use the ESP8266 chip in a custom PCB board to lower the power consumption.
Regards,
Rui
It seems like this should have been stated clearly near the beginning of the article. A bit misleading for some people.
Hi Dave.
Yes, you are right.
I added a note at the beginning of the tutorial.
Regards,
Sara
300 μA isn’t it too much? It should be near 20 μA. Probably not accurate multimeter or fake chip
You are talking about the ESP8266 as a standalone chip consuming 20 μA. However development boards have passive components that use more current…
300µA is consumed by the 10k resistor,replacing by a 220k drops current to 30µA
Great tutorial, many thanks.
Plamen
P.S. You have reversed the terminals of the amp-meter. Proof: from the power source follow its positive terminal, which is connected to a red wire, which then is connected to the black lead of the amp-meter, which is connected to the common (negative) terminal on the amp-meter. Therefore the current will flow through the amp-meter from its negative to its positive terminal, which is in reverse. A digital amp-meter will show
(cont) a digital amp-meter will show a negative number, an analogue one will show its needle struggling to turn left beyond the zero mark and/or pushing against the small vertical pin just left of tbe zero mark.
Apologies for the interrupted post (fingers trouble).
Hi Plamen.
You are right. We’ve switch the red probe with the black probe.
In the case of a digital multimeter, the only difference is that we see a negative number.
But it can be problematic using an analog multimeter, you are right!
Thank you for noticing that and sharing it with us.
Regards,
Sara 🙂
The article should at least mention the options for further power savings such as removing LEDs etc.
Hi.
That’s true. Removing the LEDS will save some power. Thank you for your suggestion.
Regards,
Sara 🙂
When I run the sleep routine it crashes after the set time (prints out garbage and then restarts) this is both on an Node MCU and an ESP-01 with the hardware modified. Any comment?
Can we also deep sleep arduino mega or not?
Hi Dear
In this article, you say:
“Now, when I press the pushbutton the ESP wakes up does some action, in this case it prints a message in the Arduino IDE serial monitor. After a few milliseconds it goes to Deep Sleep”.
The way I programmed the ESP8266 – 01 is by using an FTDI to do the communication. To do this, it requires some cable connections between them. I have done that in my breadboard and code was downloaded successfully on the ESP01. In a second breadboard, I built the circuit with the push button, which seems to work ok (every time I push the button, the light flashes). But I have 2 problems:
1- How am I gonna check on the Serial Monitor if ESP is not connected to my PC?
2- Im using a Li-Ion Battery 3.7v to power the circuit. I used my digi muiltimeter to measure the current. It shows 22mA. I think this is way to high.
Hi Diego.
if your ESP is not connected to your computer, you can connect an LED and add a few lines of code to blink the LED when the ESP wakes up. This way you can keep that that woken up.
It shows 22mA in deep sleep mode?
Regards,
Sara 🙂
Hey,could you please guide me here…I want my nodeMCU to go to deep sleep just after connecting to client as a server and send data to it…….when i get it to practice,and go for web page formed ,my esp goes to deepsleep and i cant get any data!!!!
now,is there a way in which i can use both the properties of nodeMCU,ie. i want it to connect to server then send data then go to deep sleep
Hi Sabi.
Are you using the same ESP to send data and host the web server?
When the ESP is in deep sleep you can’t access the web server because it is sleeping, so it can’t respond to client requests.
Regards,
Sara
hiii
we want code for esp8266 with temperature sensor(ds18b20)
we want some feature in our code
if you interested to write sketch for us
please inform us with your quotation
thanks
Hi.
We don’t write custom code.
You can take a look at the following tutorial to learn how to use DS18B20 with ESP8266:
https://randomnerdtutorials.com/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide/
Regards,
Sara
Thanks very much: I want to do some battery-operated temperature sensors, and needed info on how to do deep sleep. This is just what I needed (including, alas, the fact that the soldering to use my ESP-01s is too fiddly for an old through-hole guy. 🙁 Ah, well: ESP-12s are cheap now…).
Hi Ran.
You can use an ESP8266-12E chip with an adapter (or with your own PCB). It consumes very low power in deep sleep mode.
Here’s the links for the ESP8266-12E and adapter:
– https://makeradvisor.com/tools/esp8266-12e-esp-12e/
– http://bit.ly/2Hbz6Ei
I hope this helps,
Regards,
Sara
Hi Rui….why do you measure 300 micros instead of 20? Is this due to the 10k resistor?
Have you tested up to 1 Mohm?
Hi Marcelo.
I’m sorry but I didn’t understand your question.
Hi Sara,
I have ESP8266-01 module and Arduino UNO.
I have connected VCC and CH_PD of ESP8266 with 3.3V of Arduino UNO, GND with GND and RX and TX of ESP8266 with Pin 4 and 3 respectively of Arduino UNO. They both are working accordingly.
Now to enable deep sleep mode:
1. After the soldering of GPIO16 and RST, is there need to install ESP8266 board on IDE or by just simply writing the code will work as I have already connected ESP with Arduino IDE.
2. Except soldering and NodeMCU, by just writing the code deep sleep mode work?
3. Is there need to include library of ESPWIFI.h in my code?
Hi.
Why are you connecting ESP8266 to your Arduino?
To upload code to your ESP8266 you need an FTDI programmer or a serial adapter like this: https://makeradvisor.com/tools/esp8266-01-serial-adapter/
Otherwise uploading this code to your arduino will not work – you would need to send commands via serial port (which is not covered in this tutorial).
To make the ESP8266 work with Arduino IDE you need to install the ESP8266 add-on: https://randomnerdtutorials.com/how-to-install-esp8266-board-arduino-ide/
I hope this helps.
Regards,
Sara
what’s the purpose of using the Resistor in the above circuit?
Will it not short anything?
Hello
I am working on ESP 8266 and I have a doubt in the deep-sleep function along with auto-connection, I saw that on the site that has an example with auto-connection but and providing the SSID and password of the WIFI network.
Could you help me get an idea
Thank you very much in advance
Hi Rafael.
I’m not sure if I understood what you really need.
But I think this example will help: https://randomnerdtutorials.com/wifimanager-with-esp8266-autoconnect-custom-parameter-and-manage-your-ssid-and-password/
Regards,
Sara
Hi Sara. Is there any similar way that will allow me to wake up?
The problem is I have only 3 seconds for the reed-switch and I need to connect and send web request. So not enough time even with static IP. Thank you in advance. Majo
Hi Majo.
Instead of deep sleep, you can add an auto power off circuit to your ESP8266. The ESP8266 will only cut power after the task is executed.
You can follow these projects:
https://randomnerdtutorials.com/latching-power-switch-circuit-auto-power-off-circuit-esp32-esp8266-arduino/
https://randomnerdtutorials.com/power-saving-latching-circuit/
Regards,
Sara
Ah, I just spotted it. I don’t have any ESP-01s so I didn’t know they had a “power on LED”. That’s why your minimum current is ~ 300uA. A red LED’s forward voltage is ~ 1.8V, so (3.3V – 1.8V) / 4700 (the current limiting resistor) = 313uA. It looks like it’s at the limit of your meter’s range, so 333uA (including the 20uA) is shown as 0.3mA. My Fluke has lower ranges. 🙂 One of the other Espressif docs showed the 20uA Deep Sleep current as being at 3.6V and I was running my D1 Mini a hair over 3V, so 16uA for me.
can you show the exact circuit you use to have a 16uA?
I’ve see also that there is a ‘sleep’ contact behind the d1 mini, what is it for ?
Thanks!
If you want to play with some other low-power modes, the Low-Power demo is released, https://github.com/esp8266/Arduino/tree/master/libraries/esp8266/examples/LowPowerDemo
It includes the elusive Timed/Forced Light Sleep that took *lots* of googling to figure out how to engage. Forced Light Sleep only sleeps at ~360 to 380uA, but it recovers with an interrupt in less than 5ms, compared to the 120ms+ that Deep Sleep takes to boot, and your program doesn’t have to start from scratch afterwards.
Rui and Sara,
Loved the video. However, you missed an important use case that I have been struggling with. I have a Feather that I want to put into deepsleep with a switch. The switch is too be used to turn both on and off. In the above ESP.deepSleep(0) code, the switch is only used to turn back on.
While you might say just use the EN-GND pin (https://io.adafruit.com/blog/tip/2016/12/14/feather-power-switch/), I don’t know of a way to run code before the power is turned off, which is needed.
Can you provide any suggestions? Is there any documentation I can look at. I have had no success in finding anything in my own searches.
Thanks in advance.
Hello,
I have a very important decision to take based in the wake up from deep sleep.
If ESP32, or ESP8266 can wake up from deep sleep based on RX pin status, then i use ESP. If not i have to use a regular transceiver.
What is all about. I have an RFID reader connected to an NodeMcu – ESP8266 RX pin.
When i read a card, i send the numbers of the card to a second ESP.
I must use Deep sleep for ESP board as it is powered by batteries. I must wake up the ESP only when i do a read with the RFID reader, and this transmit the ID of the card to ESP RX pin.
Between that, full deep sleep. I do not need RTC, no Wifi, no nothing. Just the ESP sleeping.
Please help me on this.
Thank you
Hi.
I answered your question on the other post: https://randomnerdtutorials.com/esp32-deep-sleep-arduino-ide-wake-up-sources/
Regards,
Sara
Thank you Sara
great post.
ESP.deepSleep(0) is misleading as the ESP8266 cannot sleep for more than ~3.5h at the time. I think you should mention that. Great article otherwise!
See thingpulse.com/max-deep-sleep-for-esp8266/ for details.
Ha, that would have been my question 🙂
As I understand it, deepsleep(0) puts the DSP8266 to sleep forever or until a reset. Your reference discusses deepsleep max which is not the same.
Managed to solve? I have the same problem
Sara, this is a great article, thx.
Do you have some advice how to handle deepsleep with connecting Bluetooth devices?
Hi.
Unfortunately, we don’t have any tutorials covering that subject.
Regards,
Sara
Have you considered a tutorial on waking the ESP8266 from deepsleep(0) using the DS3231 Alarm (SQW)?
Hi
You put a lot of effort into these tutorials – thank you.
I’ve got a Wemos ESP8266 board with an integrated 18650 battery. I want to use it to monitor the oil boiler (input and output temperature and burn time) which has no mains nearby other than the demand.
I’d like to be able to put it into deep sleep to be woken up when it gets power through the micro USB charging input (ie when the demand mains power is back on).
My only thought is to use a transistor to draw the RST down to ground when power is applied. Some research and fiddly soldering to get to the signal i need and the RST pin which is not on the header.
Any advice would be appreciated.
Thanks
Maybe i misunderstand your requirements but I think you should be able to to use the transistor to connect and disconnect the GND of the ESP. That way it just powers off when there is no charge on the demand. And it powers back up when the demand switches on. Ofcourse you do need to put it to sleep when done so uses less power.I guess it’s not needed, but If you want the ESP to reset itself periodically during the demand is on. Then you could can solder GPIO16 to reset.
Lauren – Thanks for replying.
I really want the unit to be running all the time and reporting instantly when the burner is turned on or off. Also i want the temperatures reported every minute or so. Reporting is over WiFi to an MQTT broker and Home Assistant. When the boiler is off for a while temperature reporting can be at a greater interval.
I was thinking I could wake up every minute to report temperature but wanted the appliance of power to also wake the unit up to give more accuracy to burn timings (which I will equate to oil consumption in Home Assistant).
The battery will keep the thing going for over a day if i use modem sleep but I’m not sure if the charge will be sufficient in the summer when the boiler is only used for hot water. Still considering my options.
My board has an LED on GPIO16 and it does not wake the unit up from deep sleep (I’ve just found this out). The LED does comes on at the end of the sleep period, but when connected to the RST it goes off again but does not restart the sketch.
Maybe I’ll just let the battery run out when no meaningful measurements can be taken and the unit will restart pretty much as soon as the power is applied. Just doesn’t seem very professional.
Aha yes that makes sense.
Having both the timer and a external wakeup is exactly what i wasn’t able to get working either.
In my most recent project i used an ATTINY13 for that purpose. I programmed the Attiny to wakeup when it gets an interrupt (in my case from a PIR Sensor) and i also programmed a watchdog so it always wakes up after 2 minutes of sleep. When the Attiny wakes up i put a GPIO to a transistor HIGH and then connect the ESP GND that way. I forward the input of the Interrupt PIN via a GPIO to the ESP so it knows if the PIR (Could be your “demand” input) was high or LOW.
First step in ESP is to put a GPIO from ESP to ATTINY to HIGH so it knows the ESP is awake. After the sketch of the ESP is done, just before deepsleep (not sure if it’s really needed) i put that GPIO to LOW so ATTINY knows it can go to sleep and therefor it will also disconnect the GND of the ESP again.
Attiny is more efficient in Deepsleep than ESP so it’s also a battery saver.
I’ve done some testing and coding using light sleep (which I can waken with other GPIOs).
Running it consumes about 50mA and in light sleep less than 10mA. So with the right duty cycle I get the average current about 10mA. The battery will keep it going for 12 days. It charges at about 0.5A so it only needs to be powered for 30 minutes each day (250mAh). I should get away with that in the summer.
Now to code for real! Nothing else to do during lockdown!
This is a great article. I wanted to know if the code will allow for a wake up to occur after a set amount of time and for an external button to wake up the esp8266 from sleep. In short, I want to be able to have the esp8266 periodically report that it is living as well as be able to be woken (maybe reporting its battery strength) and via the external button.
I also wanted to do that. My trial code told me it was one or the other but not both. I’d be interested still if there is a way.
Is it possible to simultaneously connect the button and gpio16 with rst to end deep sleep with a timer or a button?
I would like to use this setup for knowing when my mailbox is opened. The problem is that in such a setup, the mailbox is closed most of the time. If a reed sensor is used, we need to know when the reed sensor has its magnet taken away from it as opposed to when a magnet is brought near the reed sensor. Can you provide guidance on how this can be done?
Hhmm, don’t these reed switches come in normally-closed or normally-open configuration?
these one have both amazon.nl/EsportsMJJ-stks-Schakelaar-Magnetische-Switches/dp/B075F3KG2K/
Hi Everyone,
Could someone please point me to a reference regarding the ‘e6’ for microseconds?
I don’t know whether it’s an ‘esp’ thing or Arduino IDE or ???, but I can’t find out.
It’s just because I want to be able to pass (parse?) XX as a parameter to a function with switch() statements.
— I could simply use longhand microseconds – but I am curious.
Alternatively, how does one pass ’20e6′ as a usable parameter value?
TIA,
Chris
I don’t know what you are referring to but it’s probably the usual notation for x 10^6.
ie 20e6 is 20,000,000. So if microseconds, that’s 20 seconds.
https://en.wikipedia.org/wiki/Scientific_notation#E_notation
Aaaah!
Of course, it’s that E ! I should have realised.
I did have a deep memory of E… but after 30 or so General Anesthetics in my deep past, had misplaced it …
@Marcel: Thanks for the link. Now I can sleep better 😉
In the article above:
[qoute]To put the ESP8266 in deep sleep, you use ESP.deepsleep(uS) and pass as argument the sleep time in microseconds.
In this case, 30e6 corresponds to 30000000 microseconds which is equal to 30 seconds.[/quote]
I’m simply trying to find the original specs of the “e6” reference. I’m guessing it’s an Espressive thing, but where…?
But instead of 30e6,
… would not be better to use micros() or, delayMicroseconds(30);
… or, is the ESP.deepSleep() function fussy?
Hi everyone.
I used this toturial and I have strange problem!
After the code uploded I recive this text from serial monitor
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 3584, room 16
tail 0
chksum 0xb0
csum 0xb0
v2843a5ac
~ld
m)!⸮⸮%⸮⸮Y
‘I⸮⸮q⸮P⸮⸮@ʎ⸮⸮%k@5$VQ⸮
in 74880 baudRate!
any suggestion?
Hi team,
using the example code on an ESP8266 in VScode IDE I notice every time the processor wakup it stucks into boot process. Moving ESP.deepSleep() into loop() dosn’t help.
Anny suggestions how to get out of the blocking situation?
#include <Arduino.h>
void setup() {
Serial1.begin(115200);
Serial1.setTimeout(2000);
while(!Serial1) { }
//Serial.printf(“\n\nCompiled from: %s at: %s %s”, FILE, DATE, TIME);
//Serial.println(“\nESP8266_deep_sleep-timer-controlled”);
Serial1.println(“\n\nback from sleep mode, normal performing 5 sec”);
delay(5000); // do whatever you want here
Serial1.println(“now going into deep sleep mode “);
ESP.deepSleep(5e6);
}
void loop() {
// ESP.deepSleep(5e6);
yield();
}
Thankyou
Hi,
Thank you for the advice. I went through the 8266 / BME280 / Thingspeak weather station and it drains a lot of battery, even with longer intervals. So I tried to insert the deep sleep command at the end in the sketch, moving the void loop after it, and removing the original delay time. I only works for one or two times and and then stop. Am I doing anything wrong?
#include <ESP8266WiFi.h>
#include “ThingSpeak.h”
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
const char* ssid = “xxxxxxx”;
const char* password = “xxxxx”;
WiFiClient client;
unsigned long myChannelNumber = xxxxxxxxx;
const char * myWriteAPIKey = “xxxxxxxxxxxx”;
unsigned long lastTime = 0;
unsigned long timerDelay = 1800000;
float temperatureC;
float humidity;
float pressure;
float volt;
float bateria;
String myStatus = “”;
Adafruit_BME280 bme; //BME280 connect to ESP8266 I2C (GPIO 4 = SDA, GPIO 5 = SCL)
void initBME(){
if (!bme.begin(0x76)) {
Serial.println(“Could not find a valid BME280 sensor, check wiring!”);
while (1);
}
}
void setup() {
Serial.begin(115200);
initBME();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("\nConnected.");
}
temperatureC = 1.034 * bme.readTemperature() - 2.8112; //calibrada
Serial.print("Temperature (ºC): ");
Serial.println(temperatureC);
humidity = 0.95 * bme.readHumidity() + 7.26; //verificar calibracao
Serial.print("Humidity (%): ");
Serial.println(humidity);
pressure = bme.readPressure() / 100.0F;
Serial.print("Pressure (hPa): ");
Serial.println(pressure);
volt = analogRead(0);
bateria = volt * 0.0029492188 * 1.3576;
Serial.print("Bateria (V): ");
Serial.println(bateria);
ThingSpeak.setField(1, temperatureC);
ThingSpeak.setField(2, humidity);
ThingSpeak.setField(3, pressure);
ThingSpeak.setField(4, bateria);
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
Serial.println(“going into deep sleep mode”);
ESP.deepSleep(6e8);
}
void loop() {
}
Hi.
What happens exactly when it stops working?
Regards,
Sara
Hi Sara, thank you for your response!
The string of funny symbols appears, as usual, followed by the message “Attempting to connect”, and then stops completely.
Hi.
I think this discussion is about that issue: https://forum.arduino.cc/t/trouble-with-wifi-connection-on-esp-8266-after-deep-sleep/512782/5
Try one of the suggestions and see if that solves the problem.
I hope this helps.
Regards,
Sara
Great article! Followed it to the letter, and everything is working as needed. Except for when I power up the NodeMCU initially, it needs a push of the physical RST button and then everything runs and cycles correctly. The problem is, that the NodeMCU and the DHT22 are in a 3d printed case without access to the reset button. I’m using a wire from D0 (16) to RST pins and using the timer (not a push button). Any ideas what is causing that initial boot not doing anything (nothing on the serial monitor either) until pushing the RST button? Or a workaround? Thanks!
Please disregard my previous comment. Once I unplugged it from my PC USB port and plugged into regular wall outlet, it started working once starting without having to hit the reset button. Strange!
Hi !
Thx for this awesome tutorial ! I do have a question though… I built the circuit for the external wake up with a node MCU board and a button as an interrupt. It does work but not exactly as I’d like it to. The board actually wakes from sleep when the button is released, not when it is pressed ? My need is to be able to detect when water reaches a certain level (flood alert) and I was thinking of just putting 2 leads next to each other and hoping to wake the node MCU when the water connects these two leads (that would be when the button is pressed in on my board) but the way this works seems it would only wake up after the water level has dropped. Has anyone done anything of the sort before ? Thx for any hints !
Hi,
In my last project with Wemos D1 mini, I use deep sleep with wake up pin connected to reset, using the function ESP.deepSleep(xxxe6)…
Everything works fine except one thing: during the restart, the digital outputs go to 1.5v for a moment, even if they are not controlled by the software.
It can activate the mosfet I will connect to one of these outputs.
I put in the setup () digitalWrite (x, LOW), but I have not solved.
The only way I find is to connect a 10uF capacitor…but I don’t like this solution because I will use the output with PWM analog write..
Do you know this behavior? Do you have any idea how to fix it?
Hello Sara,
In the tutorial you mention that RST has to be connected to GPio16 AFTER uploading the example sketch. I missed that line the first time experimenting. I guess that for the ESP-01 this would mean: ‘soldering after uploading’. Maybe you can expain the reason somewhat more.
A WARNING:
There are batches of ESP8266 chips for sale that do not recover from deepSleep(). The example below is from the CheckFlashConfig sketch with the sleep command added:
Serial.println(“Going to deep sleep.\n”);
ESP.deepSleep(10000000);
This is what you see after the first boot:
… removed a few lines…
Flash ide size: 4194304 bytes
Flash ide speed: 40000000 Hz
Flash ide mode: DOUT
Flash Chip configuration ok.
Going to deep sleep.
… ten seconds later it should boot but …
ets Jan 8 2013,rst cause:5, boot mode:(3,6)
ets_main.c
There is nothing one can do about it, and the issue has been confirmed by others. I have been using plain 8266 chips (not DEV boards). Everything else seems to work more or less, so it may be just a bad batch.
Thanks for sharing.
I have a routine that makes my ESP8266 (wemos D1 mini) going to deep sleep afetr checkinh wifi spots. It works well, but now I can’t upload any new sketch from Arduino IDE : esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
Do you know any way to correct this ?
Hi.
If the board is sleeping, you wont be able to upload new code.
You need to press the RST button and upload code right after to catch it awake.
Regards,
Sara
also, need to disconnect (eg unsolder) the “sleep” connection points on the ‘back’ of the board if you have connected them
Hello! I am trying to change this code to work for a vibration sensor instead of a button. I’ve copied the code but I am still having issues. I am following the schematic as close as I can, which means I’ve plugged in the sensor’s VCC cable to the RST slot instead of GPIO 16. How can I make this work? Do I need a signal inverter?
I am replying to my old comment. I was able to figure it out. The problem was I was using a vibration sensor instead of a button. The vibration sensor sends a HIGH when triggered, but for wake from deepsleep to work the board needs to receive a LOW. So, to turn the HIGH into a LOW I needed to use an inverter to convert the HIGH to LOW. Now it works!! More details here:
https://old.reddit.com/r/esp8266/comments/w49f28/was_finally_able_to_convert_my_high_signal_to_low/
Great!
Thanks for sharing the solution.
Regards,
Sara
Just to clarify, waking from a deep sleep is a reset, so the code begins again at the setup(). Have I understood that right?
In which case, is there any way to run a counter to count how many sleep cycles it has been through?
Hi.
Yes.
You need to save a variable in the flash memory/eeprom to keep track of the number of sleep cycles.
Regards,
Sara
Hi
Running your example (below) with the ESP8266-01S, it does indeed deep sleep for one
cycle but will not recycle (go back to sleep) after that. I understand the -01S has GPIO16 connected to Reset. Current drain after the first wake up cycle continues at about 11ma. I would expect it to wake up and then repeat the cycle. No?
If this is normal, how would I make it recycle?
Gratefully
Will
/*
* ESP8266 Deep sleep mode example
* Rui Santos
* Complete Project Details https://randomnerdtutorials.com
*/
void setup() {
Serial.begin(115200);
Serial.setTimeout(2000);
// Wait for serial to initialize.
while(!Serial) { }
// Deep sleep mode for 30 seconds, the ESP8266 wakes up by itself when GPIO 16
//(D0 in NodeMCU board) is connected to the RESET pin
Serial.println(“I’m awake, but I’m going into deep sleep mode for 30 seconds”);
ESP.deepSleep(30e6);
}
void loop() {
}
Hi,
I m trying to put my Node mcu into deepsleep and then have it awoken by movement detected by a PIR sensor. While I can put the Node mcu into a time delayed deep sleep and have it awake I cannot seem to figure out how to incorporate the signal from the PIR. I am using an interrupt function and tried all combinations of RISING, FALLING etc but nothing seems to work. Perhaps it is not the coding and I need to attach wires in a different manner or perhaps it is not possible to do what I am trying to do?
Hi Sara,
thanks for your useful tutorial.
I’m asking for your help because I’ve tried to run the code with the external wake up using a S1 mini v 4.0.0 based on ESP 8266.
It has a pin marked as RST just close to a pin marked A0 (Sleep) that I was not able to find what it is for.
However, I connected the pushbutton to RST pin with a jumper and with a 10K resistor as shown in your drawing of the circuit, but when I put someting in the loop, such as
Serial.println(“BUTTON PRESSED AWAKEN AGAIN!!”);
delay(500);
just to verify that the MCU awakes after the button pressure, nothing happens and I think that ESP8266 is still sleeping.
I don’t undesrtand where my mistakeis. Would hou help me ?
Thanks
Hi.
Try to connect the RST button directly to the GND and 3V3 pin and see if something happens.
Maybe you don’t have the button connected properly…. or the RST pin on your board is not actually connected internally (in that case, it would be an issue with the hardware)
Regards,
Sara
Hi,
Where does ESP8266 start from when running the program again after deep sleep? From the very first line on entire code (like after a reset) or from the beginning of loop() function? How about diffrent variables, are they preserved after deep sleep?
Thanks!
Hi.
It starts running from the very first line, like after a reset.
Regards,
Sara
you need a reset PULSE to gnd to wakeup, if you keep the reset low it will not start
Also, need a resistor of 470R between gpio 16 and reset button to gnd , as the reset button will short gpio 16(which is normally HIGH ) to gnd.
The chip enable must not let open, floating, needs a resistor of 10k and capacitor of 100nf , see hardware design reference expressif.
espressif.com.cn/sites/default/files/documentation/esp8266_hardware_design_guidelines_en.pdf
ESP-NOW and DeepSleep
In my case, I need to send pool temperature to the server each minute. For the pool, I will not use power supply for security reasons.
To powering with batteries, we need to limit the current and time needed to run our application.
Using the minimum hardware possible.
Using ESP-NOW and not WIFI because ESP-NOW transmit data without the need of maintaining permanant WiFi connection.
Setting the ESP in deep sleep mode for a maximum of time, waking up only for getting the data and transmit.
I choosed ESP12F because in all other development bords, only the ESP8266 can be set in deepsleep mode, but USB to serial device stay powered.
I uses a pack of 4 AA batteries and a voltage regulator (MCP1702 3.3 volts: dropout = 0.75V, quescient current = 2 uA )
To keep 3.3V on the ESP8266, the batteries voltage must be 3.3V + 0.75V = 4.05 Volts minimum.
The total current in deep sleep is now 12 uA.
After powerup, the code need to make a pairing with the server, this takes less 2.5 seconds running time.
Searching 1-wire bus
End of search : 18ms
Setup done
Pairing request on channel 1
Pairing request on channel 2
Pairing request on channel 3
Pairing request on channel 4
Pairing request on channel 5
Pairing done on channel 6 in 2333ms for 78:e3:6d:09:fc:89
Send data of device 1
I'm awake from 2366ms , but I'm going into deep sleep mode for 10 seconds
After wakeup, the running time is less then 350ms
Searching 1-wire bus
End of search : 18ms
Setup done
Pairing request on channel 5
Pairing done on channel 6 in 285ms for 78:e3:6d:09:fc:89
Send data of device 1
I'm awake from 318ms , but I'm going into deep sleep mode for 10 seconds
To perform a test in a reasonable time, I make tests with a sleep time of 10 seconds and, after 1 month, the battery voltage is about 4 volts.
So, if I need a transmission each minute, the 4 AA battery can do the job at least for 6 months (in Canada, we don’t have 6 months of summer time!).
Thanks for sharing your results.
Regards,
Sara
I want to develop a project with the ESP8266 E12 nodemcuv3 module. I will use both GPS module and accelerometer module in this project. However, I could not decide on the battery that would be sufficient for my device for 2 days. I would be glad if you help. Thank you for your time…
Dears,
i want that my ESP8266 NodeMCU wakes up after 6 or 7 minutes, reads the current measurements of temperatur and humidity from a DHT11 and send them via MQTT to my ioBroker – something exactly you described above.
So my question as a rookie is:
Should i put the parts (or functions, let’s see) waking up, connecting to WiFi and MQTT server, reading and sending measurements and going back to sleep all in the setup() or loop() part of the sketch?
Hi.
It doesn’t matter.
You can put everything on the setup() because it will only run once.
But, you can also put it on the loop() even though it will just run once (just make sure the deep sleep function is just called at last).
Regards,
Sara