I added a button to the Raspberry Pi to shut it down with a long press.
Shutdown the Raspberry Pi
Since the Raspberry Pi is a Linux OS, it cannot be turned off suddenly.
It is necessary to connect via ssh or connect a keyboard and monitor and use the shutdown command to shutdown the OS.
However, the Raspberry Pi installed at the manufacturing site for IoT is not always able to shut down properly.
I added a button for shutdown to my Raspberry Pi, and I’ll write an article about it as a reminder.
- To avoid shutting down when a button is touched accidentally, shutdown is now done only when the button is “long pressed”.
- Selected a button with LED light.
- The LED light on the button turns on when the program is running and turns off when the program is shutdown by pressing and holding the button.
I bought an OMRON pushbutton switch (model number = A165L-JRM-5D-1).
The meaning of the model number is as follows.
The type of the button.
IP standard grade of water resistance or protection.
Whether it is illuminated or not
I wanted a button with an LED light, so I chose the illuminated type.
Button behavior when pressed
This shows the terminal arrangement.
In terms of circuitry, I would have preferred to use an a-contact, but since the button I had available was a c-contact, I chose a type of button with only one contact.
The terminal arrangement for 1c is as follows (from the OMRON manual)
I don’t use NC connector.
It is used as a button with real a-contact.
The following environment was tested.
Raspberry Pi 4B
Raspberry Pi OS 32Bit
I saved the following program in “.local/shutdown_test/”
# -*- coding: utf-8 -*- """ Created on Sat Jun 5 08:24:26 2021 ・Press and hold button to shut down ・LED on during program operation, off during shutdown @author: Souichirou Kikuchi """ import os import RPi.GPIO as GPIO from time import sleep PIN_BUTTON_LED = 4 # LED on the button PIN_BUTTON_SD = 22 # shutdown button def shut_down(channel): # Long Press to shutdown if channel == PIN_BUTTON_SD: sw = 0 for _ in range(15): sleep(0.2) # 0.2 seconds x 15 times = 3 seconds sw = GPIO.input(channel) if sw == 0: # If there is even one LOW out of 15 times, it will not shutdown. break if sw == 1: GPIO.output(PIN_BUTTON_LED, GPIO.LOW) # Button LED off os.system('sudo shutdown -h now') # shutdown GPIO.setmode(GPIO.BCM) GPIO.setup(PIN_BUTTON_LED, GPIO.OUT) GPIO.setup(PIN_BUTTON_SD, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.add_event_detect(PIN_BUTTON_SD, GPIO.RISING, callback=shut_down, bouncetime=200) if __name__ == "__main__": try: print('ShutDown Test Program Start') # Program start GPIO.output(PIN_BUTTON_LED, GPIO.HIGH) # LED lights on the button while True: sleep(0.001) except: pass finally: GPIO.cleanup() print('ShutDown Test Program End') # Program ended
I’ve included some comments in the program, but I’ll add a few more.
GPIO.setup(PIN_BUTTON_SD, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
is the GPIO22 connected from the button NO, but I have specified a pull_down resistor.
GPIO.add_event_detect(PIN_BUTTON_SD, GPIO.RISING, callback=shut_down, bouncetime=200)
In the same way, when the voltage of GPIO22 rises from LOW to HIGH (RISING), the shut_down function is called.
Since the voltage does not rise in a straight line, the interval of 200 milliseconds between the next call to the shut_down function is left with bouncetime=200 to prevent the function from being called repeatedly.
Run the program with the following command.
cd ~/.local/shutdown_test python shutdown_button.py
The LED lights up, and I was able to confirm that long press it (for about 3 seconds) will shut it down.
At the end.
This is the end of the article on shutdown by button.
The reason why Raspberry Pi (Linux OS) can’t be shutdown suddenly is that various files used by the OS are written to the SD card while the OS is running.
If you suddenly turn off the power while writing files, those files may be corrupted, which may affect the next time the OS boots and prevent it from booting.
As a countermeasure for this, I’ve written an article on how to “add a shutdown button”.
- Set the file system to Read Only(SD Card)
- Relay circuit to give grace until OS shutdown
I’ve heard that there is a way to do the above, and I’d like to try it soon.
I hope this article will be useful to someone somewhere.