The operation is explained in /boot/overlays/README

It is quite safe, I have it on all my Pi in case I need to shutdown a headless Pi (or one without keyboard attached), although my Pi are rarely shutdown, unless I am adding hardware.

I use dtoverlay=gpio-shutdown,gpio_pin=21 so I can shutdown by bridging pins 39/40

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 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. Please note that
        Raspberry Pi 1 Model B rev 1 uses GPIO1 instead of GPIO3.
Load:   dtoverlay=gpio-shutdown,<param>=<val>
Params: gpio_pin                GPIO pin to trigger on (default 3)
                                For Raspberry Pi 1 Model B rev 1 set this
                                explicitly to value 1, e.g.:

                                    dtoverlay=gpio-shutdown,gpio_pin=1

        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. Same applies for GPIO1
                                on Raspberry Pi 1 Model B rev 1.

        debounce                Specify the debounce interval in milliseconds
                                (default 100)
Answer from Milliways on Stack Exchange
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › community › general discussion
How to enable and disable GPIO pins - Raspberry Pi Forums
November 12, 2021 - import RPi.GPIO as GPIO from time import sleep in1 = 24 in2 = 23 ena = 25 enb = 18 temp1=1 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(in1,GPIO.OUT) GPIO.setup(in2,GPIO.OUT) GPIO.setup(ena,GPIO.OUT) GPIO.setup(enb,GPIO.OUT) GPIO.output(in1,GPIO.LOW) GPIO.output(in2,GPIO.LOW) #Motor A p=GPIO.PWM(ena,1000) p.start(25) #Motor B p=GPIO.PWM(enb,1000) p.start(18) print("\n") print("The default speed & direction of motor is LOW & Forward.....") print("r-run s-stop f-forward b-backward l-low m-medium h-high e-exit") print("\n") while(1): x=input() if x=='r': print("run") if(temp1==1): GP
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › software › raspberry pi os
command/code to turn off 3.3v gpio power - Raspberry Pi Forums
I want to turn off and turn on 3.3v gpio power from terminal? Is there any command or c code to do this? ... You can't turn it off. An exception for the Pi 4 which can but only when restarting or going into the low power shutdown state.
Discussions

Raspberry Pi 4 B GPIO boot and shutdown button(s) - Raspberry Pi Stack Exchange
Load: dtoverlay=gpio-shutdown,... 3) For Raspberry Pi 1 Model B rev 1 set this explicitly to value 1, e.g.: dtoverlay=gpio-shutdown,gpio_pin=1 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) ... More on raspberrypi.stackexchange.com
🌐 raspberrypi.stackexchange.com
September 18, 2020
raspberry pi - Disable GPIO by default and temporarily activate it via python script - Stack Overflow
I'm using a Raspberry Pi 2 with DietPi and plugged in an alarm module into the GPIO 14 (Pin 08). I would now like to disable this Pin by default and only allow it to turn on temporarily within my ... More on stackoverflow.com
🌐 stackoverflow.com
python - Raspberry pi GPIO pin not turning off - Stack Overflow
I bought RPi, flashed the OS. I also bought a 5V relay. I have connected the 2nd pin to VCC of the relay, 6th pin to GND of the relay, 40th pin to the 'IN' point of the relay. At present I have not More on stackoverflow.com
🌐 stackoverflow.com
pi 2 - How to turn off all gpio pin during powering up raspberry pi? - Raspberry Pi Stack Exchange
I am trying to control linear acutronix RC servo using raspberry with the help of adafruit servo hat. While powering up raspberry pi without sending any commands linear motor moved to 100 percentage More on raspberrypi.stackexchange.com
🌐 raspberrypi.stackexchange.com
March 31, 2017
🌐
Stack Overflow
stackoverflow.com › questions › 30045659 › how-to-disable-gpio-pins-on-the-raspberrypi
python - How to disable GPIO pins on the RaspberryPi? - Stack Overflow
There is a built-in function GPIO.cleanup() that clean up all the ports you've used. For the power and ground pins, they are not under software control. ... Sign up to request clarification or add additional context in comments. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Trying to find a book series about a doomed boy and his friends fighting off released monsters in middle school
Top answer
1 of 2
7

