Creating Power off Button In Raspberry Pi


Creating Power off Button In Raspberry Pi

Creat Power off Button In Raspberry Pi

In order to create an off button to properly shutdown Raspberry Pi rather than taking out the power cable to shut it down, the below lesson will help to create a new power button.

The set up requires on wires to make connections and does not require any other component, therefore, you just need to know how to connect the pins and you can make this.

However, the below lesson is written with a simple assumption that the user has a fare knowledge of Raspberry Pi and how to connect it with monitor and how to SSH and types some simple commands in the terminal of Pi.

Step 1:

Raspberry Pi – Python script

In order to enter you home directory, enter


In the terminal as in this lesson I assume you will be working from your home directory.

A script which can will help Pi to understand whether the button is closed needs to be downloaded. To get the script, type the below line of code in Raspberry terminal:

Once, downloaded, check if the file had been downloaded to your home directory. The command is:


The file with name should be present in your home directory

Step 2:

Raspberry Pi – Converting the download into hidden file

This is an optional step for the users who wish not to disclose the script with anyone or who wants the name of the file to be something easier to remember

For this, type in the terminal:

Once, done, again type the same command to go to home directory and the file should have become hidden now:

ls -al

There should be another file with “” as the name.

Step 3:

Modification in the script

Script ModificationScript Modification

The user woud require to understand which pin of Raspberry Pi can be used to connect the Off button. This can be checked using:

cat /proc/cpuinfo

On running the above line, check the value of Revision. Check this value in eLinux wiki page to know your model. Once, the PCB revision number and the model is known, the user needs to go to hobbytronics page to know the GPIO number. Since, the connection is to be made with GPIO pin and the ground pin, it would be good if GPIO pin is selected which is near the ground pin.

To open the above downloaded file for editing, type:


Change the GPIO number in the script where it reads “YOUR_CHOSEN_GPIO_NUMBER_HERE”. Save the script and exit it.  To do so, you can press:

CTRL + x



Step 4:

How to run the script during boot-up

Script Run During boot-up

The requirment is to run this script every time the boot-up takes place. To do so, open the rc.local file. To open type:

sudonano /etc/rc.local

Navigate to the bottom of the file and type the below line befor “exit 0” command:

python /home/pi/

Now save and exit as before:

CTRL + x



If the user wants some other scripts to run simultaneously along with this script at the boot-up, this can be done by adding below line instead of the previous one which is: python /home/pi/ “followed by path and script name of another script. & here runs both the script at the same time.

Step 5:

Raspberry Pi – Adding the button

Raspberry Pi - Add button

Adding Power ON-OFF Button

In this step, the focus is to add the switch off button. First connect the GPIO pin with the ground pin. However, the user should make sure that the chosen GPIO pin should be connected. Any wrong connection can result in short-circuit and looing Raspberry pi.

First check if the script is running correctly by re-booting Pi before establishing connections.

Caution: This step is important as the script will ensure that the internal pull up resistor is enabled. If the internal pull up resistor is not enabled, the Pi can be damaged completely.

User can use jumper wires, however, I used hookup wires and soldered the GPIO ping with the ground wire. The space between the two should be apt as more space may result in too much stress on pins which could break the pin and too less space may result in wires touching each other all the time. In order to avoid any static charge, taping on the wire can also be done.

Any type of wire can be used which can conduct electricity. You can use diode wire, cut-off resistor wire or anything, but in case you are soldering make sure to use one which can easily be soldered.

Another caution is that soldering process will violate the warranty of Raspberry Pi.

To ensure warranty is not violated, the user can use female to female jumper wires to establish connection.

Caution: do not keep the connections all the time, as the pin used may be used as an output before the scripts run and hence, it can damage the Raspberry pi permanently.

Step 6:


Once, the GPIO pin is connected to ground, the user can see an LED in green which will flash 10 times after sometime, which is an indication that a safe shut down can be performed and the user can remove power supply.


About Author

Leave A Reply