Raspberry Pi Hardware Reset and Shutdown Buttons

2,286

53

4

Published

Introduction: Raspberry Pi Hardware Reset and Shutdown Buttons

In this project we are creating a little board with hardware buttons to reset and shutdown our Raspberry Pi as it is often times tedious to SSH into the Pi to shut it down properly and certainly better than just switching the power off.

The goal is that we have 2 buttons that, when held for a few seconds will either send a proper reboot or proper power off command to our Pi. A green status LED will show that the script (and the Pi of course) is running. And a yellow LED will indicate when the command to reboot was successfully submitted. The same goes for the red LED and the power off command.

The project is based on the code found here.

Step 1: Gathering the Tools

3 LEDs, I used 3mm

3 Resistors, I used 120 Ohm

6 Pin Connectors

6 Jumper cables

2 momentary switches

Perfboard or breadboard

Soldering Iron (optional if using a breadboard)

Wire

Pliers

Wirestripper (optional)

Multimeter (recommended)

Step 2: The Schematic & Code

Connect all the LEDs to GPIO Pins on your raspberry pi so we can control them later. Don't forget the resistors in between!

Do the same for the switches. I connected all parts with a common ground rail.

If you use switches with more inputs / outputs (like I did) use a multimeter to check which connection actually is made on contact.

I modified code I found here: github.

You may need to install gpiozero first, depending on your OS.

#!/usr/bin/env python3

# import all important libraries
from gpiozero import Button
from signal import pause
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
import os, sys
import time

# Define GPIO 

red = 19
green = 26
yellow = 16
GPIO.setup(red, GPIO.OUT)
GPIO.output(red, 0)
GPIO.setup(yellow, GPIO.OUT)
GPIO.output(yellow, 0)
GPIO.setup(green, GPIO.OUT)
GPIO.output(green, 1)

holdTime = int(3) # Hold Button for at least 3 seconds

# Define Commands

def reboot():
	GPIO.output(yellow, 1)
	GPIO.output(green, 0)
	time.sleep(0.1)
	GPIO.output(yellow, 0)
	time.sleep(0.1)
	GPIO.output(yellow, 1)
	time.sleep(0.1)
	GPIO.output(yellow, 0)
	time.sleep(0.1)
	GPIO.output(yellow, 1)
	time.sleep(0.1)
	GPIO.output(yellow, 0)
	GPIO.cleanup()
	os.system("sudo reboot")
def shutdown():
	GPIO.output(red, 1)
	GPIO.output(green, 0)
	time.sleep(0.1)
	GPIO.output(red, 0)
	time.sleep(0.1)
	GPIO.output(red, 1)
	time.sleep(0.1)
	GPIO.output(red, 0)
	time.sleep(0.1)
	GPIO.output(red, 1)
	time.sleep(0.1)
	GPIO.output(red, 0)
	GPIO.cleanup()
	os.system("sudo poweroff")

# the script
pwroff  = Button(21, hold_time=holdTime)
rst = Button(20, hold_time=holdTime)
pwroff.when_held = shutdown
rst.when_held = reboot
pause()

and save it as shutdownbuttons.py for example.

Follow the instructions on the linked github source to make the script executeable and run it at boot.

$ chmod +x shutdownbuttons.py

and make sure its path is added to /etc/rc.local

sudo nano /etc/rc.local

and add

sudo python home/pi/shutdownbuttons.py

it should look like this:

sudo python home/pi/shutdownbuttons.py &
exit 0

.

Step 3: Solder Everything Together

Use a multimeter to check if there is no short and all connections are good.

Step 4: The Final Product

And here is a short video of the final product (still WIP stage in my case).

Share

    Recommendations

    • Casting Contest

      Casting Contest
    • Microcontroller Contest

      Microcontroller Contest
    • Woodworking Contest

      Woodworking Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    4 Comments

    Nice post! One question---unless I missed something, shouldn't the wires on pin 3 on both Switch 1 and Switch 2 be connected to pin 4 on the switches, so that a switch press will ground GPIO20, or GPIO21 depending upon which switch was pressed? I've been needing this shutdown ability, and will use this post! I'm hoping to monitor input power, and issue a shutdown command with this circuit if the power fails. Thanks for the instructions.

    1 reply

    Thank you for your comment! Alex in NZ also messaged me about that. I used the wrong symbol for the buttons I actually used. But I corrected the schematic now to simple momentary switch buttons. If you use buttons with more poles, use a multimeter to see what connection actually breaks and wire accordingly. But were absolutely right, with the original symbol the connections should have went to pin 4.

    Really neat. Thank you for that :-)

    What a great idea! Such a simple way to reset your project!