The operation is explained in /boot/overlays/README

It is quite safe, I have it on all my Pi in case I need to shutdown a headless Pi (or one without keyboard attached), although my Pi are rarely shutdown, unless I am adding hardware.

I use dtoverlay=gpio-shutdown,gpio_pin=21 so I can shutdown by bridging pins 39/40

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 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. Please note that
        Raspberry Pi 1 Model B rev 1 uses GPIO1 instead of GPIO3.
Load:   dtoverlay=gpio-shutdown,<param>=<val>
Params: gpio_pin                GPIO pin to trigger on (default 3)
                                For Raspberry Pi 1 Model B rev 1 set this
                                explicitly to value 1, e.g.:

                                    dtoverlay=gpio-shutdown,gpio_pin=1

        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. Same applies for GPIO1
                                on Raspberry Pi 1 Model B rev 1.

        debounce                Specify the debounce interval in milliseconds
                                (default 100)
2 of 2
7

I see two separate questions re use and configuration of "power buttons" to halt and boot an RPi 4:

Question 1:

Is the shutdown initiated with connecting the GPIO3 pin and the GND pin considered a safe shutdown?

Answer:

Yes - it is a safe shutdown. For details & confirmation, see REF 1 and REF 2.

Question 2:

I know that I can change the pin for shutdown, by changing dtoverlay=gpio-shutdown to dtoverlay=gpio-shutdown,gpio-pin=X, but is there a way to change the pin for boot up as well? Considering GPIO3 is normally used for I2C.

Answer:

Before going any further, I'd like to make sure we're clear on two points:

  • You can startup and shutdown your RPi with a single push-button switch connected between GPIO 3 and GND. You didn't specifically say that you wanted this 1-button arrangement, but it's far more convenient than using separate buttons for shutdown and startup. The only good reason I can think of to do it any other way is to reduce energy consumption while leaving power connected to the RPi.

  • Shutting down your RPi - whether done with a button press, or from the command line - does not remove power. Your RPi will continue to consume some power in shutdown unless and until you "pull the plug".

That said, let's review the options:

Option 1. Move I2C from GPIO 3 and GPIO 4

The latest version of the firmware provides numerous overlay options for "relocating" I2C1, and/or "adding" I2C buses in /boot/config.txt. Review the latest version of the README for details.

This is the easiest option, and you need not read any further unless you need to minimize power consumption while in shutdown.

Option 2.

The RPi 4 introduced a new bootloader that's stored in EEPROM (instead of a bootcode.bin file). It also provided new configuration properties - two of which are relevant to reducing power consumption while the RPi 4 is shutdown or halted; i.e. the system is not running, but is powered up:

WAKE_ON_GPIO

If 1 then 'sudo halt' will run in a lower power mode until either GPIO3 or GLOBAL_EN are shorted to ground.

Default: 1 (0 in original version of bootloader 2019-05-10) Version: All

POWER_OFF_ON_HALT

If 1 and WAKE_ON_GPIO=0 then switch off all PMIC outputs in halt. This is lowest possible power state for halt but may cause problems with some HATs because 5V will still be on. GLOBAL_EN must be shorted to ground to boot.

Default: 0 Version: 2019-07-15

This informs us that the GLOBAL_EN input node may be used in lieu of GPIO 3 to restart the RPi 4 in all possible configurations of these parameters. Three things should be noted re GLOBAL_EN:

  • It is not a GPIO
  • It is pulled up to 5V
  • The actual restart is not* triggered when GLOBAL_EN is pulled LOW by pressing the pushbutton, but when it is "released" and allowed to return to its "pulled-up" (logic HIGH) state.

The GLOBAL_EN node is shown in the photo below:

Summary for Option 2:

This is more difficult than Option 1, but offers the advantage of reduced power consumption in halt/shutdown. The most straightforward hardware implementation is to use two pushbuttons - one for startup, one for shutdown. If you want to take some additional effort, a single button startup & shutdown solution is illustrated here.

