Raspberry Pi GPIO power button
Sep 8, 2017
1 minute read


Button Pin RPi GPIO
1 GPIO 3 (SCL)

Power On

On my Raspberry Pi 2 Model B Rev 1.1, the system will power on from a halted state when shorting the SCL (GPIO 3) to Ground.


When the system is on, the below script will monitor SCL (GPIO 3) for a button press. When a button press is detected, the script will shutdown the Raspberry Pi safely.

Create /home/pi/scripts/shutdown.py

# Raspberry Pi button shutdown

import RPi.GPIO as GPIO
import subprocess

# Define GPIO pin number
SCL = 3

# GPIO pin setup
GPIO.setup(SCL, GPIO.IN, pull_up_down = GPIO.PUD_UP)

# Wait for button press
GPIO.wait_for_edge(SCL, GPIO.FALLING)

# Shutdown
subprocess.call(['sudo', 'shutdown', '-h', 'now'])

Edit /etc/rc.local adding the below line above exit 0

sudo python /home/pi/scripts/shutdown.py &