Raspberry pi Gpio power on button

Hello, i was wondering if there is an easy way to connect a button to rpi gpio and program it so it can turn on and off my rpi?

Turning your Pi off, sure... that should be relatively easy. You'll detect the change and that will fire off a poweroff command

Turning it back on, on the other hand, might not be possible (unless you are using a Pi4, in which case it might work). When you run poweroff, it stops all processes and shuts down the system. So there won't be anything listening to the GPIO pins in order power it back on again.

If you're using a Pi4, it may be possible to turn it back on with the latest bootloader. More info here:

Hey, thanks for your reply, but can you please tell me how to set it up. I didnt find any tutorial for it. Thanks

He did, did you study the links provided?


Yeah I did but i just dont understand.

What is it that you don't understand?

1 Like

You could also buy a wireless wall socket with a 433mhz remote control. Plug in the power supply of the raspberry and you have a functional on / off switch as well.

A wall socket switch will cause an abrupt shutdown (rather than graceful with a GPIO based solution that can call the poweroff command). This is generally not a problem with OpenWrt since files are rarely written to disk (most activity, by design, happens in RAM as to avoid unnecessary flash memory wear). However, if the OP has any additional services such as logging, file sharing, etc., it is possible that there may be files open and a non-graceful shutdown could be problematic.

1 Like

I dont how how to set it up in the operating system. I dont see anythign about power off switch in https://openwrt.org/docs/techref/hardware/port.gpio

The hw part is on the RPi side, study their manual(s).

The power switch is just a triggered script.

You can use the OpenWrt documentation that I linked to setup a GPIO in the OS... and you can select which GPIO pin(s) you want to use based on the power button document I linked. (remember, only the Pi4 will be able to wake from off, based on the boot loader and the info from the website).

You'll have to figure out what the GPIO pin mappings are for your Pi, but they should all be there and is likely just the actual GPIO pin number per the documentation (for example, the case fan uses gpiopin=14 which is almost certainly GPIO 14 (physical pin 8 on the 40-pin connector header).

A simple test would be to connect an LED (with an in-line resistor) to a GPIO pin, set the GPIO to output, and then toggle it on and off to see if you're changing the state. Once you've identified that GPIO, you can make it an input, connect a switch and see if you can get it to respond (your 'script' can be anther one-liner that maybe sends a line to the logger: logger "GPIO button press detected"

With all of that, you should be able to simply replace the logger command with poweroff and be done.