[SOLVED] Turn on Uno by hardware (button), turn off by software (via MOSFET)

What I am trying to do

I am trying to build a circuit so that you can hit a button to turn a microcontroller on (I am using an Uno board), but hitting the button again does not turn it off. Instead, I wan't to have it turned off by the microcontroller deciding it should turn itself off. This is connected to a battery, so I want this to have the device completely power off when not in use without needing to disconnect the battery.

Essentially, I want to have it turned on by hardware, but off by software.

What I have done to implement this

In order to implement this I decided to make a power circuit where the power from the battery would flow to the board by one of two ways:

  1. a button

  2. a transistor, which is controlled by digital pin on the microcontroller

The transistor that I used for experimenting with this circuit is an N-channel power MOSFET transistor, that I got from Adafruit here: N-channel power MOSFET [30V / 60A] : ID 355 : $2.25 : Adafruit Industries, Unique & fun DIY electronics and kits. I know this transistor is way over kill for this project, but it was what I had on hand. I don't suspect the type of transistor is what is causing my problem.

The basic schematic is shown in the picture.

What I expected / did not expect

I expected to need to tie the gate of the MOSFET to the ground (I did, using a 1k resistor) to keep it normally at negative and thus off, but did not expect to need to put in resistance between the gate of the MOSFET and the pin on the Uno board that I am using to control the MOSFET. If I didn't put in the resistance, when I connected the gate to the I/O output pin the device would turn on before I pressed the button. If I connected the gate to ground rail, it would stay off, as expected. If I connected the gate to the +5 rail, the Uno would turn on, as expected.

As soon as I touched the gate to any pin on the board or to the drain of the MOSFET, the board would turn on. You can see that here:

My sort of solution

By experimentation, I found that if I put in between 1.7k and 2.2k of resistance between the gate of the MOSFET and the I/O pin I am using, I could get this to work (this is reflected in the schematic above). Too little and the board would turn on automatically (rather than after a button push) and too much and the board would not stay on (i.e. even after pushing the button the board would not stay on, largely as the HIGH on the I/O pin could not keep the MOSFET gate HIGH).

However, even with this solution the power LED on the Uno board would sometimes faintly light. Really faintly. As far as I can tell the microcontroller is not running as the Pin13LED is not on (I turn it HIGH as soon as the board starts running), but since I want this to be completely powered off, this faintly lighted LED concerns me as it indicates some power is flowing.

