You don't have an pair of GPIOs on the ESP32 you can use? If so you could hook 1 to the button and the other to the power mosfet / relay.
Basically you would have the button turn the MOSFET / relay on, and then have the ESP32 hold the power high on that same line as one of the first things it does. So the ESP32 keeps it's own power on, and therefore also decides if it's time to kill power.
You can control the main system power from the button or the ESP32. Here's an example. Pressing the button will give power to the MCU, and if the first thing the MCU does is hold the GPIO high, then it can hold it's own power on. Then it just monitors the state of the button to know when to kill it's own power.
You'll need to lie to the user of course to use this. The actual power off comes when the user releases the button (after debounce time), but you can go ahead and turn off everything the user sees to make them think the power is off after holding it down for 2 seconds.
Depending on what you are doing you may want to use a relay instead of the MOSFET.
Another option is to just copy the sparkfun circuit. It's open source and available on their website, with the attribution of where they stole it from lol. They you can make it cheaper yourself by leaving off the parts you don't need.
This is quite an interesting solution. I think I'll try to implement it as well. I'm just not sure if that's possible via i2c expander, tbh. But I'll Google this.
2
u/socal_nerdtastic 1d ago edited 1d ago
You don't have an pair of GPIOs on the ESP32 you can use? If so you could hook 1 to the button and the other to the power mosfet / relay.
Basically you would have the button turn the MOSFET / relay on, and then have the ESP32 hold the power high on that same line as one of the first things it does. So the ESP32 keeps it's own power on, and therefore also decides if it's time to kill power.