🌐
Vilros
vilros.com › pages › on-off-button
Raspberry Pi GPIO On Off Button – Vilros.com
This line will hook the specified pins to a system process to Shut Down/Turn on the Raspberry Pi in an orderly manner. Please note, there are two different ways to accomplish this change; choose the one that works best for you. Choice 1: Using Raspberry editor while running. Open the terminal and enter the following command to edit the file and add the special line at the end. sudo nano /boot/config.txt · Move your cursor to the last line of the file and add the following line: dtoverlay=gpio-shutdown,gpio_pin=3,active_low=1,gpio_pull=up,debounce=200
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › projects › automation, sensing and robotics
Turn GPIO Pins Off On Boot - Raspberry Pi Forums
All of the GPIO pins are turned ... the GPIO's default state to off from the time when I turn on my Pi? ... Not quite sure what you mean by on. It should be an input, pulled high by default. If you would like to change that behaviour, https://www.raspberrypi.org/documentati ...
🌐
Stack Overflow
stackoverflow.com › questions › 37721586 › disable-gpio-by-default-and-temporarily-activate-it-via-python-script
raspberry pi - Disable GPIO by default and temporarily activate it via python script - Stack Overflow
import time import sys def unexport(gpio): value = open("/sys/class/gpio/unexport","w") value.write(gpio) value.close() def checkHours: return int(time.strftime("%l")) def checkMinutes: return int(time.strftime("%M")) def checkAMorPM: return time.strftime("%p") def disableGpio3AM46: if (checkHours == 3) and (checkMinutes == 46) and (checkAMorPM == 'PM'): print 'amazing' unexport(gpio) ... Sign up to request clarification or add additional context in comments. ... Since posting this question I've figured out this way to disable and enable the pin/gpio through python:
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 78470100 › raspberry-pi-gpio-pin-not-turning-off
python - Raspberry pi GPIO pin not turning off - Stack Overflow
import RPi.GPIO as GPIO import time in1 = 29 # i.e 40th pin GPIO.setmode(GPIO.BCM) GPIO.setup(in1, GPIO.OUT) try: GPIO.output(in1, GPIO.HIGH) # 1 time.sleep(1) GPIO.output(in1, GPIO.LOW) # 2 print("inside try after low") # 3 time.sleep(1) except KeyboardInterrupt: GPIO.cleanup() # 4 · My problem: Once the relay turns high at comment # 1 it does not turns off at point # 2. I kill the program using 'control + c' then it turns off which is not what I desire. I just want it to turn on and then turn off. The point # 3 does get executed. # 4 executes when ctrl+c pressed. If I remove cleanup code and do not press ctrl+c then the relay's green light remains turned on continuously. This is raspberry pi 64bit OS.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › hardware and peripherals › raspberry pi pico › micropython
Enabling and disable a GPIO - Raspberry Pi Forums
It was surprisingly as simple as using >= or <= and it worked perfectly fine. Below is how we programmed Pi Pico: from machine import ADC, Pin from time import sleep POT = ADC(0) LED = machine.Pin(7, machine.Pin.OUT) while True: volt = POT.read_u16() if volt <= 43590: LED.value(0) elif volt >= 43591: LED.value(1)
🌐
Stack Exchange
raspberrypi.stackexchange.com › questions › 64192 › how-to-turn-off-all-gpio-pin-during-powering-up-raspberry-pi
pi 2 - How to turn off all gpio pin during powering up raspberry pi? - Raspberry Pi Stack Exchange
March 31, 2017 - I am trying to control linear acutronix RC servo using raspberry with the help of adafruit servo hat. While powering up raspberry pi without sending any commands linear motor moved to 100 percentage position.so I just wanted to know how I can turn off all gpio pin while powering up raspberry pi?
🌐
GitHub
gist.github.com › lbussy › 9e81cbcc617952f1250e353bd42e7775
External Button Shutdown for Raspberry Pi · GitHub
Load: dtoverlay=gpio-shutdown,<param>=<val> Params: gpio_pin GPIO pin to trigger on (default 3) For Raspberry Pi 1 Model B rev 1 set this explicitly to value 1, e.g.: dtoverlay=gpio-shutdown,gpio_pin=1 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".
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › hardware and peripherals › device tree
About RPI4, dtoverlay (gpio-shutdown / gpio-poweroff) - Raspberry Pi Forums
I connected one side of the momentary power switch to GPIO 3 (pin 5) and GPIO 17 (pin 11). The 2nd switch pin to GND. And added below to config.txt to have power on and power off functions working - works like a charm: dtoverlay=gpio-shutdown,gpio_pin=17,active_low=1,gpio_pull=up,debounce=1000 Your red LED will be on when you use gpio-shutdown.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › general programming discussion
On GPIO turn off power pins? - Raspberry Pi Forums
I have a device I want to turn on or off via GPIO that uses pins 2 (5v) and 6 (ground). I have seen many examples of controlling state of the various GPIO pins, but nothing about the power pins. just use a transistor driven by yet another GPIO to interrupt pin 2 to your device. Does that sound like an acceptable solution? If so, I can elaborate. François · Francois http://raspberry...
Top answer
1 of 1
1

