Electrical – Switching between power sources using a P-mosfet. Problem with LDO

ldopower supplyvoltage-regulator

I'm trying to design a circuit to switch power supply from USB +5V to battery when the USB is disconnected.
This circuit will power a WeMos D1 mini board, which needs a 3.3V input voltage, so the USB 5V voltage is converted to 3.3V through a LDO regulator, while battery voltage is already 3.3V.

I'm trying to select the power source through a FDN340P p-mosfet, like in the diagram below, but it doesn't work because when USB is disconnected, and the battery power is applied to Vcc, it goes backwards into the voltage regulator and Vin rises to 3V, switching off the p-mosfet.enter image description here

I've tried also putting a 1N4004 diode between Vin and the mosfet gate, but voltage doesn't drop across the diode (probably because current is too low?).

How can I keep the mosfet switched on when USB is disconnected?

Best Answer

Your MOSFET turns off becuase the battery voltage goes somehow from the out of the LDO to its input.

There are dedicated power path ICs that you can use. But if you want to learn a bit, and I am just guessing after a fast sketch, you can start with a couple of N channel MOSFETs used in a back-to-back configuration in series to the OUT pin of the LDO. Their gate, tied toghether, are connected to the input of the LDO, which is always higher then 3.3V by 5V-3.3V. If you can find a NMOS with a Vth less than such value, you can verify if it is a working solution.

I would use a back to back PMOS or something else also for the battery switch, because I suspect that its parasitic diode will let some current flow in the battery when it is discharged and below 3.3V. I have no clue if it is fine/safe, and works always fine in this way.