Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This script will wait for a button to be pressed and then shutdown
- # the Raspberry Pi.
- # The button is to be connected on pin 5 (GPIO 03) and any ground
- # GPIO03 is used because it can also wake from halt (hardware function)
- # http://kampis-elektroecke.de/?page_id=3740
- # http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio
- # https://pypi.python.org/pypi/RPi.GPIO
- import RPi.GPIO as GPIO
- import time
- import os
- # we will use the pin numbering of the SoC, so our pin numbers in the code are
- # the same as the pin numbers on the gpio headers
- GPIO.setmode(GPIO.BCM)
- # Pin 05 (gpio03) will be input and will have his pull up resistor activated
- # so we only need to connect a button to ground
- GPIO.setup(03, GPIO.IN, pull_up_down = GPIO.PUD_UP)
- # Pin 17 is our power pin for the LED light. When on, the circuit completes.
- GPIO.setup(17, GPIO.OUT)
- # Flash the LED as the system starts up, then leave it on.
- GPIO.output(17,1)
- time.sleep(.25)
- GPIO.output(17,0)
- time.sleep(.10)
- GPIO.output(17,1)
- time.sleep(.25)
- GPIO.output(17,0)
- time.sleep(.10)
- GPIO.output(17,1)
- time.sleep(.25)
- GPIO.output(17,0)
- time.sleep(.10)
- GPIO.output(17,1)
- # ISR: if our button is pressed, we will have a falling edge on pin 5
- # this will trigger this interrupt
- :
- def Int_shutdown(channel):
- # flash LED on pin 17
- GPIO.output(17,0)
- time.sleep(.15)
- GPIO.output(17,1)
- time.sleep(.10)
- GPIO.output(17,0)
- GPIO.output(17,1)
- time.sleep(.10)
- time.sleep(.10)
- GPIO.output(17,0)
- GPIO.cleanup()
- # shutdown our Raspberry Pi
- os.system("sudo shutdown -h now")
- # Now we are programming pin 5 as an interrupt input
- # it will react on a falling edge and call our interrupt routine "Int_shutdown"
- GPIO.add_event_detect(03, GPIO.FALLING, callback = Int_shutdown, bouncetime = 2000)
- # do nothing while waiting for button to be pressed
- while 1:
- time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement