Top Logo Sp Logo

Raspberry Pi GPIO On Off Button

Connect the button to Pin 5 (GPIO 3) and Pin 6 (GND) and be sure to fully connect the wires 

See Image below showing which pins to connect to: 

In order to program the button, we need to add one line to the end of a system file named config.txt which is in the /boot folder. 

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

Save the file by pressing: 
CTRL+X and then Y, and then ENTER 

You will need to reboot your Raspberry Pi for this change to take effect. (sudo reboot or via the shutdown menu)

Done! 

 

 

Choice 2: Setup shutdown via Windows 

This is an alternate way to make the necessary changes to connect the button to the process to initiate a system shutdown. 

This method uses a Windows based computer to make the changes to the same file as outlined in Choice 1. A Windows computer can read and write the config.txt file directly on the SD card. 

Insert the SD card (using a USB SD card carrier or other method to attach the SD card). Windows should ask or allow you to open the File Explorer and see the files on the SD card. 

Use and editor like Notepad (not a word processing editor like MS Word) to open the file config.txt Move to the end of the file and add the same line as given in Choice 1 

dtoverlay=gpio-shutdown,gpio_pin=3,active_low=1,gpio_pull=up,debounce=200

Save the file and be sure to either eject the SD card on File Explorer or safely eject the file from the task bar. If you do not safely eject the SD card, it may become unusable. 

Remove the SD card and insert the card in the Raspberry Pi and boot normally. 

Done! 

Reboot the RPi and the button should act as a shutdown button.
----------------------------------------------------------------------------------------------------------

Important Notes: 

  1. You can connect the button to any GPIO/GND Pin combination (as long as they are next to each other) and change the GPIO number in the line of code to use.
    However please note that it will then work as a shutdown pin ONLY. (GPIO 3 is required for the button to work as ON button as well) 
  1. You can easily program a longer press/hold on the button to require a longer press/hold by changing the number at the end of the line.

In the example below the button is programmed via GND and GPIO 17 (Pin Numbers 9 & 11) and requires a button hold of a full second.

dtoverlay=gpio-shutdown,gpio_pin=18,active_low=1,gpio_pull=up,debounce=1000

 

  1. When programming via a computer please be aware that Windows does not have access to many of the files used by the Raspberry Pi, nor will it allow you to read/write any of the files created for use (such as the default user pi).

This technique is only to change a few systems configuration files.