Intro: Simple Shutdown Raspberry Pi Button by Using GPIO
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.
Step 1: Write the Code
All the secret is in the code. Go to home folder.
Create a new file with:
sudo nano shutdown.py
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
input_state = GPIO.input(buttonPin)
if (not input_state):
os.system('sudo shutdown -h now')
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/shutdown.py &
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.