Simple Shutdown Raspberry Pi Button by Using GPIO

Introduction: Simple Shutdown Raspberry Pi Button by Using GPIO

About: I’m degree in Psychology in 2001, with a thesis in Artificial Intelligence. I’m working on Smart Object, IoT and Interactive Design. I’m developing autonomously my projects. I make autonomously programs in A...

When I use my Raspberry Pi without screen and keyboard, I have a lan or something else. But if I can't see my Raspberry Pi in lan, or if I can't see my Raspberry Pi on my screen, or if my keyboards not work because the batteries are died, I use my secret shutdown switch.

Do you want a Raspberry Pi 3? or a switch

Step 1: Write the Code

All the secret is in the code. Go to home folder.

cd /~

Create a new file with:

sudo nano

now write inside this:

import RPi.GPIO as GPIO import time import os

buttonPin = 21


GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

last_state = True

input_state = True

while True:

input_state = GPIO.input(buttonPin)

if (not input_state):


os.system('sudo shutdown -h now')

time.sleep(0.05) GPIO.cleanup()

Step 2: First the Code

When you start your Raspberry Pi, you can run the previous code by modifying the rc.local file.
Write this in terminal:

sudo nano /etc/rc.local

Then modify the file by adding this line before "exit 0"

python /home/pi/ &

Step 3: Now Try to Shutdown by Wire

Now it's the time of truth.
Reboot you Raspberry Pi, and try to connect the last two GPIO pin like in figure. The last two pin in Raspberry Pi Zero are Ground and GPIO 21.

Ta taaa your Raspberry Pi shutdown without screen, keyboard or others kind of solutions. All you need is a cable...or a switch. See the image. My Raspberry Pi zero shutdown only when turn it upside down. I have used a tilt switch.

Do you want a tilt switch ?

Be the First to Share


    • Puzzles Speed Challenge

      Puzzles Speed Challenge
    • "Can't Touch This" Family Contest

      "Can't Touch This" Family Contest
    • CNC Contest 2020

      CNC Contest 2020