Autoshutoff arduino after set time interval

Here's what I want to do:

After 15 minutes of the program starting (using millis()), I'd like the Arduino to shut itself off.
So something like this

if (millis() == 900000) {
digitalWrite(7, HIGH) //Relay IN pin with NC configuration
}

I've already tried a mosfet configuration, but that doesn't work without direct input. I'm now attempting to make a relay configuration work (connected to mains which connects to the board after the relay), however, it shuts off briefly just to power up again.

To counter this problem, I tried a voltage regulator connected to mains, which connects to the VCC and GND pin of the relay (so that the power stays on whether or not the Arduino is powered on), however, with this setup, the relay takes seemingly no input from the board and just activates the electromagnet, no matter whether the IN pin is activated as low or high.
My thinking behind this is that the relay receives the signal from the Arduino, and sustains whatever the last signal was sent with the power from mains (IE. switch open once the IN pin receives a signal).

Any advice?

So you tried something like this and it didn't work?
This one shows a PNP, I think with the correct P-channel MOSFET could be made to work as well.
Post your schematic, let us take a look.


You'd have to be a little careful in the software with this. I would read the power hold line, see that it is low before enabling the pin as an output and driving it low - you wouldn't want to be driving it high while the external switch is holding it low.
A P-channel MOSFET like NDP6020 should work well, it low Rds at logic levels. $1.73 for a single one at Digikey.

CrossRoads:
So you tried something like this and it didn't work?
This one shows a PNP, I think with the correct P-channel MOSFET could be made to work as well.
Post your schematic, let us take a look.


You'd have to be a little careful in the software with this. I would read the power hold line, see that it is low before enabling the pin as an output and driving it low - you wouldn't want to be driving it high while the external switch is holding it low.
A P-channel MOSFET like NDP6020 should work well, it low Rds at logic levels. $1.73 for a single one at Digikey.
https://www.onsemi.com/pub/Collateral/NDP6020P-D.PDF

I used a P channel, N channel kind of circuit that I found off the internet. However, I still run into the same problem because as soon as I lose power to the board the electromagnet loses its power and it opens the switch again.
EDIT: To clarify, this setup works fine with a regular switch, just not with the relay.

But the most important question is - why?

Does it have to return to "life"?

Why not just:

#include "LowPower.h"

void setup()
{    your stuff here    }

void loop()
{

   (your code and time control for 15 mins)

   LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 
}

This would reduce power to a bare minimum, and you could restart the Arduino from an interrupt if you add that to your code.

You could use a latching switch. Or attach it to the normally open part of a relay, wire a button in parallel and hold the button down to switch on. In setup set the relay to closed. Then it will switch off permanently when you depower the relay (normally turn pin low) and the relay goes back to default position. Press button again to bypass relay and turn on. Use a capacitor if you don’t want to hold the button down long enough

Does the relay have any backsurge?

tpbertu:
Does it have to return to "life"?

Why not just:

#include "LowPower.h"

void setup()
{    your stuff here    }

void loop()
{

(your code and time control for 15 mins)

LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
}





This would reduce power to a bare minimum, and you could restart the Arduino from an interrupt if you add that to your code.

No, I just need it to turn off until the power switch I have attached to my power supply is turned off and on again (resetting the arduino).

Sorry, I'm quite new to Arduino, does your code achieve this? Or would it still provide enough power to my components (ie. an LED screen) so that they remain on?
Nevermind, just found out that that library doesn't work for the Uno.

GoForSmoke:
Does the relay have any backsurge?

I don't believe that it does. I followed instructions on how to wire it, and tried wiring it differently but it won't work any other way.

pmagowan:
You could use a latching switch. Or attach it to the normally open part of a relay, wire a button in parallel and hold the button down to switch on. In setup set the relay to closed. Then it will switch off permanently when you depower the relay (normally turn pin low) and the relay goes back to default position. Press button again to bypass relay and turn on. Use a capacitor if you don’t want to hold the button down long enough

You misunderstood my post. I don't want there to be any human input; I want the arduino to shut itself off purely from the code running on it, so a latching switch won't work.

No, I didn't. Look up latching relays, or my alternative to use a non-latching relay.

pmagowan:
No, I didn't. Look up latching relays, or my alternative to use a non-latching relay.

If I understood your post correctly, you stated that with the latching switch, you'd need to press the button every time you want to power the board, no?

Perhaps I wasn't specific enough, I want to forget about it completely, and operate purely on code. I have an external power switch which lets current flow or not from the power supply (12v dc from the wall) to the barrel jack, all I want is a switch like this, but that can be activated through the board (ie. a relay switch). So if I forget to turn my project off, for example, it powers off by itself.

