Pi shutdown button

Is there a way to add a shutdown button to the gpio pins to shutdown the pi? I’ve got the pi and printer running off the same psu and I’d like to be able to turn everything off without having to switch my computer back on to perform a shutdown and I can’t see a way to do it from OctoClient. I’ve been looking for 3 days and all I can find is tutorials on shutting down the pi, but not with octoprint installed and tutorials on how to turn off the printer when the print is finished but all I want to do is shut the pi down manually then flick the power switch. Any help would be much appreciated as I’m pulling my hair out on this one.




Definitely, you'll want to get your GoogleFu up to speed in order to work on a 3D printer.

I'm fond of the TP-Link SmartPlug device which allows me to attach an IKEA KOPPLA power strip to this. One goes to the printer board, one goes to the Raspberry Pi's power adapter. So now, you can programmatically tell the TP-Link to power off, turning off all the devices at once.

Now for the graceful shutdown, you'll need to tell Raspbian to do so. The REST API within OctoPrint can be exercised to do the shutdown.

As for the button itself, you'll need to create a script to do something when a button is pressed.

Am I missing something. . top of the gui in about the center is a power button icon, click it and select shutdown system.... Then turn off the power to printer and pi..

I am in the process of building new 3d printer and added RPI shutdown button to it just 2 days ago.
I used push button, pull-up resistor and a buzzer. The button is connected to Ground (pin 39) and GP17 (pin 11). 10 kOhm pull-up resistor is connected to +3.3v (pin 1) and GP17. Buzzer, which is optional, is connected to GP19 (pin 35) and Ground. I wrote a script that starts at RPI boot and runs infinite loop checking if the button is pressed for longer than 5 seconds. If it is, buzzer beeps and RPI shutdown is initiated.

If this is something you are interested in, I can share the script.


1 Like

That would be awesome if you could share that! The buzzer would definitely be added peace of mind. As I’m a bit of a noob I just need to figure out how to run a script in the background but I’m sure there is plenty of information about that on the interwebs. Thanks in advance :+1:t2:

First of all - I am very new to Python, so the script is most likely very far from being perfect, but it works.
I am executing it from root cronjob with command:

@reboot /home/pi/add_on_scripts/shutdown_button.py &

The buzzer that I am using does not have built in oscillator, that's why the script is in charge of generating the tone.

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

Buzzer = 35      # pin-35 BCM-19/GPIO-24
Button = 11      # pin-11 BCM-17/GPIO-0

Freq = 0.00175

def setup(buzzer_pin, button_pin):
    global BuzzerPin
    global ButtonPin
    BuzzerPin = buzzer_pin
    ButtonPin = button_pin
    GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
    GPIO.setup(BuzzerPin, GPIO.OUT)
    GPIO.output(BuzzerPin, GPIO.LOW)
    GPIO.setup(ButtonPin, GPIO.IN)

def on():
    GPIO.output(BuzzerPin, GPIO.LOW)

def off():
    GPIO.output(BuzzerPin, GPIO.HIGH)

def beep(x):
    for i in range(50):

def loop():
    n = 0
    while True:
        if GPIO.input(ButtonPin):
            n = 0
            n += 1
        if n == 5:
            from subprocess import call
            call("sudo shutdown -h now", shell=True)
            # print("Shutting down")

def destroy():
    GPIO.output(BuzzerPin, GPIO.LOW)
    GPIO.cleanup()             # Release resource

if __name__ == '__main__':     # Program starts here
    setup(Buzzer, Button)
        if GPIO.input(ButtonPin): # pull-up resistor is connected start infinite loop
    except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.

1 Like

Take a look at foosel's post here and my previous script attempt before that. She's using an event_detected litmus test to lower the amount of processing time.

@OutsourcedGuru Thanks for the link. I will try event_detected, compare CPU load and report the results.

I’m wanting to shut down the pi without having to turn my desktop back on.

Yeah I finally figure that out.. just never turn off my computers so it didn't click at first.

Well, I'll show you what I made but it would be too complicated to write-up a step-by-step tutorial for it.

one button to rule them all

You don't need this to be remote so you wouldn't need 75% of the electronics I used. You simply need a button, two wires over to the GPIO pins of the Raspberry and what I discussed earlier.

And of course you'll need the Python code that lmcbmai has provided at least to start.

I managed to install a shutdown button on a raspberry pi running retropie which was a pretty easy task but unfortunately getting one on octoptint is way beyond my skill set. Im in terminal looking at pi@octopi:~ so do you navigate around the same as if you were in pi@raspberry:~ ?. I feel silly for asking but my google fu is out of order on this one.

Yes. It simply has a different hostname (octopi instead of raspberry), that is all.

One easy option is to simply download the octoprint app for a mobile device phone tablet what ever and it takes 2 seconds to shut it down with the app then just unplug it or if you have a switch on the power supply flip the switch.

1 Like

How do you do a shutdown from the OctoClient? I can’t find a shut button on the app and I can’t find the gcode for shutdown. I tried sudo shutdown-h now with no success and also I need to make the pi address static so I can actually connect to the printer without turning on the computer to access the modem ect ect. I appreciate the help but I’d like a way to turn off the printer all in its own, the old fashioned way.

what do you want to do, shut down the pi or the printer? If the printer, then just turn it off pi can keep running. Last I understood what you wanted, you wanted to shutdown the pi without using the octopi gui.
If you want to shut down the pi from the octoprint gui, top right of center is a power button icon, click it and select shutdown system count to 10 and then turn off the power to the pi and printer. Obviously you have to login to the octoprint and be a user that is setup for controlling the system..

Sorry I should’ve been more clear, when I said printer I meant printer and pi. I have the pi and printer running off the same psu so if I shutdown the pi I can then just flick the power switch on the psu and power will be cut to everything.

That's is what I thought. So you have said you can not find the button on the GUI to shut down the PI, or at least I think that is what you meant. Select shutdown system

Or am I still not understanding that what it is you can not find in the GUI?

I can find the shutdown button on the GUI, it’s the OctoClient app where I can’t find the button and I tried typing “shutdown -h now” into the terminal on the app and that didn’t work.

1 Like