Introduction: Raspberry Pi Zero Switch Off by Button
I've a small and sweet Raspberry Pi zero . When I switch on my Raspy zero without a monitor or keyboard or LAN, I don't want switch off the pi directly by putting off the socket by the line. In fact this action can compromise the Raspy. You can be breaking the kernel, and the system can crash after that.
I've written a code that put the last pin in standby for a connection between the ground. When I connect the last pin with the ground, the code starts. The code is a simple sudo shutdown -h now
Step 1: Create the Code
Go to GitHub and read the code (https://github.com/masteruan/Raspbery_switch).
import RPi.GPIO as GPIO
import time import os
buttonPin = 21
GPIO.setmode(GPIO.BCM)
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): print("Shutdown")
os.system('sudo shutdown -h now')
time.sleep(0.05)
GPIO.cleanup()
Step 2: Connect the Pins
Now is the moment to connect the pin. After the starting of Raspberry Pi, you can connect the last pin to ground.
You can read on screen, the command: sudo shudown-h now and the Raspberry Pi switch off.
Materials
Step 3: Modify the Rc.local
Now if you want start automatically the script do you modify the rc.local file. This file start when you switch on the Raspberry Pi
#!/bin/sh -e
# # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing.
# Print the IP address _IP=$(hostname -I) || true if [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP" fi
python /home/pi/Desktop/shutdown.py &
exit 0
Step 4: Mounting a Switch Or...
Now you can mount a switch like in photo. I've mounted a tilt switch. When I turn upside down the Raspberry Pi, this switch off.
7 Comments
3 years ago on Step 4
Everybody
please use the RPI FIRMWARE overlay gpio-shutdown:
https://github.com/raspberrypi/firmware/blob/maste...
It uses a pullup on a GPIO to shutdown & start-up again. Much better.
Name: gpio-shutdown
Info: Initiates a shutdown when GPIO pin changes. The given GPIO pin
is configured as an input key that generates KEY_POWER events.
This event is handled by systemd-logind by initiating a
shutdown. [...]
This overlay only handles shutdown. After shutdown, the system
can be powered up again by driving GPIO3 low. The default
configuration uses GPIO3 with a pullup, so if you connect a
button between GPIO3 and GND (pin 5 and 6 on the 40-pin header),
you get a shutdown and power-up button.
Load: dtoverlay=gpio-shutdown,=
Params: gpio_pin GPIO pin to trigger on (default 3)
active_low When this is 1 (active low), a falling
edge generates a key down event and a
rising edge generates a key up event.
When this is 0 (active high), this is
reversed. The default is 1 (active low).
gpio_pull Desired pull-up/down state (off, down, up)
Default is "up".
Note that the default pin (GPIO3) has an
external pullup.
debounce Specify the debounce interval in milliseconds
(default 100)
Reply 2 years ago
That looks better, but this part of the instructions doesn't work for raspberry OS where /etc/inittab no longer exists:
Then add following lines to /etc/inittab file: # Action on special keypress (Key Power) kb::kbrequest:/sbin/shutdown -t1 -a -h -P now
Reply 2 years ago
Ok. Thanks for the tip!
Question 5 years ago
Where to get a switch like this and if I want headers to plug into, where to get them?
Answer 5 years ago
Hi,
see the link in the description.
6 years ago
Python has some problems with Indentation.
This code works for me:
import RPi.GPIO as GPIO
import time
import os
buttonPin = 21
GPIO.setmode(GPIO.BCM)
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):
print("Shutdown")
os.system('sudo shutdown -h now')
time.sleep(0.05)
GPIO.cleanup()
Reply 6 years ago
Yes you are right! Follow this: https://github.com/masteruan/Raspbery_switch