# Maintain state of GPIO pin across reboot

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?

• Yes! Any Edge triggered 1 bit memory would be good enough – Swanand Jul 12 '17 at 3:13
• @Swanand thanks. 74HC74 would work? Can you provide any DIP IC number which I can use? – ritesht93 Jul 12 '17 at 3:23
• @ritesht93 A SOT23-6 PIC10F200 would be cheaper and smaller than a 74hc74 (and can be gotten instead in DIP8 if you prefer that.) But an I/O pin is usually restarted in a high impedance state, so a resistive pull-up might be enough. Can you provide your current schematic? – jonk Jul 12 '17 at 4:38
• You may add a latching feed to drive your external component. circuitdigest.com/electronic-circuits/… – Kapil Singh Apr 13 '18 at 19:40

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.
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.

Have a look at an I2C expanders like MCP23008. Basically the same setup as your ATtiny minus another firmware. It will just keep any state you wish regardless of ESP reboots.

Try using a capacitor that would provide the required voltage for the reboot duration. The following link may help you to find the appropriate capacitor value.

http://www.electronics-tutorials.ws/rc/rc_2.html

edit:

simulate this circuit – Schematic created using CircuitLab

To be more specific, I am assuming that the time required for reboot is less than two seconds so now the RC network's time constant must be nearly twice the reboot time. Choosing a time constant of 4.7 seconds.

Time constant T = R * C

ie., 4.7 s = 4.7K OHMS * 1000uf

The reason why we should have the time constant to be twice the required time is, when a capacitor starts discharging, at half of the time constant the capacitors voltage would be about 60% of the voltage while fully charged, which is 3 volt at 2.35 second, since the reboot time is less than that, a high level will be maintained.

• Instead of linking something that is very basic, you could explain that all you need to do is hook up a resistor to a capacitor. I thumbed your answer up, but someone else thumbed it down to 0. (Probably because you linked) – Harry Svensson Jul 12 '17 at 7:31
• thank you for the tip. I'll to make sure that I give all the explanations by my self and not provide a link. – karthik Jay Jul 12 '17 at 7:49
• Copy the relevant information and keep the link as well. – Bence Kaulics Jul 12 '17 at 14:28
• Just wondering, @karthikJay, what is the discharge (or charge) path during reboot? While I was answering I assumed that the reboot won't power-down the chip so there will be no discharge through the input protection diodes and since the outputs should tri-state there won't be any pull-down there either. The capacitor should hold its charge for a long time. – Transistor Jul 12 '17 at 18:55
• (1) No. The diodes will only conduct if the input goes above the Vcc or below GND. The OP is resetting his micro without a power cycle so the power stays on the chip. C will either be a little below Vcc or a little above GND. The outputs should go tri-state (disconnected) on reset and remain that way until the program turns them to outputs again. See my pseudo code for an explanation of that part. (2) There is only one state to worry about: was the pin high or low at the point of reset. – Transistor Jul 12 '17 at 19:27

If the state of the pin prior to power off is known, you can use pull up or pull down to maintain that state while the my is booting up. Large capacitors can also help.

The issue is that the pins typically default to in out or non GPIO functions so you look their states in reset.

Try using a simple latch, but you might need three GPIO pins. While running, if the output at the GPIO that you are using is HIGH, provide a high-low pulse at SET, and if LOW do the same at the RESET. During power on check for the state of Q, if HIGH maintain the required GPIO pin as HIGH else make it LOW

• Sorry, me again. What happens when both SET and RESET float (due to tristate) on micro reset? You might get away with it for a couple of seconds. A small capacitor on each line? Component and pin count is rising! – Transistor Jul 12 '17 at 20:02