To shutdown or reboot Raspberry Pi (4 in my case) by a momentary power switch, so you don't need to unplug the cord which might damage your SD card
In any case you shouldn't unplug the cord to shutdown Pi, it could lead to data corruption and SD card malfunction.
So add a power button to safely and easily shutdown, reboot, or wake up your Raspberry Pi.
Note: When we "shutdown" the Pi, Pi gets into halt state which consumes little power, and we can only disconnect the power supply in this state.
To wake Pi up from "halt", easily short GPIO3(PIN5) and GND(PIN6). However, in my project I'm using I2C which required GPIO3, so I won't elaborate the wake up function in this repo.
- Hardware Wiring
- Soft reboot by short press the power switch
- Soft shutdown by long press the power switch
- register the script to run on boot
- Wiring
Pick a GPIO PIN (GPIO 13 for example)
Connection: GPIO13 — power switch — GND
- Clone the repository:
git clone https://github.com/bblabNTU/pi_soft_reboot.git
- If you install ubuntu on rpi:
sudo apt-get install python3-rpi.gpio
- Modify listen-for-shutdown.service on Line:7 :
ExecStart=/usr/bin/python3 <.py location>/listen-for-shutdown.py
sudo mv listen-for-shutdown.service /lib/systemd/system/listen-for-shutdown.service
sudo chmod 644 /lib/systemd/system/listen-for-shutdown.service
sudo systemctl daemon-reload
sudo systemctl enable listen-for-shutdown.service
sudo reboot