How can i switch off the 5V pin? Is there a way?

As Milliways implies, a switch. However, if it's not being used to actually kill the power to the Pi but just the LCD, then you could use the same switch to trigger shutdown (for which you'll need more than just the 5V line connected, which is a bit of a complication, because you can't wire that and a GPIO together on a switch, although a simple voltage divider with two resistors would solve that problem).

Alternately, the "switch" could be an NPN transistor used to control the 5V power to the LCD -- or more precisely, the return to ground (because of the relationship between the collector, emitter, and base voltages).

If you use a GPIO that is by default pulled low, I'm pretty sure (but not 100% positive) that very late in the shutdown process it will go back to that default state, and thereby turn off the power to the LCD.

If not, it is just a matter of setting the GPIO programmatically at shutdown. There's no equivalent of /etc/rc.local for this although I believe cron has a feature that could be used, or you could write a systemd service file with an ExecStart and ExecStop. The former is it seems necessary to the latter, you can't have something stopped that isn't "started successfully" (man systemd.service), but there's probably some way to have something that just runs during shutdown if you dig around. Of course, since you do presumably want to turn the power on at some point, you could use the service for that -- and note "started successfully" could mean at any point, it doesn't have to be at boot.

Anyway, first you need an NPN transistor. These are super cheap (as in pennies) and super easy to use. Unfortunately, you cannot buy just one and they likely don't have any at the nearest grocery store, but what you are looking for is a bag of something like the very commonplace 2N2222.

🌐
LibreELEC
forum.libreelec.tv › thread › 9019-is-it-possible-to-turn-off-on-5v-gpio4-raspberry-pi-from-software
Is it possible to turn off/on 5v GPIO4 Raspberry Pi from software? - Hardware - LibreELEC Forum
June 22, 2017 - To control pins 2 and 4 you ll need to use a gpio and a relay or npn transistor...with that script you control the transistor to cut current from pin 4 by disconnecting pin 6 (Ground) ... I have libreelec 8.1.1 and RPi.GPIO add-on is not in the repo. Can someone point me in the right direction? I'm looking to write a couple scripts for fan and status led control, but can't seem to find anything useful in my searches. ... Is this what you'r looking for?This is a project using rpi fan control with speed control and rgb led... ... I suspect the problem is that you have an odroid c2 which is not a raspberry pi.
🌐
Embeddedpi
embeddedpi.com › documentation › gpio › mypi-industrial-raspberry-pi-psu-shutdown-gpio-line
MyPi Industrial Raspberry Pi Power Off Shutdown GPIO Line
Name: gpio-poweroff Info: Drives a GPIO high or low on poweroff (including halt) Load: dtoverlay=gpio-poweroff,= Params: gpiopin GPIO for signalling (default 26) active_low Set if the power control device requires a high->low transition to trigger a power-down. Note that this will require the support of a custom dt-blob.bin to prevent a power-down during the boot process, and that a reboot will also cause the pin to go low.
🌐
Instructables
instructables.com › circuits › raspberry pi
HOW TO TURN OFF RASPBERRY PI PROPERLY : 5 Steps - Instructables
June 11, 2019 - HOW TO TURN OFF RASPBERRY PI PROPERLY: Probably every RPi user once wonders how to turn off Raspberry Pi? You can't just turn off the power. If you do so, it is very likely one day SD card will be corrupted, and your RPi will not start up. You must first shut down OS, and only then you c…