Multiple Wakeup Sources from Deepsleep

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
paulg
Posts: 29
Joined: Fri Oct 23, 2015 1:06 pm

Multiple Wakeup Sources from Deepsleep

Post by paulg » Sun Nov 24, 2019 6:32 pm

Hi, the ESP32 documentation suggests that it supports several ways to exit from deep sleep and that the various methods can be used together. Is this supported in the ESP32 MicroPython port?

I am successfully using machine.deepsleep() with a timer argument. I would also like to be able to exit deep sleep if a GPIO pin goes high, or preferably if one of two GPIO pins go high. Is this possible? I would really appreciate an answer, and if possible, an example showing how to do it. Thanks in advance.

dylandonkersgoed
Posts: 1
Joined: Sun Nov 24, 2019 9:14 pm

Re: Multiple Wakeup Sources from Deepsleep

Post by dylandonkersgoed » Sun Nov 24, 2019 9:19 pm

I came searching for information about the same topic, though it seems like I'm a little further along. I found this blog post helpful: https://randomnerdtutorials.com/micropy ... p-sources/

Relevant snippet:

Code: Select all

wake1 = Pin(14, mode = Pin.IN)
wake2 = Pin(12, mode = Pin.IN)

#level parameter can be: esp32.WAKEUP_ANY_HIGH or esp32.WAKEUP_ALL_LOW
esp32.wake_on_ext1(pins = (wake1, wake2), level = esp32.WAKEUP_ANY_HIGH)
What I'm trying to figure out is if there's a way that I can trigger it on one pin being low or another being high rather than all being low or any being high. Not sure if that's what you mean by:
if one of two GPIO pins go high

paulg
Posts: 29
Joined: Fri Oct 23, 2015 1:06 pm

Re: Multiple Wakeup Sources from Deepsleep

Post by paulg » Mon Nov 25, 2019 2:04 pm

I carried out a few experiments. The following code demonstrates various wakeup sources being used together.

Code: Select all

# test21.py: Deepsleep and wakeup source experiments
# Copy to your ESP32 board as main.py

from machine import Pin, deepsleep, wake_reason
import esp, esp32
from time import sleep

led1 = Pin(25, Pin.OUT)
btn1 = Pin(33, Pin.IN, Pin.PULL_UP)     # Normally high, low when pressed
btn2 = Pin(14, Pin.IN, Pin.PULL_DOWN)   # Normally low, high when pressed

print('\nHello. This is test 21')
esp.osdebug(None)

while True:
    print('Wake Reason =', wake_reason())
    led1.on()
    sleep(5)        # Do NOT remove this statement
    led1.off()
 
    esp32.wake_on_ext0(pin = btn2, level = esp32.WAKEUP_ANY_HIGH)
    esp32.wake_on_ext1(pins = (btn1, ), level = esp32.WAKEUP_ALL_LOW)
    deepsleep(9000)
Deepsleep will end either when the timer expires or when button 1 is pressed or when button 2 is pressed.
The wake reason will be printed (2 = Ext0, 3 = Ext1, 4 = timer).
To halt the program enter Ctrl-C at the REPL while the LED is on.

paulg
Posts: 29
Joined: Fri Oct 23, 2015 1:06 pm

Re: Multiple Wakeup Sources from Deepsleep

Post by paulg » Mon Nov 25, 2019 2:12 pm

I should also have said that the GPIO pins used must be RTC ones. These are listed in the Random Nerd tutorial mentioned above.

Post Reply