Things used in this project

Code

shutdown_pi.pyPython
Thanks to Inderpreet Singh for the original code! Be careful about the indents if you're copying & pasting.
#!/bin/python 
# Simple script for shutting down the raspberry Pi at the press of a button. 
# by Inderpreet Singh https://www.element14.com/community/docs/DOC-78055/l/adding-a-shutdown-button-to-the-raspberry-pi-b
 
import RPi.GPIO as GPIO  
import time  
import os  
 
# Use the Broadcom SOC Pin numbers 
# Setup the Pin with Internal pullups enabled and PIN in reading mode. 
GPIO.setmode(GPIO.BCM)  
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)  
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
 
# Our function on what to do when the button is pressed 
def Shutdown(channel):  
   os.system("sudo shutdown -h now")  
def Restart(channel):
   os.system("sudo shutdown -r now")
 
# Add our function to execute when the button pressed event happens 
GPIO.add_event_detect(18, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)  
GPIO.add_event_detect(23, GPIO.FALLING, callback = Restart, bouncetime = 2000) 
 
# Now wait! 
while 1:  
   time.sleep(1) 

Credits

Photo on 7 5 17 at 5 06 pm 7 05huowsyxv
Alex Glow

The Hackster team's resident Hardware Nerd. I like robots, music, EEG, wearables, and languages. FIRST Robotics kid.

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

LED Roulette Game
Easy
  • 10
  • 2

Full instructions

A fun pocket-sized travel game for any age. Try your luck and make the light stop on the green LED.

New MATRIX Creator Weather App on Eclipse Day
Easy
  • 13
  • 2

Full instructions

In celebration of Eclipse Day we have made this app to tell you what the weather is outside so you know if you can see the eclipse.

Mask with Blinking Eye
Easy
  • 74
  • 3

Protip

Makes a great mask more eye-catching when one eye is blinking. Has an on-off switch, is powered by a cell , and driven by an ATtiny85.

Chicken Coop Livestream
Easy
  • 2,881
  • 11

Full instructions

We have two chickens, and their coop is located at the back of the garden. I decided to build a Pi-based streaming device to check on them.

PHPoC - Detect Someone Taking Your Stuff  and send Gmail
Easy
  • 634
  • 14

When your stuff is being taken away by a thief, PHPoC makes an alert and sends a notification to you via Gmail.

Test Your Internet Speed Using a Raspberry Pi + Ubidots
Easy
  • 453
  • 6

Protip

Use your Raspberry Pi to measure any internet connection speed with Ubidots.

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaFree StoreBlog