Implementing soft switch

Hello, I'm new to the forum. I know next to nothing about circuit design so if you could help me I'd be very thankful.

I'm trying to implement a soft switch for Arduino Mega. The idea is that a momentary push button would turn on the Arduino Mega (which I've converted to run on 3.3V, but still at 16MHz) and then the software would wait for a few seconds, read the button again and then set a pin high which would keep the device on. Turning the device off would work the same way. The software would read the the state of button and set the power pin low when the button would be pressed long enough.

So far I have this:

The circuit

(Also as an attachment.)

-LOAD represents Arduino board and accompanying devices
-SW1 is a momentary push button (power switch)
-switch PIN_PW represents the pin on Arduino that keeps the power turned on
-PIN_B represents a pin that reads the button state (INPUT_PULLUP)

The diode D2 protects PIN_B and diode D1 prohibits PIN_B going to ground through power MOSFET TR1.

Problems that I see:
-The forward voltage of D1 is around 0.8V so that leaves only 2.5V for Arduino. Is that enough to keep it running? According to the datasheet ATmega 2560's voltage range is 1.8-5.5V. I could maybe use a special schottky diode with voltage drop of 300mV at 1amp and that would leave 3.0V for the Arduino.
-I'm planning to use LM1117 3.3V regulator. This uses about 2mA even when the device is turned off and will drain the battery eventually. It would be nice if I could cut off the regulator, too. But how?

Would you have other thoughts or suggestions?

This functionality can be accomplished with some ordinary toggle switch.

Also a controller can be sent to sleep, better than cutting power.

16MHz is not supported with 3.3V supply.

Hello mmikkone,

Welcome to the forum. Please read 'how to use this forum - please read' especially the bit on posting images, which I accept is a pain to do.

Not withstanding the comment from DrDiettrich that using sleep might be better, what do you think the voltage on PIN_PW will be when SW1 is not pressed and TR1 is not conducting?

(I am deliberately not telling you the answer because I want you to think about it).

"turn on the Arduino Mega (which I've converted to run on 3.3V, but still at 16MHz)"

How is that working out? The Atmega2560 datasheet says it needs 4.5V for all speeds. I would suspect some odd behavior might occur running 16MHz at 3.3V.

I don't understand PIN_PW. You have it drawn as a switch, but presumably it's just a GPIO pin.

Anyway, yes you can do a soft switch to control things at the battery. But that usually involves two transistors, one to switch the higher voltage, and the second to control the first. Here's an approach that I think is right for high-side switching:

ShermanP: Thank you, that youtube video looks like exactly what need. And yes PIN_PW is GPIO. I just made it as a switch so that I (and you) could test the simulation.

PerryBebbington: This is how I thought it would work: When device is off and the SW1 is pressed, that connects Arduino to ground and turns it on. The voltage at PIN_PW is zero. Then Arduino keeps checking the state of SW1 at PIN_B (after bootloading) and if it's pressed long enough, PIN_PW is set HIGH. This turns on TR1 and connects Arduino to ground through TR1. SW1 can be released, TR1 keeps Arduino connected to ground and PIN_PW will remain HIGH. When the user wants to shut down the device, he will press SW1, which is detected at PIN_B. If the button is pressed long enough, the software will decide to turn off the device. It sets PIN_PW LOW, TR1 turns off and the device looses connection to ground and turns off. At least that's what I imagined would happen. But I'm guessing there's something that I missed?

DrDietrich: Interesting idea. Still the voltage regulator would keep drawing 2mA, but it would do so in my plan also. ShermanP's video looks like a solution.

Crossroads: I changed the regulator as AMS1117 and soldered an extra diode. There's a tutorial where the same thing is done for Uno. Actually my board is a Chinese small footprint version (about half the size of Arduino Mega), but I don't think it's very much different form Mega. The processor is ATmega2560 and there's a 16MHz crystal for the processor on it. There's also a 12MHz one but I believe it's for something else (USB serial?). It's been running fine for over a year and I have two of these boards, they both run fine.

Crossroads: I dug out the datasheet for ATmega2560 and according to that the processor tops out at 8MHz at 3.3V. I don't know what's happening, but my boards just keep working.

mmikkone:
PerryBebbington: This is how I thought it would work: When device is off and the SW1 is pressed, that connects Arduino to ground and turns it on. The voltage at PIN_PW is zero. Then Arduino keeps checking the state of SW1 at PIN_B (after bootloading) and if it's pressed long enough, PIN_PW is set HIGH. This turns on TR1 and connects Arduino to ground through TR1. SW1 can be released, TR1 keeps Arduino connected to ground and PIN_PW will remain HIGH. When the user wants to shut down the device, he will press SW1, which is detected at PIN_B. If the button is pressed long enough, the software will decide to turn off the device. It sets PIN_PW LOW, TR1 turns off and the device looses connection to ground and turns off. At least that's what I imagined would happen. But I'm guessing there's something that I missed?

