2

I am using a Pi 4 with Raspian, writing a program in Python (3.9.2) to do (amongst other things) shutdown of the Pi via a physical button. Based on this link https://linuxhint.com/set-up-shutdown-button-raspberry-pi-python/ I am using the code below:

import wiringpi as wp

. . .

def is_shutdown_button_pressed(self):
        if wp.digitalRead(self.POWEROFF) == wp.HIGH:
            return True
        else:
            return False

POWEROFF being GPIO 5 (pin 29) or GPIO 25 (pin 22) (have tested both) and in both cases the IF is always true (so returns true) regardless of the switch position (ON or OFF).

enter image description here

The image above is what I am doing, except in the image is GPIO 26. So the question is wether the sketch works at the mentioned GPIOs or it has to be a specif GPIO for powering off the Pi?

EDIT: I made it work. Turns out I was grounding the IO pin when the button is pressed, but what was necessary was to offer 3.3V to it instead. Both wiringpi and gpiozero work for it. I end up using pin 22 (GPIO 25).

1 Answers1

2

You can use any (most anyway) GPIO if you're only interested in performing a shutdown for the RPi. However, if you want the push button to toggle the RPi between the run and shutdown states you must use GPIO 3.

schematic

simulate this circuit – Schematic created using CircuitLab

No code is required - other than a one-line invocation of a device tree overlay in /boot/config.txt. There's another answer here that has additional details, and some background information on this neat little scheme.

You should also know that the documentation for this (and all other) device tree overlays and parameters is on your local system in /boot/overlays/README. However, this file may be best-accessed via the RPi GitHub page for README as it's always up-to-date.


N.B. When switching the RPi "OFF" in this way, you should know that the RPi continues to draw power. This is why a button press can turn the RPi back "ON" again.

Seamus
  • 23,558
  • 5
  • 42
  • 83