# WeMos self-turn-off with N-channel MOSFET

I'm trying to build an internet-connected button. When button is pressed, WeMos D1 turns on, makes a network call and then cuts off the power by pulling the irlb8721 gate to LOW.

Here's the program I currently use for test:

void setup() {
pinMode(2, OUTPUT);
digitalWrite(2, LOW); // Turn on built-in LED

pinMode(5, OUTPUT);
digitalWrite(5, HIGH); // Keep ourselves powered after button is released
delay(5000); // Do the work, e.g. make a network call
digitalWrite(5, LOW); // Cut off the power
}

void loop() {}


The problem is that after the power is turned off, there's still ~20mA current leaking, which is a deal-breaker for a battery-powered button. Disconnecting the pin 5 (D1) gets rid of that for some reason.

Resistor between gate and source is 676kOhm, between D1 and gate - 10kOhm. Thanks for your help!

• It would be helpful to include a schematic in your post. – Tom van der Zanden Feb 4 '18 at 12:14
• Are your sure that switch is in the right spot..not the issue but it looks wrong – Trevor_G Feb 4 '18 at 12:47

I think , because you are using the WeMos to turn itself off, what is happening is as the MOSFET turns off, the voltage on the pin driving it goes up.

Or to put it another way, the pin driver is standing on the ground it is trying to pull up.

This is what you are trying to do..

simulate this circuit – Schematic created using CircuitLab

Without studying the specs on the device that internal pull-up may or may not be present and controllable.

At some point it will turn into some form of linear regulator or oscillate.

You may have more success using

pinMode(5, INPUT);


digitalWrite(5, LOW);


That will allow the pull-down to do it's job, assuming that pull-up does not exist or can be turned off.

If it exists and can not be turned off, you need to significantly reduce the pull-down value and gate resistor and add another one to the switch line and eat the current loss through the divider.

simulate this circuit

However, either way, cutting it's own throat may be problematic since the device may reset itself during the power down or brown out.

• You're right, using pinMode(5, INPUT) drops the current to 2mA with 10kOhm resistor and to 0.11mA with 100kOhm resistor. Using 300kOhm resistor, it's no longer able to keep itself on. Thank you! – Maxim Kachurovskiy Feb 4 '18 at 14:17
• @MaximKachurovskiy yup it will always be iffy though. Using some sort of flip-flop circuit would be better. – Trevor_G Feb 4 '18 at 14:43

This is due to leakage through the circuitry of your MCU. Even when your MOSFET is turned off, the MCU remains connected 3.3V. Some current can flow from the 3.3V input, out through pin D1, through the 10k resistor, through the 676k resistor, to ground. This pulls the gate of the MOSFET up, allowing more current to flow (but due to the forward voltage drop of the diode, the gate voltage remains limited and perhaps the MOSFET does not turn on enough for the MCU to boot up).

A possible solution would be to use a high-side switch instead (using a P-channel MOSFET), whose gate you pull down using a second (N-channel) MOSFET. This is illustrated in an answer to another question on this site.

• Actually, the diode between 3V3 and D1 is reverse-biased and does not pass any current. However, there is leakage through all of the active circuitry inside the chip (and on the module) from 3V3 to G, which then finds its way to D1 through the other protection diode (the one you said doesn't matter), which is forward-biased. Otherwise, this is a good answer. – Dave Tweed Feb 4 '18 at 13:09
• Thank you, that makes sense. However, now that it's possible to limit the leakage to 0.11mA, I'm on the fence which option to go with :-) – Maxim Kachurovskiy Feb 4 '18 at 14:37
• @MaximKachurovskiy actually both this answer and mine play a part. Ultimately you are putting the device in a state that there is enough current coming out of the pin it's holding up the gate. – Trevor_G Feb 4 '18 at 20:02