Skip to content

Commit 57076eb

Browse files
committed
initial commit
1 parent 9e97fcf commit 57076eb

File tree

5 files changed

+298
-0
lines changed

5 files changed

+298
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

README.md

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
2+
# Raspberry Pi Power Button - Wake/Power Off/Restart(Double Press)
3+
4+
## Information
5+
6+
When Raspberry Pi is powered off, shortening GPIO3 (Pin 5) to ground will wake the Raspberry Pi.
7+
8+
This script will use pin GPIO3(5), Ground(6) with momentary button.
9+
10+
3V3 (1) (2) 5V
11+
GPIO2 (3) (4) 5V
12+
-----------------------
13+
| GPIO3 (5) (6) GND | <-
14+
-----------------------
15+
GPIO4 (7) (8) GPIO14
16+
GND (9) (10) GPIO15
17+
GPIO17 (11) (12) GPIO18
18+
GPIO27 (13) (14) GND
19+
GPIO22 (15) (16) GPIO23
20+
3V3 (17) (18) GPIO24
21+
GPIO10 (19) (20) GND
22+
GPIO9 (21) (22) GPIO25
23+
GPIO11 (23) (24) GPIO8
24+
GND (25) (26) GPIO7
25+
GPIO0 (27) (28) GPIO1
26+
GPIO5 (29) (30) GND
27+
GPIO6 (31) (32) GPIO12
28+
GPIO13 (33) (34) GND
29+
GPIO19 (35) (36) GPIO16
30+
GPIO26 (37) (38) GPIO20
31+
GND (39) (40) GPIO21
32+
33+
## Default Behavior
34+
35+
| __Button Press When Pi is On__ | __Description__ |
36+
| ----------------------------------- | --------------- |
37+
| Single | Nothing |
38+
| Double | Reboot |
39+
| Long and releases (Above 3 seconds) | Power off |
40+
41+
| __Button Press When Pi is off__ | __Description__ |
42+
| ------------------------------- | -------------------------- |
43+
| Single | Powers on the Raspberry Pi |
44+
45+
## Requirements
46+
47+
python3-gpiozero
48+
49+
Can be install via apt
50+
51+
```bash
52+
sudo apt install python3-gpiozero
53+
```
54+
55+
### Software
56+
57+
* A Debian-based operating system that uses systemd (tested on Jessie and Stretch)
58+
59+
* the `python3-gpiozero` package to provide [GPIO
60+
Zero](https://gpiozero.readthedocs.io/en/stable/) (tested on version 1.4.0)
61+
62+
## Installation
63+
64+
### Hardware
65+
66+
#### 40-pin GPIO connector (B+, 2B, 3B, Zero)
67+
68+
Connect the button between GPIO 27 and GND. If you use an ATX power
69+
button and a Raspberry Pi with a 40-pin GPIO header, connect it across
70+
the seventh column from the left:
71+
72+
-
73+
· · · · · ·|·|· · · · · · · · · · · · ·
74+
· · · · · ·|·|· · · · · · · · · · · · ·
75+
-
76+
77+
This shorts GPIO 27 (physical pin 13) to ground (physical pin 14) when
78+
the button is pressed.
79+
80+
#### 26-pin GPIO connector (models B and A only)
81+
82+
GPIO 27 is not exposed on the original Raspberry Pi header, so [GPIO 17](https://pinout.xyz/pinout/pin11_gpio17#) is a reasonable option. If you use an ATX power button and a Raspberry Pi with a 26-pin GPIO header, connect it across the fifth and sixth columns of the second row:
83+
84+
. . . . ._. . . . . . . .
85+
. . . .|. .|. . . . . . .
86+
-
87+
You will also need to change [line 7 of shutdown_button.py](https://github.com/scruss/shutdown_button/blob/master/shutdown_button.py#L7) to read:
88+
89+
use_button=17
90+
91+
### Software
92+
93+
The software is installed with the following commands:
94+
95+
sudo apt install python3-gpiozero
96+
sudo mkdir -p /usr/local/bin
97+
chmod +x shutdown_button.py
98+
sudo cp shutdown_button.py /usr/local/bin
99+
sudo cp shutdown_button.service /etc/systemd/system
100+
sudo systemctl enable shutdown_button.service
101+
sudo systemctl start shutdown_button.service
102+
103+
## Troubleshooting
104+
105+
Enabling the service should produce output very similar to:
106+
107+
Created symlink /etc/systemd/system/multi-user.target.wants/shutdown_button.service → /etc/systemd/system/shutdown_button.service.
108+
109+
You can check the status of the program at any time with the command:
110+
111+
systemctl status shutdown_button.service
112+
113+
This should produce output similar to:
114+
115+
● shutdown_button.service - GPIO shutdown button
116+
Loaded: loaded (/etc/systemd/system/shutdown_button.service; enabled; vendor
117+
Active: active (running) since Sat 2017-10-21 11:20:56 EDT; 27s ago
118+
Main PID: 3157 (python3)
119+
CGroup: /system.slice/shutdown_button.service
120+
└─3157 /usr/bin/python3 /usr/local/bin/shutdown_button.py
121+
122+
Oct 21 11:20:56 naan systemd[1]: Started GPIO shutdown button.
123+
124+
If you're seeing anything *other* than **Active: active (running)**,
125+
it's not working. Does the Python script have the right permissions?
126+
Is it in the right place? If you modified the script, did you check it
127+
for syntax errors? If you're using a model A or B with a 26-pin GPIO connector, did you make the modifications in the Python script to use GPIO 17 instead of 27?
128+
129+
The output from `dmesg` will show you any error messages generated by
130+
the service.
131+
132+
## Modifications
133+
134+
If you use a HAT/pHAT/Bonnet/etc. with your Raspberry Pi, check
135+
[pinout.xyz](https://pinout.xyz/) to see if it uses BCM 27. If you do
136+
need to change the pin, best to pick one that doesn't have a useful
137+
system service like serial I/O or SPI. If you're using an ATX button
138+
with a two pin connector, make sure you choose a pin physically
139+
adjacent to a ground pin.
140+
141+
If you modify the timing, please ensure that you keep the shutdown
142+
button press duration *longer* than the reboot one. Otherwise you'll
143+
only be able to shut down.
144+
145+
## Notes
146+
147+
You should not need to reboot to enable the service. One machine of
148+
mine — a Raspberry Pi Zero running Raspbian Stretch — did need a
149+
reboot before the button worked.
150+
151+
The reboot code is based on the [Shutdown
152+
button](https://gpiozero.readthedocs.io/en/stable/recipes.html#shutdown-button)
153+
example from the GPIO Zero documentation.
154+
155+
This is not the only combined shutdown/reset button project to use
156+
GPIO Zero. [gilyes/pi-shutdown](https://github.com/gilyes/pi-shutdown)
157+
also does so, but pre-dates the implementation of the various hold
158+
time functions in GPIO Zero.
159+
160+
GPIO 27 was used, as it's broken out onto a physical button on the Adafruit [PiTFT+](http://adafru.it/2423) display I own.
161+
162+
This is my first systemd service, and I'm still at the “amazed it
163+
works at all” stage. The service file may not contain the ideal
164+
configuration.
165+
166+
### Connector Pinouts
167+
168+
3V3 (1) (2) 5V
169+
GPIO2 (3) (4) 5V
170+
-----------------------
171+
| GPIO3 (5) (6) GND | <-
172+
-----------------------
173+
GPIO4 (7) (8) GPIO14
174+
GND (9) (10) GPIO15
175+
GPIO17 (11) (12) GPIO18
176+
GPIO27 (13) (14) GND
177+
GPIO22 (15) (16) GPIO23
178+
3V3 (17) (18) GPIO24
179+
GPIO10 (19) (20) GND
180+
GPIO9 (21) (22) GPIO25
181+
GPIO11 (23) (24) GPIO8
182+
GND (25) (26) GPIO7
183+
GPIO0 (27) (28) GPIO1
184+
GPIO5 (29) (30) GND
185+
GPIO6 (31) (32) GPIO12
186+
GPIO13 (33) (34) GND
187+
GPIO19 (35) (36) GPIO16
188+
GPIO26 (37) (38) GPIO20
189+
GND (39) (40) GPIO21
190+
191+
192+
*****
193+
194+
195+
# pi-power-button
196+
197+
Scripts used in our official [Raspberry Pi power button guide](https://howchoo.com/g/mwnlytk3zmm/how-to-add-a-power-button-to-your-raspberry-pi).
198+
199+
## Installation
200+
201+
1. [Connect to your Raspberry Pi via SSH](https://howchoo.com/g/mgi3mdnlnjq/how-to-log-in-to-a-raspberry-pi-via-ssh)
202+
1. Clone this repo: `git clone https://github.com/Howchoo/pi-power-button.git`
203+
1. Run the setup script: `./pi-power-button/script/install`
204+
205+
## Uninstallation
206+
207+
If you need to uninstall the power button script in order to use GPIO3 for another project or something:
208+
209+
1. Run the uninstall script: `./pi-power-button/script/uninstall`
210+
211+
## Hardware
212+
213+
A full list of what you'll need can be found [here](https://howchoo.com/g/mwnlytk3zmm/how-to-add-a-power-button-to-your-raspberry-pi#parts-list). 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](https://howchoo.com/shop/product/prebuilt-raspberry-pi-power-button?utm_source=github&utm_medium=referral&utm_campaign=git-repo-readme) instead.
214+
215+
Connect the power button to Pin 5 (GPIO 3/SCL) and Pin 6 (GND) as shown in this diagram:
216+
217+
![Connection Diagram](https://raw.githubusercontent.com/Howchoo/pi-power-button/master/diagrams/pinout.png)
218+
219+
### Is it possible to use another pin other than Pin 5 (GPIO 3/SCL)?
220+
221+
Not for full functionality, no. There are two main features of the power button:
222+
223+
1. **Shutdown functionality:** Shut the Pi down safely when the button is pressed. The Pi now consumes zero power.
224+
1. **Wake functionality:** Turn the Pi back on when the button is pressed again.
225+
226+
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.
227+
228+
Of course, for the GND connection, you can use [any other ground pin you want](https://pinout.xyz/).

install.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
wget url
4+
wget url
5+
6+
if [ ! -d "/usr/local/bin" ]; then
7+
sudo mkdir -p /usr/local/bin
8+
fi
9+
10+
sudo chmod +x power_button.py
11+
sudo mv power_button.py /usr/local/bin
12+
sudo mv power_button.service /etc/systemd/system
13+
sudo systemctl start shutdown_button.service
14+
sudo systemctl enable shutdown_button.service

power_button.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/python3
2+
from datetime import datetime, timedelta
3+
from time import sleep
4+
from signal import pause
5+
from gpiozero import Button
6+
import os
7+
8+
use_button = 3 # pin 5
9+
held_for = 0.0
10+
Button.pressed_time = None
11+
12+
13+
def released():
14+
global held_for
15+
if (held_for > 3.0):
16+
print("Shuting down...")
17+
os.system("shutdown now -h")
18+
else:
19+
held_for = 0.0
20+
21+
22+
def pressed(button):
23+
if button.pressed_time:
24+
if button.pressed_time + timedelta(seconds=1) > datetime.now():
25+
print("rebooting...")
26+
os.system("reboot")
27+
button.pressed_time = None
28+
else:
29+
# print("pressed once") # debug
30+
button.pressed_time = datetime.now()
31+
32+
33+
def held():
34+
global held_for
35+
held_for = max(held_for, button.held_time + button.hold_time)
36+
37+
38+
button = Button(use_button, hold_time=1.0, hold_repeat=True)
39+
button.when_held = held
40+
button.when_released = released
41+
button.when_pressed = pressed
42+
43+
pause()

power_button.service

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[Unit]
2+
Description=GPIO power button
3+
After=network.target
4+
5+
[Service]
6+
Type=simple
7+
Restart=always
8+
RestartSec=1
9+
User=root
10+
ExecStart=/usr/bin/python3 /usr/local/bin/power_button.py
11+
12+
[Install]
13+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)