Arduino and P-Channel MOSFET

I'm attempting to get the simplest Arduino sketch to demonstrate the function of a P-Channel MOSFET (IRF9640PBF) shutting down the Arduino Uno. It's not working. Why? Here are the instructions I found as a result of searching the phrase "Shutting Down Arduino with MOSFET":

" Channel MOSFET Setup* : Connect the Drain of the P-channel MOSFET to the power supply (Vin) of the Arduino. Connect the Source to the Vcc (5V) of the Arduino. The Gate is connected to a digital output pin of the Arduino through a pull-up resistor. When the digital output pin is LOW, the MOSFET turns off, cutting power to the Arduino. When the pin is HIGH, the MOSFET stays on, and the Arduino remains powered."*

Here's the code:

void setup() {
  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);
}

void loop() {
  delay(5000); // Wait for 5 seconds
  digitalWrite(7, LOW); //Shutdown Arduino
}

The Arduino Uno is connected to my laptop via USB. I'm expecting the "On" green power LED to turn off indicating no power to the Arduino. Is that the right expectation?

Help/Suggestions will be appreciated.

It's always helpful so show us a schematic of what you built so we don't have to draw it or try to visualize it...

Don't believe everything you read. :smiley:

There are "good reasons" that N-channel MOSFETs are more-often used.

A P-MOSFET can the turned-on by pulling the gate low while the source remains high (the gate negative relative to the source). If you are trying to switch the voltage on the source, you may be able to understand why it's not working... If the source could go low you no longer have any Vgs.

You also need a "logic level" MOSFET so it can be turned-on with only 5V Vgs.

Thank you for the prompt reply. Pardon my ignorance as I have these questions:

Per advice on this forum to save batteries, I was advised a P-CHANNEL MOSFET was the way to go. You are suggesting the N-CHANNEL so I'll lok into that option.

Isn't that what digitalWrite(7, LOW); does?

Can you please be more specific? Based on the code in the original post, what should I change?

Again, I thank you for responding.

Are you expecting to turn off the USB power? I don't think that will work...

I think you need to provide a schematic or at least diagram showing what you have tried.

Mostly likely, incorrect circuit and/or incorrect choice of MOSFET (the IRF9640PBF is not a logic level MOSFET, and requires Vgs = -10V to be fully turned on).

Post a schematic diagram, with pins, parts and connections clearly labeled. Hand drawn is fine.

Check out the Pololu Power Switches, which are properly designed using MOSFETs.

1 Like

This should do what you want

Push the switch to turn the Uno ON
When the Uno starts it should set Dn LOW and light an LED.
When you see the LED, let go of the switch, Uno stays ON

When the Uno is ready to turn itself OFF set Dn HIGH and do a delay(5000)

also look at latch circuits: What is correct way to make latch power ciruit for arduino? - #9 by ShermanP

These can be used to switch an Arduino on and allow the Arduino to, at the end of the task, switch itself off.

There are many ways to skin a cat... I wanted to keep the MOSFET usage question as simple as possible which is why I posted the code in the original post. It was simple. But some context may be appropriate ...

I am lighting a 5v LED using 2 18650 batteries in series. The code turns the LED off at 6.2v but the battery ends up destroyed because it continues to loose voltage. The simplest an best solution to this use case was the recommendation to use a P-Channel MOSFET. I have been trying to get it to work in it's absolute simplest form which triggered the original question.

I am thankful for the replies but I would like to understand why the original code and connection description is not working. I understand the use of a schematic, but the code and connection description is so simple (and I don't know how) that it doesn't seem that a schematic would be necessary

Sorry @jim-p ,
That won't work.

For Q1 to be off Dn must be high. Dn cannot be high when Q1 is off because there's no power to the Uno to make it high. You might respond that R1 makes the gate high, but that won't work because R1 will be connected to the Uno's 5V power connection via Dn's protection diodes. This will attempt to power the Uno, but probably won't supply enough current so the gate will be low and Q1 will turn on, powering the Uno.

You cannot do this with 1 transistor of any kind, you need 2.

Maybe you want a LOW to sustain the latch in setup() and a HIGH to break it at the end.

Currently you have this:

void setup() {
  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);
}

void loop() {
  delay(5000); // Wait for 5 seconds
  digitalWrite(7, LOW); //Shutdown Arduino
}

That is if you are using a PMOS directly but it depends on your circuit.

Right, forgot about the path through the diodes.
Actually it does work (just tried it) not sure why but it does violate my rule of having a powered device connected to an unpowered Uno.

No, it might go flat, but it won't be destroyed... unless... you forgot to either use batteries with over-discharge protection circuits built-in (most 18650 don't, but you can find them) or an external over-discharge protection circuit.

Li-ion batteries should be treated seriously and with respect, or... fires and explosions. Not a joke.

See post #5. If you haven't heard yet about logic level MOSFETs, do some research to learn how MOSFETs work, and what gate, source and drain voltage differences mean for their action. A useful search phrase is "P-MOSFET high side switch schematic".

There is not enough information in your minimal text description of the circuit to determine if there are other problems.

I find that surprising!

My guess is that since the 10K will reduce the current to just 500uA , it is not enough to power everything on the board but still not a good idea.

Wait you are right, did it wrong.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.