ESP8266, Deep Sleep mode test, wake up with a PIR motion detector

The ESP8266 module has the deep sleep mode which allows to put it in hibernation to save the battery. You can wake up the module at regular intervals to make measurements and publish them on a server. The RESET pin (RST) is also used to wake the ESP8266. For example, the rising edge (the signal) of a motion detector (PIR) can be used. In this tutorial we will see the adaptations to the Arduino code and how to modify the wiring.

Different sleep modes and power consumption of an ESP8266EX module

There are three ways to sleep. They are detailed in the official documentation page 6

  • Modem Sleep. Modem-Sleep mode is used in applications that require processor operation, as in PWM or I2S applications. It complies with 802.11 (U-APSD). The Wi-Fi modem is stopped while maintaining a Wi-Fi connection without data transmission to optimize power consumption. Three modes are available, DTIM1 to 3. For example, in the DTIM3 mode, the modem is silenced for 300 ms and then activated for 3 ms. With this mode, the consumption is 15 mA.
  • Light-sleep. In addition to suspending the WiFI modem, the CPU is put on hold and reactivated if necessary.
  • Deep-Sleep is the single most simple mode to implement and the most energy efficient for projects that run on battery.

The table from the official documentation gives a little idea of ​​consumption. When the deep-sleep mode is activated, the consumption drops to ~ 20μA.

esp8266 table deepsleep datasheet espressif

The power consumption of the ESP8266EX (2018) modules is very similar (page 18). Consumption climbs to 170mA with a WiFi connection at 11Mbps.

esp8266ex 2018 table deepsleep datasheet espressif

How to activate the deep-sleep mode

Deep sleep mode is activated by connecting the RST pin to the D0 pin. Pin D0 is therefore no longer available for another use. On some modules, pin D0 can be identified by GPIO16

wemos d1 mini activate deep-sleep mode esp8266

 

Other consequences of the deep-sleep mode

By connecting the pins D0 and RST, it is no longer possible to upload a program. Whatever your development environment (Arduino IDE, PlatformIO, ESP Tools), you will need to disconnect the RST pin before you can upload a program. This must be taken into account when designing your circuit. The best is to plan a jumper.

As the WiFi connection is stopped, seeing will no longer be able to access a configuration WEB interface. It will be necessary to provide a condition in your program to disable the deep-sleep mode. For example by adding a micro switch on the circuit.

Enable Deep-Sleep Mode in an Arduino Program

Now that everything is ready hardware side, we will tackle the Arduino code.

The thing to keep in mind is that when you wake the ESP8266 module, the project code is executed from the beginning. This is not a standby as is usual on a computer. Therefore, it is useless to torture one’s mind and to foresee cases of re-connection. It will be enough to execute the various processing in the setup () then to put back the ESP by executing the method ESP.deepsleep (duration, mode_wifi) which takes two parameters:

  1. Standby time in microseconds
  2. WiFi mode at restart

If you want to wake the module manually or with an external action, for example a switch connected to the RST pin, just indicate a zero time (0).

On waking, it is possible to choose between 3 modes of operation of the WiFi modem.

  • RF_DEFAULT is the default mode that you do not need to specify. The equivalent is RF_CAL. In this mode, the WiFi modem is enabled normally. That is to say that we can connect to the WiFi network and publish a status on an MQTT broker. This mode consumes a peak consumption that can reach 170mA for about 1 second.
  • An alternative version that consumes less energy is RF_NO_CAL (for NO CALibration). The WiFi modem is activated but no calibration of the radio signal is performed. In theory only because according to the measurements made by Andre, the consumption seems identical.
esp8266_boot_no_rf_cal

RF_NO_CAL

esp8266_boot_rf_cal

RF_CAL

  • RF_DISABLED. If you do not need the WiFi connection you can keep the WiFi modem in standby. Attention, if this mode is activated, it becomes impossible to connect to the WiFi network. This is the equivalent of the modem-sleep mode described above.

So in summary:

  • ESP.deepsleep(0), suspends the module until it is woken up by a spike on the RST pin
  • ESP.deepsleep(5 * 1000000), wake up the module every 5 seconds
  • ESP.deepsleep(5000000, RF_DISABLED), wakes up the module every 5 seconds without re-activating the WiFi modem

ESP8266EX Deep-Sleep Mode Test

It is time to move on to a concrete example. Create a new sketch and paste the following code. You have several parameters

  • SSID, the name of the WiFi network to which the module should connect
  • Password, the network password
  • durationSleep, the duration of the module. Here, it’s 10 seconds.
  • NB_TRYWIFI, the number of attempts to connect to the WiFI network.

I used the Tricker library that comes with the ESP8266 SDK, which allows you to run a function at regular intervals to flash the blue LED on the Wemos d1 mini. It is a lighter library than the Thread library that does not require any additional installation.

The module tries to connect 10 times with a wait time of 500ms between each attempt. If the module can not connect to a network, it is in deep sleep until the next alarm clock and so on. This is a solution to avoid draining the battery. Of course, the duration of sleep is too short, but, it is for the example.

