Ok, so it doesn't look like much.... But why would you need one?
Well you don't, it is helpful if your pi is powered by a supply that also powers other things. A 3D Printer in my case.
Also included is a separate shutdown button which powers down the Pi.
After the Pi cleanly shuts down then power is removed from the Pi completely.
In a nutshell:
A relay controls power to the Pi, one of the Pi's GPIO pins powers a transistor which keeps the relay on until shutdown. Simple enough? Ok, read on for the circuit...
Step 1: The Circuit.
Here is the sketch of the circuit.
Parts you will need are:
- Some Vero/Proto/Bread Board
- Resistors (1/8Watt will do) 3k3,4k7 & 10K
- Transistor 2N2222
- Relay JRC-21F
- Capacitor 1000uF
- An LED
- 2 Push button switches
- Some Wire
- Connectors (I used JST's)
Power to the Pi can be fed through either the GPIO 5V & Ground PINS or you can splice and re solder a micro USB cable.
Once you have assembled the circuit you will need to add some monitoring software to the Pi before wiring it up...That is the next step.
Step 2: The Software on the Pi.
Put these scripts below in the home folder of the Pi. Note the GPIO pins used ... Board PIN 16 is used as the shutdown PIN. Board PIN 18 is used as the Transistor power PIN.
On the pi from a shell script run the command:
sudo crontab -e
and add at the bottom of the file:
@reboot sh /home/pi/pishutdown.sh
Test it working... before you connect the circuit you made boot up the Pi with the software you just uploaded.Either run the python script manually or if you reboot it should run on startup (Note I tested this on a raspbian based OS).
- With a volt meter (or an led with a suitable resistor) check that PIN 18 is producing 3.3V out.
- Connecting PIN 16 to Ground should call the shutdown script and the Pi should shutdown.
Step 3: Powering ON.
Press and hold the button that closes the power circuit on the Pi, and the Pi should boot up. Depending on your setup and what you have installed it may take up to 10s to boot and power the GPIO pins. Once the Transistor pin is powered you can hear the relay "click" and the LED light up. Power button can be released.
Step 4: Shutting Down.
To shutdown the Pi, either use your own software to run the shutdown command, or press the shutdown button and that will initiate the shutdown command.
When the Pi powers down the GPIO the capacitor will start to discharge, with this setup allowing almost 8 Seconds till the capacitor discharges and then the relay is powered off.
Step 5: The Math...
The Relay used is a JRC-21F. According to its datasheet its coil needs 0.22W of Power @5V to operate. So around 44mA of current.
The 2N2222 Transistor has an HFE (gain) of around 56 at 5V and at 60mA current drain. (Figures taken from the datasheet and interpolated between tables)
The Pi is setup with a 3k3 Ohm resistor, with its output at 3.3V the current is 0.001A or 1mA.
1mA fed to the Transistor with a gain of 56 gives us 56mA of current therefore enough to power the coil requiring 44mA.
Capacitor Discharge - this is a little more complicated to put in text. Initial Current drawn is at 1mA, as voltage drops current draw increases. Using an online calculator, discharge time is at 7.9 Sec.
Step 6: Limitations.
With this setup you will not manage a reboot as it may cut the power half way through the boot sequence.
If you want to have this feature you can increase the Capacitor size to 2500uF or larger depending on the reboot time of your setup.