Adding an On/Off switch to your Raspberry Pi

Full On/Off Power Switch

Parts for Full On/Off Switch

To build the full On/Off switch, you'll need the following [5]:

  • 2 x 1kohm resistor
  • 2 x 10kohm resistor
  • 2 x 22kohm resistor
  • 3 x Momentary switch
  • 2 x NPN transistor (BC548 used here)
  • 2 x diode (1N4001 used here)
  • 1 x 220µF capacitor (Panasonic ECA-0JM221I used here)
  • 1 x 5V SPDT relay (Omron G5V-1-5DC used here)
  • 1 x LED (3mm red used here)
  • 1 x 2-meter USB A to USB microB cable (24 AWG or higher recommended)
  • Breadboard
  • Jumper wires, both male-to-male and male-to-female

The reset and soft shutdown switch together form a very useful way to turn the Raspberry Pi on and off. However, one problem remains: when the Raspberry Pi is in the shutdown state, it still uses a considerable amount of power and leaves the power LED lit, so it is therefore not particularly suitable to certain applications. This final section of the article looks at a way to create a fully functional On/Off power switch that works much like the switch on your desktop or laptop computer (Figure 7). This solution includes an On switch, a hard Off switch, and a soft shutdown switch, as well as an LED. The solution connects in-line with your 5V microB USB power supply for the Rasp Pi using a spliced USB cable.

Figure 7: Schematic for the full On/Off power switch.

The breadboard configuration is shown in Figure 8. Take a 2-meter-long USB A--to--USB microB cable, cut it in half, strip the positive and ground cables back, and then use the full-size A plug on the input and the microB plug on the output. You can then plug the microB plug into the standard Rasp Pi power input receptacle (top right of the breadboard drawing) and plug the full-size A plug into the USB port on your power supply. (This assumes you have a power supply with a detachable USB cable; if not, you could always use your detachable phone or tablet charger.) The blue wire connects from the breadboard to pin 7 on the Rasp Pi, and the orange wire connects to pin 8. If you have any problems with a voltage drop across this circuit, I recommend buying a 5.25V power supply. These power supplies are custom made for use with the Raspberry Pi, and you can get these either from ModMyPi [3] or Pi Supply [4].

Figure 8: Breadboard configuration for the On/Off power switch.

The circuit is fairly simple:

  1. When the relay (RL1 in the schematic) is in the off state, but 5V is supplied to the input, the voltage travels through the relay and then through a 22kohm resistor to charge the capacitor.
  2. Once the voltage across the capacitor is high enough, it turns on transistors 1 and 2 (Q1 and Q2 in schematic), which connects one end of the relay coil to ground.
  3. When the power-on button is pressed momentarily, the relay is instantly activated. This causes power to be supplied to the output and, as the relay switches contact from pin 1 to pin 10, it also causes the capacitor to slowly discharge. Eventually, after about two minutes, the capacitor will drain to a level where it can no longer keep the transistors in their energized state, which will cause the relay to change back to its original state (removing power from the Rasp Pi). Obviously, this is not what you want, and the following aspects of the circuit allow it to maintain power to the Rasp Pi.
  4. Once the Pi is booted up, pin 8 (or another pin of your choosing – see code examples below) will begin to output 3.3V. This output will turn the LED on and it will also take over the responsibility for keeping the capacitor charged (allowing the transistors to remain energized and the circuit to maintain power to the Rasp Pi). Additionally it provides a 3.3V source for the safe shutdown switch – the safe shutdown switch routes this 3.3V back to a GPIO pin set to an input which, in turn, triggers a safe shutdown to be called within the system.
  5. When a shutdown is initiated (either using the safe shutdown switch provided, a terminal window, or the built-in LXDE functionality), the GPIO output (pin 8 – or another pin of your choosing) will be dropped. This will cause the capacitor to begin discharging, and after about two minutes (as explained in step 3), the relay will switch to its initial state and the power to the Rasp Pi will be dropped.
  6. You can then start the Rasp Pi again using the On button.

The two-minute delay explained in step 3, unlike in other power management solutions, also has the benefit of allowing a system reboot, which can be initiated using a restart button, as explained above in the restart switch section, or in software.

The two-minute delay can be altered by using a different capacitor size – if you want a longer delay, you could replace the 220µF capacitor with something a bit bigger, and if you want a shorter delay, you could replace the capacitor with something a bit smaller.

Using the hard Off switch should be avoided unless absolutely necessary because of the potential for corruption to the SD card with a hard power down. An example of when this might be necessary is if the Rasp Pi has frozen and will not respond. Care should be taken, however, to make sure that the processor activity indicator LED on the Rasp Pi is not flashing when you press the Off switch. If this LED is not flashing (or on solid) it should mean little or no writing to the SD card is occurring, which should reduce the likelihood of a corrupted SD card.

Code Examples

For both the safe shutdown switch and the full On/Off power switch, some code is needed for the safe shutdown functionality to work correctly. The switch causes the script to initiate the shutdown. In Python you have two ways to implement this solution – with a while loop or using interrupts. Open the Python IDE and paste in the code from either the interrupt or while loop options described in the following sections [5]. A later section describes how to load a Python file during the boot process.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content