Raspberry Pi Smart Power Button: A DIY Solution for Raspberry Pi Up to Model 4
on
The success and popularity of Raspberry Pi boards is certainly well-deserved; but over time, many users have noticed and complained about the lack of a power button. Finally, the desired button has been added to the new Raspberry Pi 5 model, but owners of earlier models may find this project useful. Let's build a Raspberry Pi Smart Power Button.

Simple Wiring
Accessories that add power button functionality to the Raspberry Pi are nothing new. There are both commercial and DIY offerings, from the simple cable with a switch to sophisticated HATs (Hardware Attached on Top). However, the solution that I’m proposing has the advantage of a rather simple wiring diagram and does not require any programming or installation of scripts or automatic startup services.
A few components and the addition of three lines of text to the config.txt file make it possible to power on the Raspberry Pi board at the touch of a button, shut down the operating system correctly (even in headless systems), and automatically disconnect the board’s power supply without having to unplug the USB connector, thus reducing the current draw to zero.
Configuring the Pins
The circuit, illustrated in Figure 1, must be installed between a standard power supply and the Raspberry Pi board, and also connected to two available generic GPIO pins on the 40-pin header. Before connecting the circuit, it is necessary to define the behavior of these two pins using a feature introduced in the most recent versions of Raspberry Pi OS.

This feature is known as Device Tree Overlays, which allows instructions for hardware operation to be inserted in text form into the config.txt file. This file (which is loaded at each boot of the operating system) is located in the bootfs partition of the operating system’s micro SD card. It is easily accessible even from Windows and can be modified using any text editor. The following three instructions must be added at the end of the file:
gpio=23=op,dh
This sets GPIO23 to be an output set to 1 (HIGH).
dtoverlay=gpio-poweroff,gpiopin=23,active_low,active_delay_ms=10000
This will set GPIO23 low when safe shutdown is complete.
dtoverlay=gpio-shutdown,gpio_pin=24,active_low=1,gpio_pull=up,debounce=3000
That starts a safe shutdown when GPIO24 is held low for more than 3 s. GPIO24 is set as an input with pull-up. An example of a modified config.txt file, including comments, is shown in Figure 2.

The Circuit
Now the operation of the circuit becomes clearer. By pressing and holding the SW1 button, the MOSFET Q1 (P-channel) is turned on. This powers the Raspberry Pi board and boots the operating system. Within about 1 s, GPIO23 goes high, the blue LED lights up, the MOSFET Q2 (N-channel) goes on and keeps Q1 on and the button can be released.
If you shut down Raspberry Pi from the Start menu on the OS desktop, at the end of the process (Safe Shutdown) GPIO23 will go low, the blue LED, Q1 and Q2 will turn off and the power to the board will be cut. The same effect is achieved by pressing the button for more than 3 s. After this time, GPIO24 activates the Safe Shutdown procedure without the user having to access the operating system, which is very useful in headless systems.

To proceed with the schematic description: C1 and R1 prevent Q1 from turning on when the power supply is connected. D1 and D2 protect GPIO24 from the 5 V voltage present at Q1’s gate (via R1) when Q2 is turned off. R4 is the pulldown resistor of Q2’s gate, and R2 and R3 protect the GPIOs from overcurrent. The MOSFET Q1 was chosen for its low on-resistance value (about 16 mΩ @VGS = -4.5 V) in order to minimize voltage drop and power loss.
The completed prototype is visible in Figure 3. It was realized on a small piece of solderable breadboard that can fit close to your Raspberry Pi board.
Editor's Note: This article (240546-01) is scheduled to appear in the Elektor Circuit Special 2025.
Discussion (0 comments)