There have been several discussions on the forum of how to turn an Arduino on and off using a momentary contact pushbutton and some electronics. For a multi-node CAN bus project, with a 24V-DC power source, I need a bit more. If the MCU is ON, but has been inactive for a while, it sends messages to stop all outputs, puts the other nodes on the network to sleep, puts the CAN controller and transceiver to sleep, and then puts itself into PWR_DOWN sleep. Moreover, before turning power OFF, one node must do the same orderly shut down of the other nodes in the system. I want one button to do all of this, and a simple push-on, push-off switching of DC will not do.
I wanted the pushbutton to do three things:
(1) turn power ON if it is OFF
(2) turn power OFF, with an orderly shutdown of the CAN network, if it is ON and the system is AWAKE
(3) interrupt SLEEP if the system is sleeping
The schematic of what I've come up with is attached as a jpg, and the attached zip contains pdf and DesignSpark files for the schematic and pcb layout, the data sheet for Recom R-78AAxx-0.5_SMD DC-DC converters, and a small sketch to show how I'm using it. My test board was just built with through-hole components on a piece of scrap board, while the PCB (not yet ordered, so spotting errors would be appreciated) is formatted to mate with an Arduino Nano and with my CAN boards (already on hand and functioning), but has two extra rows of pads (as do the CAN boards) to make it easy to connect IO lines.
Here, I'll briefly describe the circuit.
The Recom (as for a number DC-DC switching converters from various manufacturers) has a sense pin (Vr in the schematic) that has to be either floating or high to turn the converter on. The push-button connects 24V to the R1/R2 voltage divider to feed ~5V to Vr via diode D1. R1 and R2 were chosen so that the voltage on Vr is within Recom's specs whether the 24V-nominal batteries are nearly discharged or if there's a battery charger still connected (i.e. from < 23.4 to > 29.4 volts). These resistors are paralleled by a 10 uF capacitor to keep Vr high for a hefty RC time delay. Vr is kept alive (through 47K current limiter R5) from pin A1 of the Nano, used as digital output, and R4 makes sure that pin A1 is pulled down if the Arduino power fails. A1 is set OUTPUT, HIGH during Setup () thus keeping the DC-DC converter turned on. Any pin could be used for this; A1 was not yet being used for other things. For a source voltage different from 24V, R1 and R2 would need to be changed. Their sum should be kept about the same, however, to give the same RC delay. As shown, the delay is sufficient to get Setup through the digitalWrite (A2, HIGH) instruction with a deliberate momentary push on the button, but not with just a "tap".
A single transistor inverts the voltage from the R1/R2 divider and this connects to Arduno pin 2 (= interrupt 0) which is configured as INPUT_HIGH. I just used a 2N2222 and 100k bias and pull-down resistors for the trial board, but the schematic shows a transistor with those resistors buit-in. Voltage can not get directly to the transistor's base from Vr because of D1. Hence, pin 2 will go LOW whenever the pushbutton is pressed, then decay back to high when it is released. If the Arduino is running, the sketch tests for digitalRead (2) == LOW to set A2 LOW and turn OFF the DC-DC converter. If the Arduino is sleeping, pin 2 going low interrupts the sleep. If the Arduino is OFF, the state of pin 2 is irrelevant except that the sketch must make sure that it goes low by the end of Setup so that a shutdown is not immediately triggered on entering Loop.
The actual CANbus master node sketch is quite lengthy and does lots of other things, and I'll eventually post something about that and the Nano-sized CAN boards. For the demo sketch I've extracted just a few pieces from that, and added some lines so that you can see how it works with the Serial Monitor with or without having built an actual DC-DC board.
This circuit functions well and I think that I've designed it in a way that the Arduino is well protected. Some of the more experienced people here, though, may find flaws. If so, I'd dearly like to hear from you before I have boards made or, worse yet, fry something.