ESP32 "soft power button" implementation

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
User avatar
EresIONo
Posts: 2
Joined: Wed May 18, 2022 10:25 pm

ESP32 "soft power button" implementation

Post by EresIONo » Wed May 18, 2022 10:51 pm

I've been experimenting creating a soft power button for an ESP32 project. The required behavior is with a 1sec press go into deepsleep, then wake the ESP32 with a press of the same button.

1 - THIS WORKS:
  • create button pin with IRQ_RISING callback1
  • in callback1: start timer, remove irq, add new IRQ_FALLING callback2
  • in callback2: if timer>1s THEN remove irq, add wake_on_ext0 + deepsleep ELSE reset callback
Although this works reliably, what I find annoying is that the ESP32 goes to sleep only after you release the button.

2 - THIS DOESN'T WORK
  • create button pin with IRQ_RISING callback1
  • in callback start 1s timer with callback2
  • in callback2: if button is still down, remove irq, add wake_on_ext0 + deepsleep
Problem with this is, the button is still down when the ESP32 goes into deepsleep, so it wakes up immediately

Is there a better approach to doing the second version? I have attempted to disable interrupts as a test but that makes no difference

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: ESP32 "soft power button" implementation

Post by karfas » Thu May 19, 2022 5:44 am

Use your first version and lie to the user.

After a second, turn everything off (or whatever you do to signal the user the thing is off), but wait for the user actually releasing the key before you go into deep sleep.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

User avatar
EresIONo
Posts: 2
Joined: Wed May 18, 2022 10:25 pm

Re: ESP32 "soft power button" implementation

Post by EresIONo » Thu May 19, 2022 4:25 pm

That is a good idea! There is a small OLED display on the device, I'll use a timer to turn that off after a second if the power button is still down.
karfas wrote:
Thu May 19, 2022 5:44 am
After a second, turn everything off (or whatever you do to signal the user the thing is off), but wait for the user actually releasing the key before you go into deep sleep.

Post Reply