That is exactly what I thought you were thinking would happen.

Yes, you missed something and I am still reluctant to tell you what it is as I still want you to work it out for yourself as I know you will learn something from doing so. It's a common mistake for people new to this and it's one that's important you learn from. I repeat my question because the answer to my question points you at what you missed. So, my question, which you have not answered, was:

What do you think the voltage on PIN_PW will be when SW1 is not pressed and TR1 is not conducting?

Ok... I was thinking maybe pin_pw would be internally somehow disconnected? On the other hand the whole upper part of the diagram is at high potential, also Arduino. So maybe it's high? And thus would turn on TR1.

If I connect ground to ground there isn't much current flow.

mmikkone:
Ok... I was thinking maybe pin_pw would be internally somehow disconnected?

Regardless of whatever else is going on inside the chip all inputs and outputs have internal protection diodes that prevent them from going above about 0.6V above Vcc or below about 0.6V below ground, for that reason alone the pins are never disconnected.

On the other hand the whole upper part of the diagram is at high potential, also Arduino. So maybe it's high? And thus would turn on TR1.

Correct. As soon as TR1 is turned off the ground pin goes high, as soon as that happens PIN_PW goes high and turns TR1 on again.

It was already observed that a controller can be powered from input pins, through the integrated protective diodes. That's not normal operation, anything weird can happen then.

DrDiettrich:
It was already observed that a controller can be powered from input pins, through the integrated protective diodes.

I'm not clear if that is in response to my previous post. If it was then I think you have misunderstood what I meant. I'm not saying the Arduino will be powered through the protection diodes, I am saying that TR1 will turn on just after it turns off becuase turning TR1 off removes the ground from the Arduino, which means PIN_PW can no longer hold its gate low, the gate will go high from Vcc through the protection diodes to PIN_PW to the gate, turning TR1 back on and powering the Arduino again.

CrossRoads:
"turn on the Arduino Mega (which I've converted to run on 3.3V, but still at 16MHz)"

How is that working out? The Atmega2560 datasheet says it needs 4.5V for all speeds. I would suspect some odd behavior might occur running 16MHz at 3.3V.

The product page gives 4.5-5.5V in the description, but 1.8-5.5V in the parameters list.

Confusing indeed. The data sheet clears that up, a bit at least: the ATmega2560 is 4.5-5.5V, but the ATmega2560V can do 1.8V and up (like most other AVR chips).

So maybe OP has the V chip, and 3.3V is OK.

That said, 16 MHz is indeed out of spec, and therefore may or may not work. p.358 of the data sheet has the speed grades, and it's rated to work at not much more than 8 MHz at 3.3V. So for stable operation lowering the speed is in order.

16MHz at < 4.5V? Is brownout detection turned off? Is stable operation not desired?

The 328P datasheet is more than clear about running at too low VCC for the clock rate.
Disable the watchdog and run an unstable core if you want. The IO might be interesting!

Speed Grade:
– 0 - 4MHz @ 1.8 - 5.5V
– 0 - 10MHz @ 2.7 - 5.5V
– 0 - 20MHz @ 4.5 - 5.5V

........

32.3. Speed Grades Maximum frequency is dependent on VCC. As shown in Figure. Maximum Frequency vs. VCC, the Maximum Frequency vs. VCC curve is linear between 1.8V < VCC < 2.7V and between 2.7V < VCC < 4.5V.

Linear between 2.7 and 4.5 volts, taking 2.7 to run at 10.... 16 is not even close to 3.3V!
10/2.7 = 16/4.3
I can see a little bit of fudging but,
16/3.3 = 10/2.06 = no way the AVR core is running 10MHz at 2V!

wvmarle:
So maybe OP has the V chip, and 3.3V is OK.

It says "ATMEGA2560 16AU 1736" on the chip. Maybe that tells you something. I also measured the clock speed with Nick Gammons piece of code from here: [SOLVED] determine CPU speed in software ? - Programming Questions - Arduino Forum and got results:

Time taken = 17048

Time taken = 17044

Time taken = 17044

Time taken = 17044

17ms taken for 16ms delay request. Approximately as expected.

Nick's post:

On my 16 MHz processor (for which this was compiled) I got:

Time taken = 15956

17044 is 93% as fast on the same clock. I don't expect that chip to run solid especially if you do even average project IO.

Do you understand that engineering recommendations are not challenges? That running in margins is not so smart?

Stay in the safe region to EXPECT stable behavior, that is the guarantee. There is no guarantee how far you can play outside the safe region while juggling 1 ball just like there's no organized sport dodging traffic while jaywalking, climbing electric towers or playing with matches in a dry forest.