I have connected the five buttons to the analog pin of esp8266 as follows:
schematic
simulate this circuit – Schematic created using CircuitLab
I want to power on/off esp8266 when SW1 is pressed. Also before powering off, some states should be written to the EEPROM. So obviously we need at least one GPIO to do this. More precisely, when esp8266 is off and SW1 is pressed, it should turned on and stay on until SW1 is pressed again. After pressing again SW1, first states should saved to the EEPROM and then turn off, waiting for another button press. What's the appropriate circuit for this situation? I thought about comparing GPIO voltage(which we can set to 3.3V when we want to power off) and ADC voltage but it doesn't work. Also I think the desired circuit should not load the ADC input.
1 Answer 1
We pretty soon discover that it's a bad idea to mix this unrelated analog resistor ladder stuff with your power on/off functionality. So either you need a dedicated button for power on/off, or you need a DPST switch.
This switch can then drive a MOSFET placed in series with your supply. You need some manner of latching function since the switch is momentary. I would add a hardware RC filter to filter out signal bouncing.
At the same time connect the switch to a GPI pin of your MCU. If done right, then the MCU shouldn't detect the key press at power-up, but only when it's up and running.
Once you have detected a switch press, save everything to EEPROM and then from software trigger the shut-down through GPIO tied to the mentioned MOSFET.
Explore related questions
See similar questions with these tags.
how to turn off esp8266 programmatically, and how to turn on with a button?
\$\endgroup\$writing to the EEPROM before power off
... why would that be a challenge? \$\endgroup\$