One of the challenges with using a Raspberry Pi as part of a gadget or appliance is having an on/off switch. What I have tried to achieve here is to have a switch mechanism that allows me to use a dual momentary switch to turn the Pi on or off. When turning it off, the Pi needs to first shut down before power is disconnected. The latest arrival from The Pi Hut is now part of a prototyping exercise:
After doing a little bit of reading, I settled on a concept that has been used elsewhere. To power up, a momentary switch first charges a capacitor, which discharges via a transistor, which pulls the input of a Schmitt trigger inverter high for long enough for the Pi to boot. During the boot sequence, the Pi pulls a GPIO pin high – this pulls the base of a second transistor up, making sure that the input of the Schmitt trigger inverter stays high.
The output of the Schmitt trigger inverter is connected to the gate of a P-layer MOSFET, which acts as the power switch for the Pi. A P-layer MOSFET is used because I want the MOSFET to switch the Pi’s 5V input, and leave the ground connected when it is off.
Once the capacitor is discharged, the GPIO pin, which is kept high while the Pi is running, keeps the power on. At the moment, that is accomplished simply by adding the following lines to /etc/rc.local (I have installed WiringPi):
# hack to bring GPIO 0 up /usr/local/bin/gpio mode 0 out /usr/local/bin/gpio write 0 1
When the Pi shuts down, and goes into its “low power” off mode, the GPIO pin eventually goes low. At that point, the input to the Schmitt trigger inverter goes low, resulting in the gate of the MOSFET being pulled up to 5V and switching off the power supply to the Pi.
I still need to make some improvements on this, including:
- replacing the LS part with a HC part – LS logic was low-power by 1980’s standards, but not anymore!
- putting a resistor either on the collectors of the BJTs or the input of the Schmitt trigger inverter.
- possibly swapping the BJTs with MOSFETs – if I can get the power consumption down. That will however require a second inverter (there are 6 on the Schmitt trigger inverter chip). 2N7000s are cheap as well!
- possibly putting a resistor on the gate of the P-layer MOSFET
The second part of the switch is the “off-switch”. That part is connected to a GPIO pin that is set as an input. When it is triggered, the Pi initiates a shutdown, which will result in power to the Pi being cut. I’ll describe that more in another post!