# How to turn on module like ESP8266 only when switch is activated?

I'm looking for a way that the ESP8266 (or alternative, like an Attiny85 with an RF module) is only turned on once a reed switch (or similar sensor) has been triggered.

The problem is that the ESP8266 should have just enough time to power on and send a message over the Wi-fi network. The reed switch could close too fast for the device to send a signal.

Are there any components I can use to add a minimum power time before the reed switch (or power, somehow) is closed again?

The purpose is to keep the batteries for a long time while also notifying when the reed switch is activated.

• Do you mean that the reed switch might be closed only for a short time and then opens again before you can get a chance to fully perform your task? – jonk Dec 30 '16 at 15:00
• It's possible to use a normally open or closed reed switch, as long as I'm able to send a short signal and then stop using the batteries once that's done. The first answer is great, but I'm waiting to see if there are any other ways. – Kevin Van Ryckegem Dec 30 '16 at 15:20
• If I read you correctly, I'd say there are two ways to go. (1) Circuit starts a timer when reed relay closes with a "too short" time period, but where the CPU periodically "re-starts" the timer until it is done. (2) Circuit starts a timer when the reed relay closes with a "too long" time period, but where the CPU can turn it off, when done. Do you have a preference? – jonk Dec 30 '16 at 15:39
• What about using a transistor, as suggested by @Dampmaskin? – Kevin Van Ryckegem Dec 30 '16 at 15:42
• What about the sleep mode suggested by Scott?? Is there a reason you can't consider that idea? And if not, why not? Also, I seem to recall that the ESP8266 accepts 5 V but that this goes through an LDO to generate either 3.6 V or 3.3 V for the actual device and that you may bypass the LDO and directly provide the lower voltage. What supply voltage rail are you providing the device? (I think the peak startup currents are on the order of $\frac{1}{3}\:\textrm{A}$, though the operational currents [sans-Wifi Tx/Rx] are lots lower.) – jonk Dec 30 '16 at 16:09

Dampmaskin has the right idea, but his design is overly simplistic, for the reasons I outlined in my comments.

A more robust circuit would look like this:

simulate this circuit – Schematic created using CircuitLab

R1 and R3 make sure that leakage currents in either transistor won't cause unwanted activation of the circuit.

Obviously, the micro must set the GPIO pin high as soon as possible after it comes out of reset, before C1 discharges too far (make C1 larger if necessary). Then, it can set the pin low (or tristate it) when the task it is doing is complete.

You can substitute MOSFETs for the BJTs if you like, in which case, R2 and R4 are not needed (but they cause no harm, either).

• It's more complicated than I expected, but better to be robust of course! What's the capacitor used for? – Kevin Van Ryckegem Dec 30 '16 at 15:57
• It's complicated because it is built of discrete components, if you prefer a simpler schematic you can use an IC having most of the needed function in a tiny package, like one intended for switching downstream port power in USB hubs. – Chris Stratton Dec 30 '16 at 15:58
• See my edit above. Plus, you should have at least one decoupling capacitor on any microcontroller anyway. – Dave Tweed Dec 30 '16 at 15:58
• What if the reed switch is not closed long enough and uC will not have enough time to turn ON Q1? – Nazar Dec 30 '16 at 15:59
• @Naz: Even a very short closure of the reed switch will charge the capacitor, which should be large enough to get the microcontroller out of reset. – Dave Tweed Dec 30 '16 at 16:00

One possible approach: Let the reed switch power the Attiny in parallel with an NPN transistor (or perhaps an N-channel MOSFET) as a low side switch. When the Attiny turns on, it can output a HIGH to the base of the transistor, turning the transistor on, keeping the Attiny powered for as long as it needs to. When the Attiny is done doing whatever it's supposed to do, it can then set the output LOW, which turns the transistor off and shuts everything down (unless the reed switch is still closed).

• It's better to use a high-side switch. Then you don't run into issues with different parts of the circuit having different ideas about what "ground" is. – Dave Tweed Dec 30 '16 at 15:23
• Also, in order to shut off the transistor, you need to tristate (open-circuit) the control pin -- and also make sure it doesn't have any ESD protection diodes -- otherwise, leakage currents through the rest of the circuit will keep the transistor partially on. This is why most such circuits employ a minimum of two external transistors. – Dave Tweed Dec 30 '16 at 15:36
• @Dampmaskin Do you mean something like this? imgur.com/a/qtmVS It's for a door sensor. I hope I put the transistor right. – Kevin Van Ryckegem Dec 30 '16 at 15:38
• I was thinking about a normally open reed switch that powered the micro directly, not a normally closed one. Anyway, I only meant to convey the concept, as I don't have a full working solution to present to you. But other than that, yes, something like that, more or less. – Dampmaskin Dec 30 '16 at 15:44
• @DaveTweed: Thanks for the comments. Perhaps using a P-channel MOSFET with a pull-up resistor on the gate would do the trick then? – Dampmaskin Dec 30 '16 at 15:46

You functionality should have nothing to do with how long the reed switch is activated. Identification of reed switch activity should put your system into a state where it does what it needs to do, complete the job, then turn off.

This is how sleep modes work.

• You mean like making the system deep sleep when the reed switch is not activated? I thought about that, however I thought that since the switch would only be activated 3-4x a day, the battery would drain less if the esp is entirely shut off (since it still consumes about 15mA when in sleep mode) – Kevin Van Ryckegem Dec 30 '16 at 16:13
• Then pick a platform with better sleep functionality. – Scott Seidman Dec 30 '16 at 16:14
• Of course, the microcontroller controls power to the RF module, probably through a FET, or LDO with an enable. – Scott Seidman Dec 30 '16 at 16:16
• The ESP8266 is basically like an Arduino -- high level software, lots of libraries that most programmers are mostly ignorant about, they can be very tiny, and they can be bought overseas for \$3, including all shipping costs. A "better sleep" platform might require more money, be much much larger, or else require a LOT more technical expertise than you can reasonably expect from a user. Though I cannot speak for the OP, here. – jonk Dec 30 '16 at 16:32