Raspberry Pi Hardware Reset and Shutdown Buttons

8,618

57

9

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).

Be the First to Share

    Recommendations

    • Puzzles Speed Challenge

      Puzzles Speed Challenge
    • "Can't Touch This" Family Contest

      "Can't Touch This" Family Contest
    • CNC Contest 2020

      CNC Contest 2020

    9 Discussions

    0
    fuzzywuzzy.dog
    fuzzywuzzy.dog

    7 weeks ago

    Sorry, but I am a bit confused with the picture with the wires going into the RPI.
    I get that there are extra wires for the fan. But you seem to also be connecting a wire to extra pins (or a pin) which isn't a usual GPIO one. Looking at the youtube preview it is the grey wire on the right.

    0
    chuck-finley
    chuck-finley

    Reply 7 weeks ago

    Thank you for your question! The grey wire should be connected to Ground.

    0
    fuzzywuzzy.dog
    fuzzywuzzy.dog

    Reply 7 weeks ago

    So, the 6 wires are using the 6 last pins of the GPIO...
    Or: The last 3 pins but both "sides" of them. 3 x 2 = 6.

    It is just I am sure I saw one of them going to one of the other pins which don't have pins in them normally, and wanted to be sure.

    0
    jcrawford1153
    jcrawford1153

    2 months ago

    I would really like to do this with only led light since it will all be inside an old Nintendo and the only visible light is the power light. What I would like to do is have the one led on while the raspberry pi is turned on, then when you press the reset or shutdown button have that same led blink. Basically the same setup you have except all using one single led. I think I can just modify the code to make this work, but I figured I would ask just to be certain.

    0
    chuck-finley
    chuck-finley

    Reply 2 months ago

    Hello,

    please excuse the delayed reply. If I got you right it would be no problem, just don't define 3 colors and replace yellow and red in the reboot and shutdown sequences with your LED color of choice.

    The modifications would like this:

    # Define GPIO

    powerLED = 19
    GPIO.setup(powerLED, GPIO.OUT)
    GPIO.output(powerLED, 1)

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

    # Define Commands

    def reboot():
    GPIO.output(powerLED, 0)
    time.sleep(0.1)
    GPIO.output(powerLED, 1)
    time.sleep(0.1)
    GPIO.output(powerLED, 0)
    time.sleep(0.1)
    GPIO.output(powerLED, 1)
    time.sleep(0.1)
    GPIO.output(powerLED, 0)
    GPIO.cleanup()
    os.system("sudo reboot")

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

    For a code this simple it's probably overkill, but you could define the blinking routine as a function of it's own since you don't have different colors. You could even do a loop instead of manually setting the 0 and 1 state, the sky is the limit for overcomplicating this code ;)

    Best regards!

    0
    Sarges6
    Sarges6

    2 years ago

    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.

    0
    chuck-finley
    chuck-finley

    Reply 2 years ago

    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.

    0
    Alex in NZ
    Alex in NZ

    2 years ago

    Really neat. Thank you for that :-)

    0
    tomatoskins
    tomatoskins

    2 years ago

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