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