Connecting an On/Off button to your Pi


Power OFF & power ON the Pi

Check to see if I2C is switched on: $ sudo raspi-config Switch I2C off, unless it is used. If that is the case, see below how to use an altenative GPIO port.

Hardware
Nothing else is required except to connect a momentary normally open pushbutton switch between physical pins 5 and 6. Actually it is pin 5 and ground, it doesn't have to be pin 6. It can be every ground pin.

Software
You don't need to install anything or write a script, it's built into Stretch release 2017-08-16 and up and can be enabled by simply adding the following line to /boot/config.txt : $ sudo nano /boot/config.txt dtoverlay=gpio-shutdown Reboot

The Pi doesn't fully power off, just shuts down and goes to the very start of the boot sequence. At that point it sits and waits for pin 5 to be connected to a ground pin before continuing with the boot.

Please note, if you have I2C activated, physical pin 5 is the I2C Clock pin. If so, this shutdown method will NOT work!


What to do if I2C is activated?

If you already using pin 5 for I2C, you can use another GPIO pin, but with a different overlay (pin number is an example): dtoverlay=gpio-shutdown,gpio_pin=26 The gpio_pin is the GPIO number, not the physical pin number (GPIO26 = physical pin 37).

Please note, using an alternate GPIO pin will not provide a boot-up function as will pin 5.