# Switch Microcontroller on and off with a button and mofset

**URL:** <https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044>\
**Category:** 3rd Party Boards\
**Created:** [October 28, 2022, 4:42pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044 "2022-10-28T16:42:39Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamedrepublic](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@hamedrepublic](https://forum.arduino.cc/u/hamedrepublic)\
**Post date:** [October 28, 2022, 4:42pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/1 "2022-10-28T16:42:40Z")

</div>

hi guys  
I need turn on/off arduino NANO by pressing a Puch button for 2 seconds each time  
I used this schematic and now I need help writing the code to make it work

 ![Push-Button Power Circuit](https://europe1.discourse-cdn.com/arduino/original/4X/5/5/9/559e5b97a4cb9f8e81fddd7b7ea844e707ca4883.jpeg)

First, when I hold the push button, the mofast is connected and the device turns on, now the analog pin 0 is low, and in order for the device to stay on, digital pin 6 needs to high.  
When the analog pin 0 button is pressed again for 2 seconds, the digital pin 6 will be reduced until the device is turned off.  
I wrote some code but it didn't work  
Please help me in writing the code  
Thanks

---

<div class="post-metadata">

**Author:** ![gonpezzi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gonpezzi/32/763557_2.png) [@gonpezzi](https://forum.arduino.cc/u/gonpezzi)\
**Post date:** [October 28, 2022, 5:04pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/2 "2022-10-28T17:04:05Z")

</div>

Upload the code you wrote, it is always more facial to correct an error than for us to start from scratch.

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [October 28, 2022, 7:26pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/3 "2022-10-28T19:26:14Z")

</div>

> [@hamedrepublic](#):
>
> I need turn on/off arduino NANO

Then put the controller into deep sleep when the condition is met. Make sure that the selected pin also allows to wake up the controller from deep sleep.

---

<div class="post-metadata">

**Author:** ![hamedrepublic](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@hamedrepublic](https://forum.arduino.cc/u/hamedrepublic)\
**Post date:** [October 28, 2022, 9:28pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/4 "2022-10-28T21:28:26Z")

</div>

thanks for answer gonpezzi  
now I write new code  
It seems to work properly  
By holding the key, the device turns on and stays on  
The only problem I have is that the arduino turns off after a shortly press button.  
How can I define that the digital pin 6 reduce just by holding the key( **A0** ) for 2 seconds?

my new code

```cpp
const int START = A0, ON = 6;
int state = 0, ONstate=0;

void setup()
{
pinMode(START, INPUT_PULLUP);
pinMode(ON, OUTPUT);

}
void loop()
{
if (state == 0 && digitalRead(START) == LOW) {
state = 1;
ONstate=!ONstate;
}
if (state == 1 && digitalRead(START) == HIGH) {
state = 0;
}
digitalWrite(ON, ONstate);

}

```

---

<div class="post-metadata">

**Author:** ![gonpezzi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gonpezzi/32/763557_2.png) [@gonpezzi](https://forum.arduino.cc/u/gonpezzi)\
**Post date:** [October 28, 2022, 9:58pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/5 "2022-10-28T21:58:41Z")

</div>

The most appropriate is to use millis to measure the pulse time. Try this code.

```cpp
const int START = A0, ON = 6;
int state = 0, ONstate = 0;
unsigned long Timer;

void setup() {
  pinMode(START, INPUT_PULLUP);
  pinMode(ON, OUTPUT);
}

void loop() {
  if (state == 0 && digitalRead(START) == LOW) {
    Timer = millis();
    while (digitalRead (A0) == LOW);
    if (millis() - Timer > 2000) { 
      state = 1;
      ONstate = !ONstate;
    }
  }
  if (state == 1 && digitalRead(START) == HIGH) {
    state = 0;
  }
  digitalWrite(ON, ONstate);
}

```

---

<div class="post-metadata">

**Author:** ![hamedrepublic](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@hamedrepublic](https://forum.arduino.cc/u/hamedrepublic)\
**Post date:** [October 29, 2022, 12:08pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/7 "2022-10-29T12:08:41Z")

</div>

i tested this code  
after press button (A0 LOW) while tuening on ardunio digitalpin 6 not high and ardunio turn off after unpress button

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [October 29, 2022, 12:19pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/8 "2022-10-29T12:19:57Z")

</div>

Try setting pin 6 HIGH in setup immediately after setting it to OUTPUT.  
Might also want to wait for A0 to go HIGH before leaving setup.

---

<div class="post-metadata">

**Author:** ![gonpezzi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gonpezzi/32/763557_2.png) [@gonpezzi](https://forum.arduino.cc/u/gonpezzi)\
**Post date:** [October 29, 2022, 1:30pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/9 "2022-10-29T13:30:44Z")

</div>

> [@david\_2018](#):
>
> Try setting pin 6 HIGH in setup immediately after setting it to OUTPUT.

It's the right thing to do, I overlooked it.

> [@david\_2018](#):
>
> Might also want to wait for A0 to go HIGH before leaving setup.

It would not recognize the off keystroke inside the loop. You would have to repeat the code also in the loop.

However, the code could look like this:

```arduino
const int START = A0, ON = 6;
int state = 0, ONstate = 0;
unsigned long Timer;

void setup() {
  pinMode(START, INPUT_PULLUP);
  pinMode(ON, OUTPUT);
  digitalWrite (6, HIGH);
  on_off();
}

void loop() {
   on_off();
  //rest of the code
}

void on_off (){
 if (state == 0 && digitalRead(START) == LOW) {
    Timer = millis();
    while (digitalRead (A0) == LOW);
    if (millis() - Timer > 2000) {
      state = 1;
      ONstate = !ONstate;
    }
  }
  if (state == 1 && digitalRead(START) == HIGH) {
    state = 0;
  }
  digitalWrite(ON, ONstate);  
}

```

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [October 29, 2022, 2:57pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/10 "2022-10-29T14:57:12Z")

</div>

No need to overly complicate things, once the output is HIGH there is no need to constantly be rewriting that, the only time another digitalWrite is needed is when the power is shut off. It should be sufficient to just keep track of whether the button has been held down for two seconds.

```cpp
const int START = A0;
const int ON = 6;
unsigned long timer;

void setup() {
  pinMode(START, INPUT_PULLUP);
  pinMode(ON, OUTPUT);
  digitalWrite(ON, HIGH);
  while(digitalRead(START) == LOW){
    //wait for power button to be released
    };
  timer = millis(); //initialize timer
}

void loop() {
  if (digitalRead(START) == LOW){
    if ((millis() - timer) > 2000ul){
      digitalWrite(ON, LOW); //turn off power
      while(true){}; //do nothing while power shuts off
    }
  } else {
    timer = millis(); //reset timer
  }
}
```

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [October 29, 2022, 3:11pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/11 "2022-10-29T15:11:11Z")

</div>

```cpp
void on_off (){
 if (state == 0 && digitalRead(START) == LOW) {
    Timer = millis();
    while (digitalRead (A0) == LOW);
    if (millis() - Timer > 2000) {
      state = 1;
      ONstate = !ONstate;
    }
  }
  if (state == 1 && digitalRead(START) == HIGH) {
    state = 0;
  }
  digitalWrite(ON, ONstate);  
}
```

Something else to be careful of. The code is waiting for START to go high before checking for the 2000ms time interval, so the input will be high at that point. If the power does not die quickly, there may be enough time to go through loop again and turn the power back on before the processor shuts down. Not a problem if there is some more code to keep loop from repeating rapidly, but may be a problem with this test sketch.

---

<div class="post-metadata">

**Author:** ![gonpezzi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gonpezzi/32/763557_2.png) [@gonpezzi](https://forum.arduino.cc/u/gonpezzi)\
**Post date:** [October 29, 2022, 4:09pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/12 "2022-10-29T16:09:08Z")

</div>

> [@david\_2018](#):
>
> The code is waiting for START to go high before checking for the 2000ms time interval

Right, and there it sits, no further instructions are executed until you lift your finger to check the elapsed time and toggle the output.

And I think that your code from post #10 is not what the PO wants, to my understanding it works like this:

- On pressing it turns on, waits for the release of the button, assigns Timer the value of millis and does not make any comparison of the time passed until another press of the button. It can be activated without having passed the two seconds.
- Once inside the loop and possibly after many laps of the loop Timer will surely be much greater than 2000 and when pressed it turns off immediately.

For both cases of on/off, the 2 seconds are never taken into account.

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [October 29, 2022, 4:31pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/13 "2022-10-29T16:31:14Z")

</div>

> [@gonpezzi](#):
>
> And I think that your code from post #10 is not what the PO wants, to my understanding it works like this:
> 
> - On pressing it turns on, waits for the release of the button, assigns Timer the value of millis and does not make any comparison of the time passed until another press of the button. It can be activated without having passed the two seconds.
> - Once inside the loop and possibly after many laps of the loop Timer will surely be much greater than 2000 and when pressed it turns off immediately.
> 
> For both cases of on/off, the 2 seconds are never taken into account.

I missed the part about having to hold the button for two seconds to turn the power on. Would help if the OP would clarify which of the following should happen:

1 \> button pressed, power comes on, arduino waits for release of the button, then begins code execution if button has been held for two seconds or more. (code in this case being code other than the part for latching the power ON/OFF).  
2 \> button pressed, power comes on, arduino begins code execution after button has been pressed for two seconds, but does not wait for button to be released.  
3 \> button pressed, power comes on, arduino begins code execution immediately, but stops and shuts power off if button is released after being pressed for less than two seconds.

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [October 29, 2022, 4:34pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/14 "2022-10-29T16:34:44Z")

</div>

> [@gonpezzi](#):
>
> - Once inside the loop and possibly after many laps of the loop Timer will surely be much greater than 2000 and when pressed it turns off immediately.

Timer is set to the value of millis() whenever the button input is HIGH, so it keeps getting updated every time though loop until the button is pressed.

---

<div class="post-metadata">

**Author:** ![gonpezzi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gonpezzi/32/763557_2.png) [@gonpezzi](https://forum.arduino.cc/u/gonpezzi)\
**Post date:** [October 29, 2022, 4:51pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/15 "2022-10-29T16:51:14Z")

</div>

> [@david\_2018](#):
>
> Would help if the OP would clarify which of the following should happen:

Yes, better to wait, and not waste time uselessly, I had understood an on/off switch when the button is pressed for at least two seconds.

---

<div class="post-metadata">

**Author:** ![hamedrepublic](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@hamedrepublic](https://forum.arduino.cc/u/hamedrepublic)\
**Post date:** [October 29, 2022, 7:27pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/16 "2022-10-29T19:27:05Z")

</div>

Thanks everyone for the help me  
I test this code. works  
Only when I hold the key to turn on and the device turns on, until I release the key, the device does not run the code.  
Is it possible for the arduino dont waits for release of the button?  
I removed this part to solve this issue

```cpp
  while(digitalRead(START) == LOW){//wait for power button to be released
    };

```

And I noticed that after test on code, the code is executed immediately, but if the key is held for a longer time, the arduino turns off again

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [October 29, 2022, 7:42pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/17 "2022-10-29T19:42:36Z")

</div>

See if this works properly, it should allow the code to run at power-on, but the button has to be released and pressed again to shut down.

```cpp
const int START = A0;
const int ON = 6;
unsigned long timer;
bool startup = true;

void setup() {
  pinMode(START, INPUT_PULLUP);
  pinMode(ON, OUTPUT);
  digitalWrite(ON, HIGH);
}

void loop() {
  if (digitalRead(START) == LOW) {
    if (startup) {
      timer = millis();
    } else {
      if ((millis() - timer) > 2000ul) {
        digitalWrite(ON, LOW); //turn off power
        while (true) {}; //do nothing while power shuts off
      }
    }
  } else {
    startup = false;
    timer = millis(); //reset timer
  }
}
```

---

<div class="post-metadata">

**Author:** ![hamedrepublic](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@hamedrepublic](https://forum.arduino.cc/u/hamedrepublic)\
**Post date:** [October 29, 2022, 10:59pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/18 "2022-10-29T22:59:30Z")

</div>

> [@david\_2018](#):
>
> ```arduino
> if (digitalRead(START) == LOW) {
> if (startup) {
> timer = millis();
> } else {
> if ((millis() - timer) > 2000ul) {
> digitalWrite(ON, LOW); //turn off power
> while (true) {}; //do nothing while power shuts off
> }
> }
> } else {
> startup = false;
> timer = millis(); //reset timer
> }
> 
> ```

thanks for write code  
worked whell 👍

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 27, 2023, 11:00pm UTC](https://forum.arduino.cc/t/switch-microcontroller-on-and-off-with-a-button-and-mofset/1047044/19 "2023-04-27T23:00:16Z")

</div>

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