Xiao ESP32-C3 deep sleep in micropython

Hi, I am new to the XIAO ESP32-C3 board. I’ve managed to code everything I needed so far except for setting up deep sleep with wake up from two digital input pins. Could anyone PLEASE share a micropython working code snippet doing that?

I’ve tried
w1 = Pin(3, Pin.IN)
w2 = Pin(4, Pin.IN)
esp32.wake_on_ext1(pins = (w1, w2), level = esp32.WAKEUP_ANY_HIGH)
machine.deepsleep()
but I get an *ValueError: invalid pin" error.

Many thanks in advance

Hi there,
Try This…The ESP32 should blink the on-board LED and print a message. Then, it goes to sleep.
This is repeated over and over again.

from machine import deepsleep
from machine import Pin
from time import sleep

led = Pin (2, Pin.OUT)

#blink LED
led.value(1)
sleep(1)
led.value(0)
sleep(1)

# wait 5 seconds so that you can catch the ESP awake to establish a serial communication later
# you should remove this sleep line in your final script
sleep(5)

print('Im awake, but Im going to sleep')

#sleep for 10 seconds (10000 milliseconds)
deepsleep(10000)

HTH
GL :slight_smile: PJ :v:

p.s.
It’s important to add a 5 seconds delay before going to sleep when we are developing the scripts. When you want to upload a new code to the board, it needs to be awake. So, if you don’t have the delay, it will be difficult to catch it awake to upload new code later on. After having the final code, you can delete that delay.
here is a good video increasing battery life for many day’s https://youtu.be/ndmJR6Z_u5w?si=n37uHIAZWqvdPu_v

Thanks a lot PJ!
Do you have a similar example that wakes up upon setting one of two pins?
I know I could go c/c++ but the application I wrote is in micropython and to translate it back to C would take a significant effort and I bought the xiao board due to the very low deepsleep current (it is an iot app that gets to run once or twice a day one one of two reed switches open up)…
Kind regards
Massimo

Hi there,
Sorry I don’t, but adding the button should not be. Look at my other posts to see the sleep and wake up interrupt for the Xiao Nrf52 on the Grove Flash Expansion board. That may help.
basically, You pick a pin to attach the correct polarity interrupt to and a Button. then include in your main code a check if an interrupt flag from a button press is set. Your Interrupt routine should only set the button pressed flag and return (no messing around). pretty 101 :index_pointing_at_the_viewer: You can handle it . LOL.
FYI, “msfujino” has many great power saving posts on here, do give those a read.

HTH
GL :slight_smile: PJ

It appears you’re attempting to use MicroPython on the XIAO ESP32-C3 board to set up deep sleep with wake up from two digital input pins. The error you’re encountering may be due to pin configuration or compatibility issues.

Sorry but this comments just gives back what I wrote above and doesn’t offer any explanation or help??
In the meantime I’ve spent some time in checking out the source code for the esp32 c3 micropython port and the error is due to a complete leck of support of proper deep sleep wakeup from pin for the C3. The invalid pin error comes from one validation macro that is not properly implemented for the C3.

There is a working branch that has a complete and working implementation but it seems to take forever to be merged into the baseline.

Explore energy efficiency with Xiao ESP32-C3 in MicroPython as it takes advantage of deep sleep functionality. Optimize power consumption and extend battery life in your IoT projects with this advanced feature.