Auto Power Off - not working :(

Hi all,
I've tried a couple of ways to make Arduino cut power through a digital pin.
The first is based on a PNP (push button to start power, then digital pin set LOW to maintain it, set HIGH to cut).

The second is based on an NPN (push button to start power, then digital pin set HIGH to maintain it, set LOW to cut).

In both cases, if I don't connect the digital pin, pushing on the button, power goes on. But if I connect the pin, power goes on immediately.
Is it something related to the fact that, by default, pins are INPUT? Please help, do you have any suggetion?
Thanks!

You need more than an Arduino to do anything like this. You need some code on the Arduino. If that code isn't working properly, and you want help to fix it, you need to do something about the fact that we can't see your code.

Thanks PaulS, you're right, Here's my code for the first case:

/*
  Blink
  with auto-power-off (PNP)
 */

void setup() {                
  pinMode(2, OUTPUT);      // digital pin as output
  digitalWrite(2, LOW);   // keep power on
  
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);     
}

void loop() {
  for (int i=0;i<10;i++)   // blink LED ten times
     {
	  digitalWrite(13, HIGH);   // set the LED on
      delay(3000);
      digitalWrite(13, LOW);    // set the LED off
      delay(1000);
	 }
  
  digitalWrite(2, HIGH);  // force power off
}

and here's the code for the second case:

/*
  Blink
  with auto-power-off (NPN)
 */

void setup() {                
  pinMode(2, OUTPUT);      // digital pin as output
  digitalWrite(2, HIGH);   // keep power on
  
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);     
}

void loop() {
  for (int i=0;i<10;i++)   // blink LED ten times
     {
	  digitalWrite(13, HIGH);   // set the LED on
      delay(3000);
      digitalWrite(13, LOW);    // set the LED off
      delay(1000);
	 }
  
  digitalWrite(2, LOW);  // force power off
}

I think we need to see a wiring diagram, now. What are you trying to cut power to?

In both of the sketches, the blinking LED does nothing useful. After 40 seconds, one sketch will unconditionally turn the pin off. The other will unconditionally turn the pin on.

Neither knows anything about the button you are pushing.

PaulS:
Neither knows anything about the button you are pushing.

The first entry shows two variants even.

In both cases, if I don't connect the digital pin, pushing on the button, power goes on. But if I connect the pin, power goes on immediately.
Is it something related to the fact that, by default, pins are INPUT? Please help, do you have any suggetion?

If pushing the button has any effect, your wiring must be different.
You cannot use an Arduino input pin, to switch the Arduino ON (probably I misunderstood your post)
You are feeding a high base current into your transistors. Are they still alive?

Hi,
thanks a lot for your response.

My idea as a newbie was to apply the same schema used to control a load through an Arduino pin using a transistor - the only difference being that the load is the Arduino itself.

In both the schematics, I first bias the transistor into conduction by pressing the external momentary push button (normally open) in the schema. Then I'd expect that stating digital pin OUTPUT and putting it in the proper state should have worked...

Consider that, in both cases, if I don't connect the digital pin 2 of Arduino board, I'm able to power the board as long as I keep button pressed (don't know if it's safe for the transistor)

but, as soon as I connect the wire to the digital pin, without pressing the pushbutton, power goes on immediately...

Your schematics must be wrong. Same error in both. You can't have the base of the transistor hardwired to Gnd or to 5v.
The pushbutton should have no effect other than to put some current through the 2k resistor.

Secondly, I wouldn't put the Arduino software into the turn-on loop. How long must the user push down the button, then?
A second transistor, inverting the first, can provide immediate latching current to keep the first transistor on, even after the briefest push.

To turn off, the Arduino output pin, through another resistor, either turns off the first transistor, or turns on the second.

Lastly, use your first configuration. It doesn't compromise the Arduino ground by the .2 v C-E voltage drop. Ground is universal.
The Arduino can run on 4.8 instead of 5.0 volts.

Hi! I'm really sorry, I've realized that the schematics are wrong... they don't correspond to the current wiring...

Here's the correct one for the first option I've tried:

And here's the correct one for the second option I've tried:

Sorry for messing things up

