How to Add a Power Button to Your Raspberry Pi

To keep the price down, the Raspberry Pi doesn't ship with a power button, yet it's easy to add your own! This guide will show you how to add a power button to your Raspberry Pi that can turn your Pi on or off. This is one of the most useful Raspberry Pi projects you can complete!

We're going to use a few scripts that will monitor two GPIO (general-purpose input/output) pins on your Pi and look for when the button is pressed to turn the Pi on or off.

Why is a Raspberry Pi power button important?

You should never "yank" the power cord out of your Pi as this can lead to severe data corruption (and in some cases, physically damage your SD card). You can safely shut down your Pi via a software command or, even better, use a power button or switch (see: this guide). :)

Note: When we "shut down" the Pi, it will send it into a halt state, which still consumes a very small amount of power. This is similar to how all modern computers work. In this guide walk through the process of adding a power button that will both halt and wake the Pi up from a halted state. Additionally, after your Pi has shut down, you can safely disconnect the power supply (should you desire) without the worry of data corruption.

Raspberry Pi 4 Support

Sadly, the default Raspberry Pi 4 bootloader doesn't ship with WAKE_ON_GPIO enabled, meaning you can't wake the Pi using the method we've outline in this guide.

But there's good news: the next version of the bootloader will support this feature. For now, you can install an experimental version of the bootloader manually and continue with this guide. To do so, download these files and follow the instructions in the README.txt.

If you're following along with this guide, it's likely that you're going to have to choose an appropriate button for your project. I'm going to use a pretty large button, but the important part is that you choose a "normally open momentary push button".

There's nothing to build here, but we need to understand how to wake up the Pi from a halt state before we build the shutdown functionality. Simply put, shorting pins 5 and 6 (GPIO3 and GND) together will wake the Pi up from a halt state.

An easy way to test this is to shutdown the Pi with sudo shutdown -h now, and connect pins 5 and 6 with a female to female cable. You only need to short them momentarily. Then you should find that the Pi is "awake".

There are two options for building the sleep functionality: using our install script or installing everything manually. I recommend using the install script, but the manual approach will help you understand how this works.

1. Use the install script

The simplest way to install the required scripts is to clone our power button repository, and run the install script.

Read the next section if you want to understand how these scripts work.

2. Install manually

The wake solution is fairly straight forward, but in order to safely shut down the Pi we're going to have to use a software solution. Since we know we're going to use pins 5 and 6 to wake the Pi, let's also use these pins to shut it down. That way we can wire up our button to those pins and it will both shutdown and wake the Pi.

We'll have to write a script that listens for a button press and shuts down the Pi. Before we look at the script, let's discuss a few different approaches. First, we could write a script that starts an infinite loop and waits for a change in the state of the GPIO pin. Using pseudo-code, that might look something like:

while True:
if GPIO3 is pressed:
shutdown the pi

While this would work, and probably doesn't have any real performance concerns, there's actually a better way. We can listen for an interrupt (edge detection). An interrupt is the change in state from LOW to HIGH or HIGH to LOW. The RPi.GPIO library provides a method called wait_for_edge that will block execution of our script until an interrupt is detected. So we'll setup GPIO3, and once we detect a FALLING edge we'll shutdown the Pi.

The first thing we'll do is wire up a button and test the functionality using a breadboard. This is a very simple circuit, but it's good to make sure it works before we move on.

You can see the image below for details, but I'll explain what's going on. I'm using two male to female jumper cables. I'm connecting one to pin 5 (SCL) and the other to pin 6 (ground). If pin 6 is already taken by something else, you can use any other ground pin instead.This is a great resource for identifying all your pins!

Then, I'm connecting them to the breadboard with a button in between. Since the button is normally open, the circuit will be open until the button is pressed. Then when the circuit is complete, it will either wake up the Pi if it's in a halt state, or it will shut it down if it's awake.

Then when I press the button, I confirm that I'm my session is lost. To get a step further, I can confirm that the network is down on the Pi by using arp-scan.

sudo arp-scan --interface=en0 --localnet

I won't go into detail about how to use arp-scan in this guide, but if you use the correct interface this should work.

You can check the output of arp-scan and make sure that the Raspberry Pi is not connected to the network.

Then go ahead an press the button again to wake it up. If you wait a few seconds, the Pi should be back up. You can test again using the arp-scan command. If the Pi is up you'll see output similar to this:

Now it's time to wire up the button we're going to use for the project. I'm going to use a jumper cable still so the button can be easily detached from the Pi. But I'll strip the other end and solder it onto the button.

Note:

If you're using a Pi without a GPIO header (pins) soldered to it, like a Pi Zero, you can solder directly to the Pi Zero's GPIO through-holes.