Electronic – Maintain state of GPIO pin across reboot

esp8266gpiomicrocontroller

I have an ESP-12 soldered onto this adapter board. I am using NodeMCU Lua based firmware to code the logic.

I have scheduled node.restart to execute periodically. 1 of the GPIO pins is used to drive an external component using HIGH or LOW state. I can restore the GPIO state (HIGH/LOW) after node.restart but during restart GPIO state is undefined. I want to maintain that state especially if the state was HIGH.

1 approach I have in my mind is to have another micro-controller such as ATTinyX which is connected to ESP-12 via UART. ESP-12 can give a string/char to ATTinyX to maintain the state (ESp-12 GPIO pin and ATTinyX pin is connected to the external component via an OR gate) and then go for a reboot. Once ESP-12 reboot is completed it can tell ATTinyX not to maintain the state because ESP-12 can maintain it now after the reboot.

This adds a bit of cost and components, is there a different way to achieve the same? I'm more on the software side, have less electronics knowledge. Any help is appreciated. Maybe it is similar to 1 bit memory which is set/reset by ESP-12, can I use a flip flop in this case?

Best Answer

schematic

simulate this circuit – Schematic created using CircuitLab

Figure 1. State holding capacitor.

Wire up a spare GPIO as shown. On reset read the state of the capacitor and set the output appropriately. This will give you a short-term 1-bit memory.

//Pseudo code to go early in boot sequence.
pinPullup(pin) = false;        //Turn off the pull-up.
pinMode(pin) = input;          //Set the pin to input mode, if required.
pdState = pinRead(pin);        //Read the input to get the power-down state.
pinMode(pin) = output;         //Configure as output.
pinWrite(pin) = pdState;       //Restore the power-down state.

You may wish to swap the order of the last two lines (depending on micro) to avoid a momentary blip.

Note that in this configuration the pin can't be used for anything else.

Related Topic