I implemented a simple script (well, someone else created it, just installed it hehe) to shutdown my raspberry pi when a button is pressed that connects pin 5 and 6 (I think those are the numbers - basically, GPIO3 and the ground next to it).

root@retropie:/home/pi# cat scripts/shutdown.py
import RPi.GPIO as GPIO
import time
import subprocess


# we will use the pin numbering to match the pins on the Pi, instead of the
# GPIO pin outs (makes it easier to keep track of things)
# use the same pin that is used for the reset button (one button to rule them all!)
GPIO.setup(5, GPIO.IN)

oldButtonState1 = True

while True:
        #grab the current button state
        buttonState1 = GPIO.input(5)
        #print "Button state: %s" % buttonState1

        # check to see if button has been pushed
        if buttonState1 != oldButtonState1 and buttonState1 == False:
                #print "Got ya!"
                # shutdown
                subprocess.call("shutdown -h -P now", shell=True,
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                oldButtonState1 = buttonState1

This works beautifully to both power down AND wake up the raspberry pi BUT I've noticed it will automatically start by itself with no apparent source / reason / event!

Any ideas on how I can debug what is causing it to wake up? There shouldn't be a bad pin connection to the button, otherwise it would power off while I'm using it. It just seems to randomly turn back on, not off!

I have a wireless keyboard / mouse dongle connected to it BUT the keyboard/mouse are both disconnected (in case that's what was pushing a wake event).

There are also 2 USB joysticks hooked up to it, but I tried waking it up by pressing the buttons and it doesn't seem to do it, so something else must be triggering the wake up!

Probably not related but just noticed this in dmesg:

root@retropie:/home/pi# dmesg | grep -i power
[    0.649860] Init: Port Power? op_state=1
[    0.649865] Init: Power Port (0)
[    5.770889] brcmfmac: power management disabled
[    6.772418] brcmfmac: power management disabled

Any thoughts on what I should look out for?

3 Answers 3


Firstly your title is misleading. You may have shut the Pi down, but not powered off.

The CPU halts, but the GPIO and Video Core continue. The Pi will draw slightly less power, because no programs are running, but is still powered up. (See https://raspberrypi.stackexchange.com/a/94140/8697).

The only reason to do this is so you can safely remove power.

Secondly there is no need to write code. Raspbian includes gpio-shutdown in the kernel, which can be activated by Device Tree. See https://raspberrypi.stackexchange.com/a/77918/8697

  • Thank you for the tips. I wanted to change the title but I can't figure out how to edit my post :\. May 10, 2019 at 15:42
  • There is an Edit button at the bottom
    – Milliways
    May 10, 2019 at 22:36
  • The gpio-shutdown works great (I was able to replace my script) but the problem persists. I'm going to try and replace the Pi to see if it is a hardware issue...(probably not but worth a try). May 10, 2019 at 23:37

I tried solving this with a replacement raspberry pi but it did not work (problem persisted).

HOWEVER, last night I removed the usb dongle for my wireless keyboard/mouse and it stayed powered down for the entire night!

So I THINK that was the problem. The usb dongle must have been signaling it to power back up (even though the keyboard/mouse were physically turned off and not pushing any events to it).

Thanks for your suggestions (I at least learned that I didn't need a python daemon wasting cycles, constantly reading the pins to determine if a button had been pressed)!



Rpi automatically turning on after putting to sleep using GPIO, ...

Automatically start by itself with no apparent source / reason / event!

It just seems to randomly turn back on, not off!


A year a ago I had a similar puzzle.

My hardware set up

These years I always do pair programming/development by using two side by side Rpis working at the same time, same SD card contents etc. This way I can easily troubleshoot doing compare and contrast by swapping PSU, long USB wires, etc.

The strange behaviour

Two Rpi worked smoothly side by side for a couple of months. Then one of them behaved strangely:

I used to use the sudo command "shutdown now" to shut them down. But then one day one Rpi did not shut down properly. It did shut down immediately, but then automatically booted up again, at random times.

I swapped "everything" between the two Rpis - SD card, PSU, long USB wire, removed all unnecessary things, like USB hubs, Ethernet cables, keyboard mouse wiring/dongle etc. But the strange event did not go away.

It did not bother me much, because "auto boot up" is no big deal comparing to "auto shut down", which is of course scary. So I took no action.

A couple of weeks passed by. One day I found the strange Rpi never booted up.

My quick and dirty conclusion is that when the strange Rpi started showing strange behaviour, it should had been already permanently partly damaged. The damage some how spread out day after day and finally the whole Rpi dropped dead.

To arrive at a postmortem I though hard to look back what did I do wrong before the strange Rpi got sick.

Then I remember one thing - In those days before the strange event, the strange PC almost always showed the "Low voltage Yellow Lighting after booting and/or Rainbow screen when booting. For a quick fix, I adjusted my regulated PSU for Rpi to 5.5V. It cured immediately and booting smoothly "ever after". I was happy and increased the dose to 6V. Everything went well and I forgot about this little fix.

But those were the days did not last long. My postmortem is that the strange auto reboot event was a symptom of sickness possibly caused by over voltage latching up, ...

/ to continue, ...

  • Thanks for sharing. Though I just tried to replace the raspberry pi and the problem persisted. HOWEVER, last night, I decided to remove the usb dongle for my wireless keyboard/mouse and the raspberry has NOT restarted in the last 8h or so! :). May 12, 2019 at 15:57
  • How nice. Perhaps the USB keyboard/mouse dongle take too much current and loads down the power supply. A stronger power supply might help.
    – tlfong01
    May 13, 2019 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.