Trials and Tribulations using Deep Sleep on an ESP8266

This is a focused topic and is not really meant to be a tutorial for a new beginner. I won’t be boring you with how to install the Arduino IDE and ESP8266 Boards Library or even why you might want to use Deep Sleep. If you’re here, you probably already know these things. This is merely some miscellaneous ramblings while researching Deep Sleep on an ESP8266. I ran into many solutions that were different from each other and some that didn’t work for me.  Here are some of the things I’ve found that I’ll be using in my outdoor remote sensor projects (more to come on these in the future).  You may find this helpful if you’re trying to use Deep Sleep to extend battery life on a remote sensor/control gizmo. Here are the bullets summarizing what I’ve found… so far.

  • First off, the focus of my exploration is primarily to use LoRa for communications on remote, battery operated sensors.  I will not be using the WiFi on the ESP8266 in the eventual final solution.  I’m only using the ESP8266 as a tiny and cheap <$1 processor.  If you’re using the built-in WiFi, this may still work for you.  Much of the good information I found on the Internet was from Andreas Spiess (A.S.) YouTube channel –  https://www.youtube.com/watch?v=IYuYTfO6iOs
  • Development boards (WeMos and NodeMCU) use very inefficient voltage regulators.  These are typically geared for… “Developing“.  They focus on conveyance of using a USB connection for development.  These run at 5V, while the ESP8266 requires 3.3V.  They waste a tremendous amount of power while in Deep Sleep… basically making it worthless for power saving.
  • Instead… you’ll need to use something like an ESP-07 or in this case a ESP-12F. This is basically the bare Espressif board.  Typically the WeMos and NodeMCU use this same ESP-12F board.
  • The ESP-01 is actually less desirable as it does not expose the pins I’ll eventually need and it has an LED that is on continuously using power.  Some have suggested that you can simply de-solder the LED and save that power usage.  
  • Not using a development board requires you to use your own voltage regulation if you plan on using a LiPo or LiIon battery.  The ESP8266 can not tolerate the 4.2V of a fully charged lithium battery, much less the 5.0V from a USB AC adapter.  I am using an HT7333-A.  (A.S.) I have found it is only using 4 μA while sleeping.
  • Not using a development board also is problematic since you must have an external programmer (no USB) and you can’t debug it with Serial Monitor.  I did all the development work on a WeMos and will load my full projects onto the ESP-12F after they’re all debugged.  In the code below, you’ll see Serial output for when I was developing on the WeMos and blinking the LED so I could see it is working once I move it over to the ESP-12F.  Once I’ve established all the code is working, all these portions will be #ifdef‘d out. We don’t want to waste power blinking lights!
  • Note in the code that some of the library functions do not work stand-alone like you’d think.  For example at the top simply turning WIFI_OFF doesn’t actually work unless you also use forceSleepBegin().  This is counter intuitive.  It sounds like your putting it to sleep immediately.  Also the delay(1) is required.  delay(0) does not work.
  • Note at the bottom of the source code that WAKE_RF_DISABLED is included in the sleep call.  
  • If you are new to using Deep Sleep (as I was) you may not know… when it wakes up, it is almost like starting from powering up.  IOW, it runs through your setup() function again.
  • There are two important differences between powering-up and waking from deep sleep.
    1. When powering up, the WiFi radio is on.  There is no way to disable it before reaching the setup() function.  When waking and because it was put to sleep with the WAKE_RF_DISABLED, the radio will be off in this pre-setup() time.
    2. When powering up, RTC memory is volatile.  It is cleared out.  When waking, values set in RTC are non-volatile.  You can use this to retain things during sleep… like where you were in your code when you put it to sleep.
  • There is a current spike because of the WiFi being on, during powering-up.  I believe (A.S.) mentioned it was well above the 250 mA that the HT7333-A can handle.  I can certainly confirm, the ESP8266 with HT7333-A does brown-out and fails to boot.
  • (A.S.) found he needed a 1000 μF capacitor across the 3.3V input power to get it past this surge.  However, I found I was able to boot up even with a 220 μF capacitor.  I’m guessing that this is attributed to me turning WiFi off as early as possible while A.S. was continuing through the WiFi initialization, and router connecting.
  • I found the ESP8266 without WiFi only uses about 15 mA while running basic logic including Mathematics, and Trigonometry functions.
  • Interestingly and by happen stance, I took out the capacitor AFTER it had powered up.  It continued to run just fine without it and even through deep sleep cycles.  This proves the WAKE_RF_DISABLED option is successfully keeping the WiFi off.
  • The following picture illustrates with the ESP8266 LED currently blinking on and with the 220 μF removed.  Also the other capacitors are lined up from 1000 to 100 μF.  The 100μF was not able to boot the ESP8266.
  • Of important note – There are no sensors or LoRa modules on this test, so things may change once those are added.  The LoRa (on paper) shows a 120 mA surge during transmitting, but this should still be within the limits of the HT7333-A regulator.  
  • I’ll update with a wiring diagram once its been finalized.  
  • Power down tests may be a long time coming… see the attached 380 mAh LiPo shown in the picture.  Based on data sheet estimations and readings I can take with a volt-meter measuring current (sans oscilloscope) this battery should last with sensors and LoRa communications going for 382 days!!! – Maybe, I can accelerate that by reducing the Sleep time. 😊
 #include <ESP8266WiFi.h>
 #include <limits.h>
 #include <user_interface.h>
 
// https://www.youtube.com/watch?v=IYuYTfO6iOs
// Voltage regulator HT7333 => 0.15V Dropout, QC = 4uA
// ESP draws peak current of 350 mA.

void setup() 
{
    // https://www.instructables.com/ESP8266-Pro-Tips/
    // According to this article...
    // There is no way to turn off WiFi via software from a power-on state.
    // We can, however, turn it off immediately.  Note all 3 statements are
    // required.  In this project, I will not be using the WiFi capabilities
    // of the ESP8266.  Will be using LoRa
    WiFi.mode(WIFI_OFF);
    WiFi.forceSleepBegin();
    delay(1);
  
    // 1 second delay with LED On so we can see it's started.
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
    Serial.begin(115200);
    Serial.setTimeout(2000);
    delay(1000);
    
    // 10 second delay with LED Off to measure current.
    Serial.println("\nFull, LED Off, 10 seconds");
    digitalWrite(LED_BUILTIN, HIGH);
    delay(10000);

    // 10 second delay with LED On to measure current.
    Serial.println("Full, LED On, 10 seconds");
    digitalWrite(LED_BUILTIN, LOW);
    delay(10000);
    
    // 30 second delay in sleep mode to measure current.
    u32 SLEEP = 30E6; // UINT_MAX;
    Serial.printf(
        "Sleep, cal=%d,  Sleep for %u seconds\n",
        system_rtc_clock_cali_proc(), SLEEP / 1000000);
    // This puts it to sleep, but also disables the radio from automatically
    // starting up when it awakes.
    ESP.deepSleep(SLEEP, WAKE_RF_DISABLED); 
}

void loop() 
{
}