Without connecting digital pin 2, in both options a short push on the button turns on the board, but connectiong digital pin 2 the board goes on immediately... is it because at startup digital states are dangling?
Thanks a lot for your answers

Try a pulldown resistor on the base (second circuit), maybe touching the pin 2 wire is triggering the transistor.


Rob

Yes, you need a pulldown (or pullup for the first circuit) on the transistor base.

Perhaps even a small capacitor in parallel with that resistor to eliminate any brief startup glitch from acting like a real digital actuation.

Thank you for the suggestions.

I've tried to add a pull-down resistor (in option 2):

But it has no effect. As soon as I give power, the board goes on. Another strange thing is that, after 10 blinks I set digital pin2 LOW, but it has no effect (the board remains on).

void setup() {                
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);   // keep power on
  
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);     
}

void loop() {
// do something  
for (int i=0;i<10;i++)   // blink LED ten times
     {
	  digitalWrite(13, HIGH);   // set the LED on
      delay(3000);
      digitalWrite(13, LOW);    // set the LED off
      delay(2000);
	 }
  
digitalWrite(2, LOW);  // force power off   <<<<<<<<<<<<<<
}

I also tried connecting the pulldown resostor from digital pin 2 to ground...same behaviour...

I'll try to add a cap, but I really don't understand why, setting pin2 LOW, it does not cut power...

As soon as I give power, the board goes on.

Sure. Assume the transistor is off, Arduino has no GND connection, so everything ( including pin2 ) is at 5V level.

im not really sure what the overall goal is here but ill try to help anyway
maybe try a circuit where the transistor is between the 5V source and the 5V input of the arduino.
Pin 2 needs to be hooked up to the transistors emitter and use the pushbutton to bypass your transistor.
if you want u could also use a pull down resistor

on a related note, what kind of power source are u using? (usbcable,battaries of adapter cable)
and please be sure that the power you try to cut is not the 5v output on the arduino board and that if your board is still connected with a usb cable tinkering with supply voltage is rellay not recommended (by me) and also pretty difficult.

Thanks daatse for your suggestion.

My goal is to build some sort of light game for kids. I'd like to switch it on pressing a button and then have it automatically switch off after some time.

I've moved the transistor and now, when I connect digital pin2, the board does not go on (as I wanted). When I press the temporary push button it goes on (as I want). The only problem remaining is that it does not remain on, even if I state digital pin as an OUTPUT and set it HIGH.

The schematic I'm using now is the following one:

To be sure that digital pin2 is HIGH, I've tried to detach it from the base of the transistor and I've connected it to a LED: the LED lights up as expected... Any suggestion why it is not able to bias the transistor into conduction?
Thanks!

The emitter of your transistor goes to 5V on the Arduino (which is HIGH). So pin 2 HIGH means the same voltage level as the emitter has. No voltage difference, no current from base to emitter. You probably need an additional transistor to drive the base?

i asume you would like turn of the arduino to save battery power? otherwise i really see no point of shutting youre arduino down.

therefore this link is worth looking in to
http://arduino.cc/playground/Learning/ArduinoSleepCode

mabey worth a shot(although im not sure):
-remove youre diode and resistors and put a resistor between the emitter and the arduino
-use a darlington transistor

You probably need an additional transistor to drive the base?

My LTSpice simulation shows that a pulse at the ON_Button switches Arduino on,
and a HIGH D2 would keep it on. D2 LOW switches it off.

ArduinoON.png

Thans pylon,
the idea to add an additional transistor has made me test the following schema:
(BTW, I've added an optocoupler rather than a transistor because I was afraid to fry pin2)

and it works!!!

Pressing the pushbutton, LED starts blinking: I then release the pushbutton and led flashes 10 times, than power goes down!

Thanks michael_x for your testing... I'm going to test the schema you have proposed.

Does anybody see any disadvantage in mine?

Thanks daatse for the reference to sleep code, but my goal is to switch completely the board off

I'm looking to implement a self-power down function in my project too. This looks like a cheaper alternative to buying a $6 Pololu switch and paying another $5 in S/H. And I might just have an optocoupler lying around. Although I don't understand the need for it?

You must have double posted, the mods don't like that at all? Anyway, I responded with one method I've used in your other post.

Lefty