Does the Raspberry Pi have a power switch?

So you’re wondering if the Raspberry Pi has a power switch? The short answer is Yes. If you short pins 5 and 6 together it puts the Raspberry Pi into a HALT state, putting the device into a low power mode state. Shorting the wires again will turn it back on. So, you are probably wondering how to implement this. To make this work we need a momentary normally open (NO) switch.

A momentary normally open switch is one where the switch remains in an “off” state until it is pressed by the user. Take a look at the diagram below:

NO/NC Diagram

A normally open switch makes no connection to the rest of the circuit until the user presses the switch. On the flip side, a normally closed switch breaks the connection when a user pushes the switch.

As mentioned above we need a momentary switch. This is due to the fact that we only need to short the 2 pins briefly. So what are some examples of a momentary normally opened switch? Despite this fancy term, this is essentially a push button, like the one pictured below:

Momentary NO Switch (Push Switch)

Adding a switch to the Raspberry Pi

To add a switch to the Raspberry Pi, simply take one end of the switch and connect it to pin 5 on the GPIO. Take the second end and attach it to any GND pin. For simplicity I have attached this to pin 6.

The diagram below shows were both connections should be made. They are marked with a green square:

Note: To view this GPIO diagram from the right angle the SD card on the Pi should be at the top

Once wired up, it should look similar to the reference picture below:

Wiring demonstration for a Raspberry Pi power switch

Shutting down the Raspberry Pi with a python script

Now that everything is connected, we need to write a small python script which can be found below

import RPi.GPIO as GPIO
import subprocess

pin = 3

GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.wait_for_edge(p, GPIO.FALLING)

subprocess.call(['shutdown', '-h', 'now'], shell=False)

Save this code as a file named shutdown.py on your Raspberry Pi. Take note of where you saved the file as it is important for a later step. Alternatively, you can get the code directly from git by using the following command:

git clone https://github.com/ConorJOHanlon/Raspberry-Pi-Shutdown.git

When you clone this repository from git, it will create a folder named Raspberry-Pi-Shutdown and inside that folder you should see a python script named shutdown.py.

To run this script, we need to make it executable with the following command:

sudo chmod +x shutdown.py

This python script will run in the background and detects a short between pin 3 and ground. After the short is detected, it will shutdown the device to a halt state. When the Raspberry pi is in a halt state, it does not need a script to turn back on. Simply shorting pins 3 and ground will boot it back up.

Run the python script in the background on boot

The final step you will need to do, is run this script on system boot in the background so that it is always actively listening. To do this we need to edit /etc/rc.local

sudo vi /etc/rc.local

Add the following line to the end of the file keeping in mind the location of the shutdown.py script:

sudo python /home/pi/Raspberry-Pi-Shutdown/shutdown.py &

Save the file and reboot the system.

And that’s it! Now you have a fully functioning power switch on the Raspberry Pi. Please note that power has to be supplied to the Raspberry Pi at all times for this to work.

Alternative methods to add a power switch to the Raspberry Pi

There are a number of other ways to add a power switch to your Raspberry Pi.

Power strip with switch

You could simply use a power strip with an on/off switch like the one pictured below:

Amazon Basics Power strip

This method is not really recommended as abruptly disconnecting power can damage the Raspberry Pi or corrupt the SD card. It can be useful if you just want something simple.

The exact one pictured can be purchased on Amazon:

AmazonBasics 6-Outlet Surge Protector Power Strip with 2 USB Ports – 1000 Joule, White

UPS Battery

A UPS (Uninterruptible Power Supply) can make your Raspberry Pi portable by adding a battery. Usually, a lot of UPS’s for the Pi, come with power switches such as the PiSugar or PiJuice. They also come with other features such as user programmable buttons!

The PiSugar is available to purchase on Amazon for both the Raspberry Pi and Raspberry Pi Zero:

PiSugar

Pisugar2 Portable 1200 mAh UPS Lithium Battery @ Amazon

Pisugar3 Plus Portable 5000 mAh UPS Lithium @ Amazon

PiJuice

The PiJuice was created by Pi-Supply. Similarly to the PiSugar, it is available for the the standard size Raspberry Pi and the Zero. Both versions can be found on their website here.

Checkout other power options for the Raspberry Pi here!

Leave a Reply

Your email address will not be published. Required fields are marked *