You can see the circuit here (with 2k of resistance between gate and I/O pin). In this circumstance the power LED is not lit up (i.e. the board remains off until I press the button, at which point it stays on as the pin is held HIGH for the MOSFET gate. Five seconds later (because that is what I decided for this test), the Uno takes the pin to LOW and the whole board powers off. I consider this to be success.

My questions

  1. Why is it that connecting the MOSFET gate pin to one of the I/O pins or to the drain of the MOSFET causes the MOSFET to 'turn on' (let current flow from source to drain, not sure of the correct terminology)?

  2. Is there something that connects the I/O pins on the Uno board to ground?

  3. Why does putting resistance between the gate and the I/O pin help to (mostly) solve this?

  4. I figured out the resistance needed by experimentation, but is there a way to calculate what this should be?

  5. Is current actually not flowing if the power LED is not lit, or is it that it is flowing very slowly and can't light up the LED? The fact that the LED sometimes is very faintly lit concerns me about this. If it is flowing slowly, how much (approx) would that be and will it have a big impact on battery life?

*** My code ***

This is the code that I am using the test this circuit

int OnOffPin = 7;

void setup() {
  // initialize digital OnOffPin as an output.
  pinMode(OnOffPin, OUTPUT);
  
  // initialize digital pin 13 to show when the OnOffPin is high
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(OnOffPin, HIGH);   // turn the OnOffPin on by making the voltage HIGH
  digitalWrite(13, HIGH);
  delay(5000);                   // wait for five seconds

  digitalWrite(OnOffPin, LOW);  // turn the OnOffPin off by making the voltage LOW
  digitalWrite(13, LOW);
  delay(5000);                  // wait for five seconds, but this should never come back on
}

Many thanks in advance for helping to answer these questions.

For this application (power switch), you need a P-channel MOSFET.

https://www.adafruit.com/search?q=p-channel&b=1

It would be easier to just put the processor to sleep. If you are using a standalone ATmega the power-down current is about 1µA which is probably much less than the leakage current of a MOSFET. You can wake the ATmega from sleep with a button.
You may need a voltage regulator for the chip of you use a 9V battery as shown in your pics. I am using an MCP1702 for this. Quiescent current is 2µA.
Please provide some more details on your project: what CPU?, what battery?, battery current when running?, running time vs. off time?, expected battery life when off? etc.

Thanks for the comments.

dlloyd:
Why do I need a P-channel MOSFET? What about the P-channel gives it an advantage over the N-channel? I had originally chosen the N-channel as the gate will be connected an I/O pin, and I don't want it to open (and thus provide current) when the Arduino is off and thus not holding the I/O pin connected to the gate high.

olf2012:
Thanks. I will need to look into the MOSFET leakage current more.

Sorry, not sure how it wasn't clear in the upfront text and in the pictures, but it is all good: I am using an Arduino Uno board. I will be trying to shrink this down using an Adafruit Trinket Pro.

I fear that even if I sleep the MCU, that there are other things (LEDs, other chips, who knows) that will continue to draw current. My ideal is to have this completely power off by software. Maybe I need a relay?

I am really trying to understand how to make this circuit work, but for the immediate application the time on and off would probably be:

  • off for a couple days or weeks
  • on for a couple hours
  • off again for a couple days or weeks
    and repeat...

While on it will just be paying attention to an accelerometer, and if it sees movement, turn on an LED, a peizo, etc..

I am easy on battery type, but for now I am using a 9V battery as it seems to be the best compromise of size and cost for the amount of energy it stores. I am hoping the battery will last for a 'while' (weeks) on that cycle. With both the Uno and the Trinket pro there is an on board voltage regulator.

dlloyd:
Why do I need a P-channel MOSFET? What about the P-channel gives it an advantage over the N-channel? I had originally chosen the N-channel as the gate will be connected an I/O pin, and I don't want it to open (and thus provide current) when the Arduino is off and thus not holding the I/O pin connected to the gate high.

It all comes down to what happens to the I/O when the power to the device (Arduino) is off. One might think that everything would be in a high impedance state and easily influenced by stray signals.

Not so. The I/O appears as a diode connected to ground. I thought I would test this on the Arduino Due which has rather sensitive I/O and is rated for 3.3V. I connected the ground of a separate 5V supply to the Arduino Due GND. The board was disconnected from any power source. Then I tried to pullup the I/O to 5V through a 100K, then 33K, then 10K pullup resistor. With 10K connected to 5V, the voltage measured on any I/O pin on the Due was 0.76V.

You'll notice it's quite rare to find any circuit examples that use an N-channel MOSFET as a DC power switch. However, I'm not saying it can't be done ... you may find that the required circuit would be more complex.

From this application note, it mentions:

The P-channel MOSFET has a distinct advantage over the N-channel MOSFET, and that is in the simplicity of the on/off control block. The N-channel load switch requires an additional voltage rail for the gate; the P-channel load switch does not.

It really depends on the specific needs of your project as to which method will work best or be the simplest to implement.

dlloyd,

Thanks for the explanation! Much appreciated.

I will have to go get me some p-channel MOSFETs then and try that. I am trying to make a DC power switch, and was not aware that n-channel MOSFETs we rare in this case.

Thanks again!

MakerSelf:
I will have to go get me some p-channel MOSFETs then and try that. I am trying to make a DC power switch, and was not aware that n-channel MOSFETs we rare in this case.

So I went and got some p-channel MOSFETs, and got this circuit to work well. Is is very stable now, and I don't have the issue that I did before with needing to crank up the resistance.

I utilized a circuit that has a p-channel MOSFET between the Bat+ and the Vin of the Arduino, with the gate attached to the drain of an n-channel MOSFET, which is between the p-channel MOSFET gate and ground, with the gate attached to the on/off pin on the Arduino.

You can read more here: MakerSelf power circuit redesigned

Thanks dlloyd and everyone else again!

We discussed about a year ago. Basically this circuit: