Self shutdown circuit and code

This will be in a car that will provide 12v constant and 12v switched to the board. I'm trying to figure out how to get arduino to power up when I apply power (via 12v switched), and then run a shutdown sequence when the key is switched off (12v constant can provide power during this time).

I'm assuming it doesn't work because of my terrible circuit, but i'm hoping to get some guidance.

int analog0;

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(2,OUTPUT);
  digitalWrite(2,HIGH);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(100);                       // wait for a second
  analog0 = analogRead(A0);         // read key voltage to check if key is still on

  if (analog0 < 100){                // if key is off run self shutdown code
    digitalWrite(LED_BUILTIN, HIGH); // turn LED on
    delay(2000);                     // wait 2 seconds
    digitalWrite(2,LOW);            // cut power
  }

How did you calculate the base resistor? I suspect that it is not letting the transistor turn on because the collector current is really quite small.

5V - transistor drop to power the Arduino thru Vin, which needs to see ~7V for the 5V regulator, is not going to work.

More typical is P-channel MOSFET, gate pulled high to 5V in this case.
Switch pulls gate low to supply power to 5V, Arduino then takes over to hold gate low to keep the MOSFET on. Releasing it lets the gate go high to turn off the FET.

And of course Arduino Gnd to the 5V supply Gnd.

P-Mosfet power switch.JPG

Or with an NPN buffer if the source voltage is higher. PNP might be better instead of P-channel in that case also, as some P-FETs won't like -12V Vgs.

P-Mosfet power switch with NPN.JPG|689x517

neongreen: I'm assuming it doesn't work because of my terrible circuit, but i'm hoping to get some guidance.

You are using an emitter follower, which is no use for switching a load, you need a common-emitter circuit for switching. Emitter-follower is used for buffering an analog voltage and always loses some voltage.

To switch on the high-side you need a PNP in common-emitter config, or a pFET in common-source.

SteveMann: How did you calculate the base resistor? I suspect that it is not letting the transistor turn on because the collector current is really quite small.

To be honest, I don't know how to calculate the base resistor. I just used 1k because it was the value used in a similar application.

CrossRoads: 5V - transistor drop to power the Arduino thru Vin, which needs to see ~7V for the 5V regulator, is not going to work.

Ah yes, that makes sense, that would be why I was seeing 4.5 on the emitter side. Duh.

More typical is P-channel MOSFET, gate pulled high to 5V in this case. Switch pulls gate low to supply power to 5V, Arduino then takes over to hold gate low to keep the MOSFET on. Releasing it lets the gate go high to turn off the FET.

Ok, I'll look into p- type MOSFETs and pnp resistors. Thank you.

I think you could do it with something close to your original circuit, if you simply change to low side switching. (Not claiming it would be optimal, just possible.)

If you figure the Arduino needs 100 mA of current, that would translate to 10 mA of base current being safely in excess of what is required, but still well within the limits of the Arduino output and transistor power. That would translate to a base resistor of (5.0-0.6)/.01 or roughly 470 ohms (rounding to a very common value). S.

Hi Neon,
In your original circuit think about where the current for the base comes from, or would come from if it worked.

PerryBebbington: Hi Neon, In your original circuit think about where the current for the base comes from, or would come from if it worked.

I've attached a pic that shows my thinking, my intention is that this is the path used at all times except initial start up.

Screenshot 2021-03-02 135914.png|483x360

Ok, good. Keep thinking about it, in your schematic how does the current get back to ground?

Although it does not exactly address what you are doing you might find this helpful https://forum.arduino.cc/index.php?topic=653831.0

PerryBebbington: Ok, good. Keep thinking about it, in your schematic how does the current get back to ground?

Hmmm, ok. I guess this is why NPN transistors are used to switch grounds?

I'm struggling to think of a question that doesn't give the answer away.

Your statement is only partially true and only if the supply is 0V and +something. Think about a supply that's 0V and -something.

I want you to think about the tutorial I gave you a link to and explain in words and a schematic why what you proposed won't work.

Good electronics test question though ... (Will this circuit work ?)

PerryBebbington: I want you to think about the tutorial I gave you a link to and explain in words and a schematic why what you proposed won't work.

I want to say that it won't work because there is nowhere for the base current to go because there is not sufficient potential across the base. When used properly, the NPN has a potential difference between base and emitter. In this design, the emitter voltage is the same as (or potentially slightly higher than) the base. When I close the switch feeding current to the left side transistor, the arduino powers up. I assume that is because of the voltage drop across the transistor (which I have measured to be about 0.5v) which allows the base current to flow into the emitter.

Your statement is only partially true and only if the supply is 0V and +something. Think about a supply that's 0V and -something.

This went over my head (which is not hard to do).

CrossRoads: Or with an NPN buffer if the source voltage is higher. PNP might be better instead of P-channel in that case also, as some P-FETs won't like -12V Vgs.

|500x375

Interesting, what is it about P-channels that makes them more sensitive to voltage? I mean 12v doesn't seem so high... Anyway, I pulled from your idea and modified a bit (no buffer PNP, but I could add one in if needed. The only issue with your schematic is that the switch needs to be a switched 12V since it's a signal coming from the ignition switch of the vehicle. I'm curious to know what you think.

EDIT: I just realized that my circuit, as drawn is feeding 12v to the signal pin, and that's probably a big problem... I should be pulling up to 5v, not 12v. I think...

|500x375

DSC_0138.JPG|4618x3464

That mod puts 12V on the Signal pin from the 12V pullup. That’s why I had the NPN driven from the Arduino.
If you only pull the P-FET gate to 5V with a 12V source, it may not turn fully off.

N- and P-Channel MOSFETs are basically voltage driven devices, while NPN and PNP are current driven. It's the physics of their internal structures. Their low Rds (on-state resistance, can be low 10s of millohms) vs the more or less fixed on-voltage drop across BJT (typically around 0.7V) means they will have less voltage drop across and them and dissipate less power.

Example, 1A total current load, a BJT with Vce of 0.7V will dissipate P=IxV watts, = 1A x 0.7V = 700mW While a MOSFET with say 50milliOhm Rds will dissipate P=I^2xR = 1A*1A*.05ohm = 50mW

Hello Neon, Good that you're thinking about this.

In your hand drawn schematic in reply #15, suppose that the 12V on the pin wasn't a problem, there is an entirely different reason it won't work. Ask yourself what the output on 'Signal' will be when the power is removed by the MOSFET, and what effect that will have.

PerryBebbington: In your hand drawn schematic in reply #15, suppose that the 12V on the pin wasn't a problem, there is an entirely different reason it won't work. Ask yourself what the output on 'Signal' will be when the power is removed by the MOSFET, and what effect that will have.

I was thinking about this as I was trying to sleep last night. Here goes: The MOSFET cuts power to the MCU when the signal pin is "high". But as soon as the MCU loses power, it is no longer able to hold the pin "high, and power comes back on.