I recognize that this is an 'extreme' question, but let me try anyway.
I'm using a board with an ESP32 Wroom and a Nextion TFT display.
Currently the board is powered by the USB connector for the development phase; later it will take the power from an external adapter and the internal 5V regulator.
My program uses about 20 working parameters that I want to save in EEPROM (the ESP32 version) to be loaded at power-up. This has been positively tested.
I'd like to store the parameters only when the program is stopping (power removed) to avoid a lot of writing every time a parameter is changed.
The question is: is there any SW mechanism that I could use to detect the event and save the parms? I doubt it.
I think that a solution could be to check the power and raise an event when it decrease lower than a threeshold, but honestly I do not know how to do it.
The time required to store in EEPROM the parms is around 2 mSec.
this is somewhat of an age old problem. it's difficult to do anything just as power is failing. There's no guarantee that there's enough time to complete any operation.
you can try updating the EEPROM as parameters change during normal operation
You could use an Adafruit HUZZAH ESP32 which features a 3,7V Li-Poly battery charger. There should be cheaper clones available also. When the USB or PSU power is disconnected, the battery takes over. Used in various PV solar projects. Don't know how fast the battery takes over though. Long time ago, there were also suggestions here with a backup battery and two diodes, but I can't remember the circuit and if that were any good at all, because no battery charger circuit was in place.
A bank of one or more SuperCaps (charged from +5 through a schottky diode) could store enough energy to give the program time to write the parameters to EEPROM when loss of the main +5 is detected.
EDIT:
I'm going to guess that one SuperCap is sufficient.
Yes, this was my original idea, but I'm missing something.
I do not have doubts that 1F cap will supply the MCU for the time required (few mSec), but there some points not clear:
where and how can I detect the power off? I think that a specific hw must be added to the board; a voltage comparator, even an old 311, could fulfill the purpose, right? I yes, then the 311 output could raise an interrupt to the ESP32 to perform the saving.
The 311 input is connected to the +5V regulator, before the supercap isolated by the diode.
What's happening at the power ON? the supercap is discharged and it will take a lot to charge (I have to limit the charging current otherwise the +5V rail will be overloaded).
How to connect the supercap to the MCU? If I add another Schottky from the supercap to the MCU Vin I will drop the voltage too much.
I'm sorry if this is not very clear, I recongnize it, but it is quite late and possibly my brain should sleep a little.
Back of the envelope, I’d say using a 3.3V processor board with a low dropout regulator you’d get at least one second of useful energy from a 1F supercap. That’s even considering the forward drop of the required Schottky Diodes (~0.3V).
Regarding your questions.
Probably with a voltage divider across the main +5V supply and connected to one of the processor’s ADC inputs.
You’d limit inrush to the supercap with a resistor. See below.
See below. No, the Schottky Diodes wouldn’t cause too much drop if you’re powering a 3.3V processor through a low dropout regulator.
All that being said, it might just be easier to use an Adafruit Feather board and small LiPo battery as @Lagom already suggested.
Thanks @ gfvalvo, as I said I was sleepy and I wrote something stupid. Your scheme is interesting, but at the end I choose for a 'manual' saving using a button on the display, for two main reasons:
During the neverending development phase the ESP is powered through the PC USB and this change the whole scenario.
I now think that should be the user (me) to decide what and when to save the setting, otherwise the device could overwrite a previous, more usefull, setting at power off.
The manual solutions, tested ok, seems to be the best choice.