I'm using a esp8266 to build a remote control to power a gas fireplace. I'm using this guide to try and maximize battery life.

The concept is the controller uses its built in WiFi to access a RESTful API on a remote server; If the API discovers"1", it should close a switch and the fireplace turns on. If the arduino reads a "0" via the API it opens the switch and the fireplace turns off. After discovery and performing the required action the unit calls Deepsleep and shuts off for ten seconds, whereupon it powers up and the cycle repeats itself again.

I just realized that if the controller powers off, I don't know what happens to the switch. If the fireplace was turned on, what happens when deep sleep starts. Is there a way (special switch?) that maintains its state in deepsleep mode?

  • Please edit question to clarify meaning of “I'd it sees” Dec 17, 2016 at 1:21
  • Save the last state in EEPROM. Or, since you are using a remote server, store the state on the server.
    – Gerben
    Dec 17, 2016 at 13:24
  • 1
    @Gerben I think he means the physical state of the IO pin - when in deep sleep the general consensus (though I haven't found a definitive answer) is that the GPIO pins will lose their drive state when the chip goes to sleep.
    – Majenko
    Dec 17, 2016 at 16:23
  • @Majenko that indeed makes more sense.
    – Gerben
    Dec 17, 2016 at 16:54

3 Answers 3


Yes, there is such 'switch'. You could use JK Flip-Flop to hold the last output. JK Flip-Flop has J, K and clock inputs and If J and K are different then the output takes the value of J at the next clock edge.

  • Wouldn't the flip-flop require power all the time, reducing the benefits of the deep sleep mode? Feb 15, 2017 at 19:34
  • ESP8266 draws from 50 up to 200 mA while J-K flip-flop draws 5-6 mA + less than 100 µA per input. Everything reduces the benefits of the deep-sleep mode, even the ESP8266's counter which is still running during deep sleep.
    – Chupo_cro
    Feb 17, 2017 at 3:26
  • So that's a "Yes, but the difference is marginal"? I was asking for enlightenment, so thanks for the info. Feb 21, 2017 at 18:44
  • Well, the difference between the deep sleep mode and 'deep sleep + J-K flip flop' is indeed considerable because the current draw in deep sleep mode is only about 10 µA but 5-6 mA might still be acceptable when using batteries because a few seconds of ESP8266 drawing full power everytime it wakes up will probably drain the battery more than J-K flip-flop drawing a few mA continuously. Depends on waking up intervals and for the remote control to be acceptably responsive ESP has to wake up quite frequently.
    – Chupo_cro
    Feb 22, 2017 at 5:06

You can get a latching relay. These come in two flavours - Unipolar, where you energise one pin to turn it on, and energise the other to turn it off, and Bipolar, where the direction of the current through the coil turns on or turns off.

Unipolar is easier to drive since you just need two transistors, diodes and resistors. Bipolar is harder, since you need a H-bridge (motor driver), but requires less wires to connect it up (2 as opposed to 3).

In both cases it is just a short pulse that is needed to switch the relay into a different state - there is no need to maintain the output, so is ideal for deep-sleep mode where everything is turned off except the RTC.


During my Deep Sleep experiments I noticed that a couple of pins held their state through a Deep Sleep reset, but I didn't write it down anywhere. Simple observation suggests that any of the pins that wiggle during boot will NOT be retained, which leaves just GPIO4 and GPIO5 for likely candidates. Try it and see!

During Deep Sleep the pins hold their state, although they're using weak FETs equivalent to 2uA source/sink current. The boot cycle following Sleep clobbers most of the GPIOs. Sigh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.