Add power button to raspberry pi

05/09/2022
Raspberry Pi 4
As a hardware enthusiast, embedded system engineer, and technical nerd, Raspberry Pi is definitely something to play with, but after using it for a long time, you will always find that it has some imperfections, such as: no power button!
There are two commonly used shutdown methods (not to mention the external screen and keyboard):
1. Log in remotely and then type a command (this is the most suitable shutdown method, but it is too troublesome, if there is no computer around, it cannot be shut down)
2. Unplug the power (simple and rude, but it can easily lead to file damage)
So in order to use the Raspberry Pi happily, it is best to add a power button like a computer to it. Baidu found a lot of domestic (yes, I am targeting China, and there are only a few articles going back and forth, but I still think that Repeatedly copy each other) There is only one way to add the power button, that is to write a python script, or other script, or compile a program, anyway, the principle is the same, let the script (or program) start up, the program will always Detects the GPIO set to the power key and shuts down if triggered. Although this method can solve the problem, it is really low! ! ! And it can only be turned off, not turned on.
Here I will share with you a very elegant method that can be turned off and turned on:
open and read /boot/overlays/README carefully
. This is the end of today's sharing, thank you.
In fact, /boot/overlays/README has already said it in great detail. In order to benefit the general public, I will sort it out.
Many people have a headache when they see English, and this file has thousands of lines, and few people will read it carefully. Here I will only extract the content related to the power supply (the content of the system files of different versions is slightly different):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
twenty two
twenty three
twenty four
25
26
27
28
29
30
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. Systemd versions older than 225 need an udev rule
        enable listening to the input device:
 
                ACTION!="REMOVE", SUBSYSTEM=="input", KERNEL=="event*", \
                        SUBSYSTEMS=="platform", DRIVERS=="gpio-keys", \
                        ATTRS{keys}=="116", TAG+="power-switch"
 
        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.
It has been made very clear here, as long as the relevant configuration is added to /boot/config.txt, the shutdown can be realized.
For example like this:
1
2
# Use GPIO3 (the GPIO here refers to the 3BCM GPIO number, that is, SCL1) as the power button, the falling edge is pressed, the rising edge is bounced, and the internal pull-up
dtoverlay=gpio-shutdown
Save and restart, then you just need to connect a button between GPIO3 and GND to shut down, isn't it easy?
Start to focus! ! Exams are to be taken! ! remember! !
There is no need to add any configuration. After the normal shutdown, the Raspberry Pi can be turned on by pulling GPIO3 to a low level without cutting off the power supply! That's right, that's the power-on button! If you don't need GPIO3 as the power-on button, please read the related content of gpio-poweroff in /boot/overlays/README by yourself
The configuration mentioned above uses GPIO3 by default, that is, SCL1 as the power button, which occupies the I2C interface. Although other GPIO software can be used to simulate I2C, it is obviously very inconvenient. It is better to change it to other GPIO as follows. The GPIO port can only be used as the power-off key, and the power-on key is still GPIO3. At present, I have not found a way to customize other power-on keys.
1
2
# Use gpio_pin=17, that is, GPIO_0 (physical pin PIN_11) as the shutdown button
dtoverlay=gpio-shutdown,gpio_pin=17,active_low=1,gpio_pull=up
GPIO3 conflicts with SCL1. The specific performance is that when I2C is enabled, GPIO3 is used as SCL1, so only GPIO3 can be used to power on, but not power off. So if you want to use only one button as the on/off button, you must give up the hardware I2C and use other pins to simulate the I2C; if you must have the on/off button and use the hardware I2C at the same time, then use GPIO3 as the power on button, and in addition Configure a key as the power-off key.
I have only verified the above content on Raspberry Pi 3B+ (system version: Raspbian Buster with desktop, Image with desktop based on Debian Buster, Version:July 2019, Release date:2019-07-10, Kernel version:4.19), Other versions are not guaranteed to be available
There are many very useful configurations in /boot/overlays/README, and interested students can study by themselves.
Finally, I hope you all act quickly! ! Abandon those low-explosive switch button solutions!