Make a “Safe Shutdown” Button for Raspberry Pi

Shutdown Button
www.pdf24.org    Send article as PDF   

Pulling the power to your Raspberry Pi can cause image corruptions & other issues that can damage your Pi! In this article, we will create a small push button that will work as a ‘Safe-Shutdown’ option. We’ll connect it to our Raspberry Pi using the GPIO pins and some jumper wires, and with a few lines of code, we will have our own power switch!

First, we are going to look at a simple way to wire a button to the Pi GPIO connector. We will then write a python script that will shut down the Pi safely. The final step will be to setup the Pi so that the button will work all the time. Let’s Get started!

What you will need:

  • Raspberry Pi B+ (Any Pi will work, Just make sure you check the GPIO pins)
  • Tactile Switches
  • Female/Male Extension Jumper Wire Packs
  • Solderless Breadboards

THE CIRCUIT

To do this we will wire the button to GPIO Pin 26(Yellow wire) and to ground (Green wire). This configuration is called an Active Low button, this means that when the button is pushed it will have no voltage or be logic low. The Pi has pull-up resistors on its GPIO pins so wiring active low mean we do not need to add an external resistor.

Raspberry Pi I/0 Pinout

Make sure you are extremely careful with the wiring here as shorting the wrong pins together can kill your Pi. Which is why I now have a Pi-shaped coaster.

Raspi Safe Shutdown Button Circuit

THE SCRIPT

We are going to use three external libraries:

  • The button module from the gpiozero library, this library gives easy access to programming the GPIO on the Pi.
  • The time library, so we can wait in human time.
  • The os library gives us the ability to shut down the Pi (You can also use this library to run any terminal commands!).

We first create a button object that lets us interact with the button. We will give this function the input of GPIO number we attached the button to 26 in our case. By default, this function assumes active low buttons so we do not need to change anything else.

We want this program to be running all the time so that when we press the button after a week-long retro gaming session it will still work. To do this we create while True loop that will keep running forever.

Inside the loop, we have two if statements the first will detect a button press then trigger the code to wait for a second then check and see if the button is still held down.

The last line tells the loop to wait 1 second before checking the button again, we are not in a hurry, so we can wait for presses. Make sure you include this wait without it python will run this script as fast as it can and that will put unnecessary load on the CPU.

You can now run this program and test it because we are using the os command we need to run this script as sudo as well.

Make sure you have saved all your work first!

Raspi Safe Shutdown Button Circuit Wiring

MAKING IT WORK

So that our button will work we need to tell the Pi to run the script on start-up. To do this we modify the rc.local file, In essence, rc.local is a to-do list the Pi follows as it starts up. In order to add more items to this list, we need to edit it. rc.local is protected by the operating system so we need to open, or do, it as a Super User (sudo). I am going to use leafpad to edit rc.local because I am not hacker enough to use nano.

Into rc.local we are going to add a command to run our shutdown script we will add this on the line before exit 0 as shown.

It is very important to remember the ‘&’ at the end this tells the Pi to run this program in the background. If you do not do this it will wait for the infinite loop to finish before moving to the next item on its to-do list. Save the file and exit if you are using leafpad you can use the menus. This last step can also be done with crontab a built-in scheduler for the Pi(and all Linux distributions! ).

Previous Post
Tor + Onion + Raspberry Pi

Make a Raspberry Pi into a Anonymizing 'TOR' Proxy!

TOR: The Onion Router is software that enables you to use the internet anonymously. By setting up TOR on a Raspberry Pi you can create a network router that scrambles ... Read more

Next Post

Run a Program On Your Raspberry Pi At Startup

The method that I usually use to run a program on your Raspberry Pi at startup is to use the file rc.local. In order to have a command or program run when the ... Read more

Short URL: http://tinyurl.com/ycd9547p

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.



Do NOT follow this link or you will be banned from the site!