The s_shut project describes a very simple system in order to shut down Raspberry Pi safely. It consists of a Python 3.x script s_shut.py, a simple schematic and this description. This project is tested in Raspberry models 3, 3B, 4B, zero-wh under raspbian V10 and above. The python script uses the awesome libgpio library,
Running Linux computer should not be powered off without a controlled shutdown by the operating system, because this can result in damaging the file system on the SD_Card. Raspberry computers do not provide a reset or power down switch.
Controlled power down is especially critical important in so called headless systems with no monitor and keyboard.
The presented solution allows initiating shutdown or a restart with a switch or an external signal.
The Project consists of a single Python 3.x script that may be started from command line or may be launched as auto-start process.
After Linux boot, a selectable port (default BCM21) is driven high and LED goes ON
A switch on a selectable port (default BCM20) triggers events used in the script s_shut.py.
Linux command shutdown -P now
will be executed by the script s_shut.py . As soon as critical storage operations are completed, LED goes OFF.
System shuts down and restarts.
Linux command shutdown -r now
will be executed by the script s_shut.py.
(Power off needs external hardware)
This is only achievable by external power off and on again. (A much more convenient solution is using the "UPS-2" project, that will follow here in the first Quarter 2021)
This summary is intended for experienced users.
- Prepare hardware, see [Fig. 1](#Fig. 1)
- Download script s_shut.py
wget https://raw.githubusercontent.com/ECOM-Klaus/Raspberry-Shutdown/master/s_shut.py
- Install libgpiod library
sudo apt install python3-libgpiod
- Activate pullup resistor in file /boot/config.txt
#set GPIO20 as input with pullup high
gpio=20=ip,pu
- Autostart s_shut.py script in file /etc/rc.local
python3 /home/pi/<*your project location*>/s_shut.py &
-
Download 'Raspberry Imager' and follow instructions for creating SD card:
https://www.raspberrypi.org/software/
This project works with all OS versions. -
Edit file config.txt:
The used gpiod library has no pullup feature. (will possibly come with Linux kernel >=V5.1). We have to set pullup in file config.txt. This SD card file is visible on the PC or MAC directly in the root, or on Raspberry in the /boot directory (use sudo ).#set GPIO20 as input with pullup high
gpio=20=ip,pu
-
Install required library on Raspberry:
sudo apt install python3-libgpiod
-
Download shutdown script or clone project Change to your project directory and download the Python3 project script
wget https://raw.githubusercontent.com/ECOM-Klaus/Raspberry-Shutdown/master/s_shut.py
OR execute from your project directory
git clone https://github.com/ECOM-Klaus/Raspberry-Shutdown.git
-
Basic test without hardware Change to your project directory and execute
~ $ cd <your project directory>
Execute script help
~ /<your project directory> $ python3 s_shut.py -h
--> expected reponse:
usage: s_shut.py [-h] [-l] [-s] [-p]
optional arguments:
-h, --help show this help message and exit
-l , --ledPort LED output bcm port default = 21
-s , --switchPort Switch control input bcm port default = 20
-p , --powerPort Optional bcm port for external power time
r
Required material: (example types)
- Metalfilm resistor 330R, 1/4W
- [Metalfilm resistor 4k7, 1/4W, optional, see [Software Installation|](#3.1.Software Installation|outline)]
- Tactile Switch Tyco FSM8J
- Green LED 3mm Kingbright L-7104SGC
Fig. 1: Wiring Diagram with default ports.
Please test first with default ports. You may change it later by command line parameters. We test first by command line, later by autostart.
- Raspi power on
- Wait until boot finished
- Start a terminal (local or with an external ssh terminal)
- Change to your project directory
- Execute
python3 s_shut.py
--> LED should switch on - Press key for > 3 seconds (LED blinks once per second), then release switch
--> Raspberry should shut down and Led goes off after SD card access is finished.
Before implementing autostart, perform the command line tests in the previous chapter.
Normally you would start this script automatically during power-up. There are different methods to achieve this, but start should be independent of the start of a graphical desktop. So it is usable also on minimum systems suchlike Raspberry zero. A proved method is starting by the script rc.local:
- Edit rc.local:
cd /etc
sudo nano rc.local
- Add at the bottom of file:
--> normally, the file is empty or there are some comment lines. Add at the bottom of file
python3 /home/pi/<*your project location*>/s_shut.py &
exit 0
- Save this file and exit nano editor:
^o ^x - Make the script executable:
sudo chmod +x rc.local
- Now check if it works.
sudo shutdown -r now
- --> Computer shuts down and restarts. LED should turn on after booting through first runlevels (wait seconds until minutes depending on model and configuration).
- Test switch functions: see chapter Functionality
Standard ports are BCM20 for LED and BCM21 for switch. External power off signal is disabled by default.
Add a line in file /etc/config.txt:
gpio=<port>=ip,pu
Use parameter -s:
python3 s_shut.py -s<led port>
Use parameter -l:
python3 s_shut.py -l<switch port>**
This needs an external power supply that switches off after negative edge of suppy port.
Use parameter -p:
python3 s_shut.py -p<supply port>
- LED does not work when executing s_shut.py
--> if no error message, check hardware connections
--> if using other port than BCM21 call script with parameterpython3 s_shut -l<port>
--> if error message concerning 'gpiod', re-installsudo apt install python3-libgpiod
- Switch is not working
--> check hardware connection
--> is pullup for switch port set in file /boot/config.txt?
--> if using other port than BCM20 call script with parameterpython3 s_shut -s<port>
Copyright (C) 2021 Klaus Mezger under MIT license
< written with https://typora.io/ >