Git Product home page Git Product logo

pi-power-button's Introduction

pi-power-button

Scripts used in our official Raspberry Pi power button guide.

Installation

  1. Connect to your Raspberry Pi via SSH
  2. Clone this repo: git clone https://github.com/Howchoo/pi-power-button.git
  3. Optional: Edit line 9/10 in listen-for-shutdown.py to your preferred pin (Please see "Is it possible to use another pin other than Pin 5 (GPIO 3/SCL)?" below!)
  4. Run the setup script: ./pi-power-button/script/install

Uninstallation

If you need to uninstall the power button script in order to use GPIO3 for another project or something:

  1. Run the uninstall script: ./pi-power-button/script/uninstall

Hardware

A full list of what you'll need can be found here. At a minimum, you'll need a normally-open (NO) power button, some jumper wires, and a soldering iron. If you don't have a soldering iron or don't feel like breaking it out, you can use this prebuilt button instead.

Connect the power button to Pin 5 (GPIO 3/SCL) and Pin 6 (GND) as shown in this diagram:

Connection Diagram

Is it possible to use another pin other than Pin 5 (GPIO 3/SCL)?

Not for full functionality, no. There are two main features of the power button:

  1. Shutdown functionality: Shut the Pi down safely when the button is pressed. The Pi now consumes zero power.
  2. Wake functionality: Turn the Pi back on when the button is pressed again.

The wake functionality requires the SCL pin, Pin 5 (GPIO 3). There's simply no other pin that can "hardware" wake the Pi from a zero-power state. If you don't care about turning the Pi back on using the power button, you could use a different GPIO pin for the shutdown functionality and still have a working shutdown button. Then, to turn the Pi back on, you'll just need to disconnect and reconnect power (or use a cord with a physical switch in it) to "wake" the Pi.

Of course, for the GND connection, you can use any other ground pin you want.

pi-power-button's People

Contributors

dieseltravis avatar josephtyler avatar maddox avatar mahksy avatar terokorp avatar zlevine avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pi-power-button's Issues

CEC-off before shutdown

Hi,

I'm generally new to the Pi and it's features.
What I try to do is to insert a subprocess before the shutdown, to turn the tv off.

What I tried:
Create a monitor_off.sh-file with command: echo 'standby 0' | cec-client -s -d 1
Edit listen-for-shutdown-file to:

subprocess.call("sh monitor_off.sh", shell=True)
time.sleep(5)
subprocess.call(['shutdown', '-h', 'now'], shell=False)

Yet even if I delete the shutdown entirely, the Pi just reboots somehow.

Do you have a better Idea or a fix for my problem?

Possible to add long press wait to listen-for-shutdown.py?

I've been using your script for a while now and I really like how clean it is. I've used it on every Pi setup I've done so far (7 total for various around the house projects and for friends) and it has worked flawlessly!

Anyway I recently set up a Octoprint for my Maker Select Plus and installed a button on the side of my case. I have accidentally tapped the button while loading and unloading filament several times. I was looking to add a wait/time delay so that a long press would trigger the script. There are other tutorials out there but i really like what I've got with your script. Also many of these tutorials offer a reboot on a short press/shutdown on a long press. I am not a professional coder, changed majors from computer science way back in 1999 so it is all quite rusty.

So really I'd like your opinion on wether the modified code below would actually work or what you would do to change it. It's hacked together from other sources and modified to the best of my abilities. I imported time and sleep in the header. Only big issue based on my limited knowledge is the 'if' call at the beginning since there isn't an 'else' that follows. I learned C way back in the day so I thought it might need a 'do'.

Note: Formatting on inserting code is strange on Github. I'm pretty new here so I added quote indentions because the code did not format correctly when copying it here.

#!/usr/bin/env python

import time
from time import sleep
import RPi.GPIO as GPIO
import subprocess

GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.wait_for_edge(3, GPIO.FALLING)

def Long_press_shutdown:
#count how long the button is pressed
#not sure - is if: the correct call???

if
            counter = 0

            while ( pressed == 1 ):

                    if ( GPIO.input(3) == False ):

                            # button is still pressed

                            counter = counter + 1

                            # break if we count beyond 5 (to stop accidental shutdowns)
                            if (counter >= 5):

                                    pressed = 0

                            else:

                                    sleep(0.2)


                    else:
                            # button has been released

                            pressed = 0

            # button has been released, count cycles and determine action


            # count how long the button was pressed

            if (counter < 2):

                    # short press, do nothing
                    pressed = 0
                    int_active = 0

            else:
                    # longer press
                    if (counter < 5):

                            # longer press, initiate shutdown

                            print("system is shutting down...")

                            # run the shutdown command
                            subprocess.call(['shutdown', '-h', 'now'], shell=False)

#Use the interrupt from pin 3 to call the routine Long_press_shutdown
GPIO.add_event_detect(3, GPIO.FALLING, callback = Int_shutdown, bouncetime = 1000)

Please take a look and let me know!

Other GPIOs?

Is it possible to use other GPIOs? I already have those pins busy with a HAT 0.96 OLED screen. Thanks for the project it is brilliant and it works perfect.

Use another GPIO (not GPIO3)

Hello,

I'm testing this project. I have a problem.

Your example works ok for me.

In this moment I need to use the GPIO3, for the touchscreen.

So, Is it possible to use another gpio, not the gpio3 in this case?