When connection is possible, the IP address of the module is displayed on the serial monitor.

This mode of operation can be used to make measurements at regular intervals and send them to an InfluxDB server or database.

Wake the module using a contactor, PIR motion detector, microwave radar …

Now, what can be interesting is to wake up the module when an event is detected using a contactor or a switch, an infrared motion detector (PIR detector) or a microwave radar.

The problem is that to wake the ESP8266, you have to send a single rising edge to the Reset pin. When using a switch or the signal of a PIR detector, the signal sent is not correct and it causes a multitude of restarts of the module. The peak power consumption is at the start of the module. If we want to avoid waste and save the battery from unnecessary energy costs, we must solve this problem.

I have tested many montages, but it is the one proposed by Tomsim on Stackoverflow that gives the best result.

esp8266 capacitor wakeup circuit npn transistor

Here, Tomsim also retrieves the state of the button from pin D2. The RST pin is connected to pin D0 with a diode. It is not mandatory. Avoid sending a high signal from pin D0 to avoid an unintentional reset. If you want to manually restart from the program the Arduino module, use the ESP.reset() method instead.

Depending on the sensor used, here are two circuits that you can test.

Equipment used

Here is the material used for this tutorial

Last update was on: 20 July 2018 8 h 03 min

Circuit with NPN transistor (2N2222, BC457 …)

The first circuit is the simplest. An NPN type transistor will be used to send a signal to the RST pin as soon as a signal is sent by a present sensor or a mechanical switch.

The signal is recovered on the basis of the NPN transistor (pin 2). The signal from the transmitter (pin 1) is recovered on the RESET pin. You can add a reminder but that does not seem obligatory. The collector (pin 3) is connected to the GND.

BC547 transistor npn esp8266 deep-sleep wakeup

 

Necessary material

pir motion sensor wakeup deep-sleep esp8266 npn transistor

Circuit with filter capacitor upstream of the NPN transistor, in case of multiple reset
If you have multiple reboot problems with the ESP8266, you can test this second timeline based on the Tomsim principle.

The trick is to install upstream a small capacitor of 1μf then connect the output of the capacitor to ground through a resistor. When a signal is sent by the contactor or the sensor, the capacitor charges and produces only one output signal to power the base of the transistor. Residual energy is removed via the resistor.

The rest of the circuit remains the same. I tested several resistance values from 1kΩ to 10kΩ successfully.

Necessary material

pir motion sensor wakeup deep-sleep esp8266 npn transistor capacitor filter

An extra WiFi MQTT alarm system

Now that you can wake up the ESP8266 with a presence detector, it’s easy to send a status change to a home automation server or MQTT broker. Here is a small example of a program that sends a state change when the ESP8266 module wakes up.

esp8266 deep-sleep pir motion detector wakeup interrupt

 

Before testing the program, change the following settings:
  • The SSID and password of the WiFi network
  • The IP address of the MQTT broker
  • NB_TRYWIFI, the number of attempts to connect to the WiFI network.

Upon awakening, the module attempts to connect 10 times (NB_TRYWIFI) to the WiFi network. If the connection fails, the module goes to sleep to avoid draining the battery. If successful, it connects to the MQTT broker and publishes the state on the DeepSleepDemo / PIR topic for one second. The blue LED is off and then the module goes back to the next motion detection.

One could improve the code and measure the time between two detections of movement to avoid multiple alerts.

Errors when uploading an Arduino program when deep-sleep mode is enabled

Many ESP8266 development board manufacturers now incorporate a soldering jumper to enable deep-sleep mode. It’s cleaner and there is nothing to plan on the circuit. Here the Wemos d1 mini v3.x.x (left) and the Wemos d1 mini Pro 2018 (right).

In both cases, it is very easy to follow the track which connects the pins RST and D0.

 

wemos lolin d1 mini pro bottom

However, this option must be reserved when the project is completed. Indeed, every time we have to update the program, it will be necessary to disconnect the pins D0 and RST. If necessary, remove the jumper using a desoldering pump.

A typical case of error message when the deep-sleep mode is not disconnected.

esp8266 deep-sleep download espcomm_upload_mem failed

If the RST pin is connected to a sensor that can cause a Reset during program installation, you may encounter the FLASH_DOWNLOAD_DATA error.

esp8266 deep sleep flash_download_data

 

Other optimizations

Of course the consumption announced by activating the Deep-Sleep mode does not take into account the rest of the assembly. If you use a sensor that draws power directly from the battery, the range will be directly affected. This is the case, for example, when using the 5V or 3V3 pins.

If you want to reduce the power consumption of the devices, you can use a digital output, provided that the necessary power is low. The Imax current available for each output can not exceed 12mA (page 18).

Another solution is to activate the energy saving mode that is offered by most I2C sensors. This is case by case, you will have to search the bookstore or develop your own driver by referring to the official documentation of the manufacturer.

 

Last update was on: 20 July 2018 8 h 03 min

 

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

DIY Projects