So, I turn the arduino on, it runs the program I have, and after 15 minutes, the power source is turned off.

But why? :cold_sweat:

exactly. That is what I have described.

You turn arduino on with a momentary button
Arduino activates a relay in setup (code)
This relay is parallel to your 'on' button and takes over powering the arduino
Arduino does its thing
Arduino turns relay off
Arduino loses power and turns off until next time you press the button.

In short:
You turn on
arduino does its thing
Arduino turns itself off

Paul__B:
But why? :cold_sweat:

Why not? Why do anything?

This is part of a bigger project, and I'd like to integrate this feature because I certainly will forget to turn it off, and for efficiency sake, it would be nice to have.

I'm asking for help for a reason, not because I'm trying to waste your time. If you don't want to help, don't.

pmagowan:
exactly. That is what I have described.

You turn arduino on with a momentary button
Arduino activates a relay in setup (code)
This relay is parallel to your 'on' button and takes over powering the arduino
Arduino does its thing
Arduino turns relay off
Arduino loses power and turns off until next time you press the button.

In short:
You turn on
arduino does its thing
Arduino turns itself off

Hmm, I see what you mean. Do you think this could this be possible with a traditional IO switch (replacing the momentary button)?

Not without significant complication. This is, however, a reasonably standard idea for many devices. If you forget to turn it off and it is idle for x minutes it turns itself off. You then need to reset it, hence the momentary button. I can think of methods to do what you want without the button but they are all overly complex.

pmagowan:
Not without significant complication. This is, however, a reasonably standard idea for many devices. If you forget to turn it off and it is idle for x minutes it turns itself off. You then need to reset it, hence the momentary button. I can think of methods to do what you want without the button but they are all overly complex.

Alright, thank you very much for your help!

I don't understand why the solution in post #1 or #2 is not working for you. The 12V from the power supply is there all the time, yes? You have to do something to tell the project to wake up; a simple button push seems sufficient, unless you want to add a Real Time Clock (RTC) module and use that to wake up the Arduino at some specified time to run via an interrupt. Then the Arduino itself can go into power down sleep mode, and you can have the rest of the project powered off and on under Arduino control. RTC modules (like DS3231 or DS3234) have battery backup, and a sleeping '328P can be draw just microAmps of current. A latching relay draws no current after it's latch state is switched.

The OP is describing what happens in a PC. The power button is a momentary pushbutton that is connected to an IT8502E JXS Power Chip that holds the Power-On pin of the PSU low, which in turn turns on the PSU. The processor can then control the power supply.

Why is this weird?

CrossRoads:
I don't understand why the solution in post #1 or #2 is not working for you. The 12V from the power supply is there all the time, yes? You have to do something to tell the project to wake up; a simple button push seems sufficient, unless you want to add a Real Time Clock (RTC) module and use that to wake up the Arduino at some specified time to run via an interrupt. Then the Arduino itself can go into power down sleep mode, and you can have the rest of the project powered off and on under Arduino control. RTC modules (like DS3231 or DS3234) have battery backup, and a sleeping '328P can be draw just microAmps of current. A latching relay draws no current after it's latch state is switched.

I'm not quite sure what you mean. The reason it doesn't work is because:
A. if I were to use the schematic I posted, and replace the pushbutton with a relay, it wouldn't be getting enough current, because I'd need to make sure the ground pin of the arduino isn't getting too much current.

B. The relay module I'm using operates on 5v, 12v is too much, I have a voltage regulator for this, but I still face the same problem of not being able to send a signal that does anything from my arduino to the relay, since it's externally powered (I still don't know why this occurs).

I could just have a pushbutton as my power button, but that's not really what I'm going for, I'd like my IO switch to be the only thing that regulates electricity flow (via human input). If it's not achievable, no biggie. I don't need an RTC to do that, literally all I want is a remote signal to be sent from the arduino to the power supply (via a relay, for example) so that it turns off.

Perhaps I'm misunderstanding what you mean. Please bear with me, I'm not an electrical engineer or an expert of any sort.

matteo69:
A. if I were to use the schematic I posted, and replace the pushbutton with a relay, it wouldn't be getting enough current, because I'd need to make sure the ground pin of the arduino isn't getting too much current.

This makes no sense. The Arduino draws what current it needs and no more.

Also, your requirements seem to be all over the place. From one post to the next you change your specifications. Do you want a button or a switch? Do you want a relay or a transistor?

It appears to me that reply #1 is your solution. You push the button to turn the Arduino on, and let the Arduino decide when to release the power.