Thanks!

Possible to use another pin?

Hey, I need both GPIO 2 and 3 for a GroveSensor module over I2C, is there any other pins the wakeup works with?

Kind regards,
Matthew

Program doesn't Activate on boot up.

Hello,
I have followed the instructions on your Howchoo article to make this code function... Only problem is, it doesn't start up during the boot. I have to use the command sudo /etc/init.d/listen-for-shutdown.sh start in the terminal every time to initialize shutdown if I want to use my button. I'm not sure why this is. I'm using a Raspberry Pi 4B 4gb.

[Feature Request] Python 3 support

I am not sure what is involved with updating unfortunately.

~ $ python3 /usr/local/bin/listen-for-shutdown.py
Traceback (most recent call last):
  File "/usr/local/bin/listen-for-shutdown.py", line 4, in <module>
    import RPi.GPIO as GPIO
ModuleNotFoundError: No module named 'RPi'

Changing the GPIO pin used for shutdown

I'm only interested in using this to shutdown and I cant seem to figure out where to find the file to edit the GPIO pin used. I would like to use GPIO23 (pin 16 on Pi B+). Thanks in advance.

RuntimeWarning

Hi,
When I launch your install script it says

=> Installing shutdown listener...

=> Starting shutdown listener...

Starting listen-for-shutdown.py
Shutdown listener installed.

/usr/local/bin/listen-for-shutdown.py:9: RuntimeWarning: A physical pull up resistor is fitted on this channel!
  GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)

Then it is not responding and I have to quit with Ctrl + C

How can I solve this problem?
Also the project is awesome ๐Ÿ˜„

Thanks.

Pi Power Button and OctoPrint-PSUControl

Hi all,

I'm using an RPi4 with an Octopi/Octoprint instance on it for 3D printer. In Octoprint software, i'm using
OctoPrint-PSUControl plugin to control a 3v3 relay on GPIO21 and through it power on and down 3D printer with Raspberry. And without the physical power button, this working as it must.

I successfully installed the power-button on GPIO3 and powering on and shutdown of raspberry is working well through the momentary button.

BUT. Then I send a command to close the relay and power on the printer through octoprint. The relay closes and the printer power on and at the same time raspberry seems to "receive" also the command to power off like if I push the new power button but I didn't touch it !

There must be a kind of interference of signal between Pi Power Button and OctoPrint-PSUControl. But I don't know where to start in investigations...

Any help will be very kind !

pull up resistor

I get that error on mi Pi 3B:
RuntimeWarning: A physical pull up resistor is fitted on this channel! GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)

Possible to use with a switch rather than button

I'm currently using this with a switch rather than a button. I have the switch connected between BCM 3 and ground; if the pi is in a low power state the switch shorting to ground will start the pi, when running (switch is on,) the gpio is pulled low, so when the switch is turned off the pin goes high due to internal pull-up. I have changed in listen-for-shutdown.py:

GPIO.wait_for_edge(3, GPIO.FALLING)
to
GPIO.wait_for_edge(3, GPIO.RISING)

which works great for turning pi on and off. The only issue I have is the pi will boot and stay on with the switch in the off position when power is applied. A quick boot and shutdown is acceptable with the switch in the off position. I was thinking to do a one time check on the pin after the internal pull-up is initialized and perform a shutdown if it is already hi, not 100% sure how to go about this in the cleanest way. Any suggestions.

Fan Is on after shutdown

Hi, Thanks for your great tutorial it really help,

Using this method the problem is Fan is still on after the shutdown. I tried the GUI method, sudo shutdown -h now and sudo poweroff but the fan is still on. Also I cannot disconnect the fan.

Is there is any way or setting to turnoff the fan.

Thanks

Uninstall script needed

Several users have asked how to uninstall the power button because they need to use GPIO3 for another project; right now, uninstall must be done manually.

We should write a script to automatically uninstall things.

RPi.GPIO not installed on OSMC

I liked the article so thought I'd add a switch to OSMC running on my RPi3. Unfortunately, it doesn't seem to have the RPi.GPIO module installed by default.

I got it working by following this post, but I'm new to this so I can't verify that everything in the post is really necessary. If somebody knows more, perhaps it could be added to the installation script.

One of the commenters on the accompanying article wrote:

Very good Post.
I found in my pi3 that RPi.GPIO were not loaded.
To fix it I did this:

sudo apt-get install gcc python-dev python-pip i2c-tools libi2c-dev python-smbus
sudo pip install RPi.GPIO

Link to original comment

Unable to Run Install Script.

I am getting this error when trying to run the install script.

Traceback (most recent call last):
File "/usr/local/bin/listen-for-shutdown.py", line 4, in
import RPi.GPIO as GPIO
ImportError: No module named RPi.GPIO

Python 3.x

64bit Bullseye on a RPi 4 b

Let me know if you need my information.

RuntimeWarning: A physical pull up resistor is fitted on this channel!

I'm having a lot of trouble getting a shutdown button to work, whether it's with adding dtoverlay=gpio-shutdown to config.txt or with your script.

Upon running sudo /etc/init.d/listen-for-shutdown.sh start I get:
RuntimeWarning: A physical pull up resistor is fitted on this channel!

I'd really like to get this button working but the extent of my raspberry pi knowledge is following tutorials and I can't seem to figure out what's wrong so I don't corrupt another SD card.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.