ESP32 DeepSleep

I have been fighting all day with an ESP32 module. It has 38 pins. Yesterday I got it to be able to get out of deep_sleep using a touch pin using MicroPython. Prior to that I was toggling the onboard LED with a touch pin. Copy the code and use it as you wish.

import machine
from machine import Pin, TouchPad
import time
import esp32
wake = Pin(4, mode = Pin.IN, pull = Pin.PULL_DOWN)
esp32.wake_on_ext1(pins = [wake], level = Pin.WAKE_HIGH)

print('Entering Deep Sleep in 10 seconds.')
time.sleep(10)
print('Time is up.  Going to sleep')
machine.deepsleep()

I put in that time.sleep(10) so I could catch it while it was awake and be able to make changes to the code if needed.

TheMicroPython bin file I am using is esp32-20190113-v1.9.4-779-g5064df207.bin

I’ve decided to add some to the code.

The above code does not indicate when the ESP32 is awake unless you are reading the serial output. I decided to include an LED indicator for when the module is awake. The LED goes out when the module goes into sleep mode.

import machine
from machine import Pin, TouchPad
import time
import esp32
wake = Pin(4, mode = Pin.IN, pull = Pin.PULL_DOWN)
esp32.wake_on_ext1(pins = [wake], level = Pin.WAKE_HIGH)
led = Pin(2, Pin.OUT)

print('Entering Deep Sleep in 10 seconds.')
print('When the blue LED is on I am awake.')
led.value(1)
time.sleep(10)
print('Time is up.  Going to sleep')
machine.deepsleep()

2 thoughts on “ESP32 DeepSleep”

  1. Thanks for this, man…unfortunately my call to deepsleep seems to immediately reset it…

    I’m using a WeMos board w/ the OLED screen

    rst:0x5 (DEEPSLEEP_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
    mode:DIO, clock div:2
    load:0x3fff0018,len:4
    load:0x3fff001c,len:4928
    ho 0 tail 12 room 4
    load:0x40078000,len:9332
    load:0x40080400,len:6216
    entry 0x400806e8
    I (433) cpu_start: Pro cpu up.
    I (433) cpu_start: Application information:
    I (433) cpu_start: Compile time: 12:45:08
    I (435) cpu_start: Compile date: Apr 14 2019
    I (441) cpu_start: ESP-IDF: v3.3-beta1-268-g5c88c5996
    I (447) cpu_start: Starting app cpu, entry point is 0x40082950
    I (0) cpu_start: App cpu up.
    I (458) heap_init: Initializing. RAM available for dynamic allocation:
    I (464) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
    I (471) heap_init: At 3FFB9B90 len 00026470 (153 KiB): DRAM
    I (477) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
    I (483) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
    I (490) heap_init: At 40093434 len 0000CBCC (50 KiB): IRAM
    I (496) cpu_start: Pro cpu start user code
    I (66) cpu_start: Starting scheduler on PRO CPU.
    I (0) cpu_start: Starting scheduler on APP CPU.
    Traceback (most recent call last):
    File “boot.py”, line 28, in
    File “boot.py”, line 18, in make_display
    File “ssd1306.py”, line 99, in __init__
    File “ssd1306.py”, line 36, in __init__
    File “ssd1306.py”, line 61, in init_display
    File “ssd1306.py”, line 104, in write_cmd
    OSError: [Errno 19] ENODEV
    OSError: [Errno 2] ENOENT
    MicroPython v1.10-278-g673e154df on 2019-04-14; ESP32 module with ESP32
    Type “help()” for more information.
    >>>

    1. Greetings SDC,
      if I found the correct board (WeMos ESP32 with OLED) you are using, the OLED is using I2C with pins 4 (SCL) and 5 (SDA). In the second list of code above change the touch pin from 4 to a different available pin. If you don’t have anything else added to your WeMos board try pin 15, otherwise choose a different pin.

      wake = Pin(15, mode = Pin.IN, pull = Pin.PULL_DOWN)

      That should take care of the rebooting issue.
      Also, I don’t have and changes to the stock boot.py script on the board I was using.
      Peace,
      Greg

Comments are closed.