[ Note: this is the continuation of another question, the first one being my attempt to implement the same idea using an N-channel MOSFET. Based on the answers I received there, I went out, bought a P-channel MOSFET, and found myself with even more questions than before :) ]
I am trying to implement something similar to this: https://youtu.be/nbMfb0dIvYc?t=4m27s
The idea is that the microcontroller (a Wemos D1 Mini in my case) is powered up by an external switch. As soon as it starts up, it sets up a MOSFET so that it can keep its power connection until it finishes whatever job it is set up to do. In the end, it "kills itself" by cutting of the power through the MOSFET.
So I bought a P-channel MOSFET (IRF9Z34N - probably not ideal with a Vgs(th) max of -4V, but it was the best through-hole part I was able to find). And I built the following circuit:
When I press the switch, the D1 powers up correctly, pulls the MOSFET gate pin low, and goes through all of its code. In the end, it pulls the gate pin high, powering itself off.
However, something is wrong. When powering itself off (after setting D5 to HIGH), I would have expected the gate voltage to go to +5V through the pull-up resistor. Instead, it stays at around 1.25V - enough to keep the MOSFET partially open, with enough current going through it to keep the MCU in some weird, not-alive-but-not-dead-either, state (the MCU doesn't receive enough current to keep running its code, but it receives enough to prevent it from fully resetting and starting over again when I press the switch).
Based on the suggestion received from Brian Drummond on my original question, I built a dual-MOSFET arrangement:
This seems to work - but it seems needlessly complicated, especially compared to the original schematic (see youtube video above, starting at 04:24).
So my question is: is there any way to "save" the original schematic? Can I implement this using just a P-channel MOSFET? If so, how?
If it helps, here is the schematic for the Wemos D1 Mini. Note that I am powering it through the 5V line, which then goes through a power regulator.
As per your comments, let's discuss why the single PMOS solution doesn't work.
All I/O pins of modern microcontrollers use protection diodes to, well, protect sensitive internal circuitry from fault conditions - e.g. if you externally drive 5V or -5V onto a pin of a MCU, which is powered by 3.3V. This could also happen due to ESD, and without such protection, devices are easily destroyed.
However, this has two consequences:
- If a microcontroller chip is turned off (0V on its power (Vdd) pin), putting a sufficient voltage to any I/O pin, will cause current to "overflow" to the Vdd pin, and the chip will spring to life, as if you've powered it normally
- Again, in a turned off MCU, if you have a pull-up resistor to an active supply, small current will flow into the chip, try to power it, but as the current is insufficient, it will hang into this "neither on, neither off" state you're seeing.
A simple rule of the thumb there is that, if a MCU is controlling something, and it is expected the MCU to be off or otherwise disabled for some time (e.g. in sleep mode, for MCUs that don't keep their I/Os driven during sleep), it is best if the controlled thing requires an "active on" singnal, and there's a pull-down resistor. No resistor to the gate of a MOSFET would be bad, pull-up is worse, pull-down works.
Note that this kind of precautions aren't required if the MCU is expected to be always active. It is not the best practice, as resistors cost pennies, but you could omit the pull-down resistor in such a case.
For your configuration, the two-MOSFET solution is about the best it can possibly get.
Another scenario to explore, which can bring the same functionality without any MOSFETs, is to use the sleep/reset/wake-on-change functionality of the MCU (I'm not proficient with the Wemos, but I bet it has something like it). When you want to "turn off", you simply go to sleep. The MCU will still consume some current, but that would be too small to matter. And your switch can be wired to a reset pin of the MCU, or, if you want to keep the processor state - to a pin, on which you've configured interrupt/wake-on-change.