{"id":14440,"date":"2017-10-21T14:12:44","date_gmt":"2017-10-21T18:12:44","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=14440"},"modified":"2021-06-23T16:21:23","modified_gmt":"2021-06-23T20:21:23","slug":"combined-restart-shutdown-button-for-raspberry-pi","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2017\/10\/21\/combined-restart-shutdown-button-for-raspberry-pi\/","title":{"rendered":"Combined Restart \/ Shutdown Button for Raspberry Pi"},"content":{"rendered":"\n<p class=\"has-text-color\" style=\"color:#ff0000\"><strong>Hey! This is obsolete. There are better solutions out there now. You should use the system-supplied and supported <a href=\"https:\/\/github.com\/raspberrypi\/firmware\/blob\/master\/boot\/overlays\/README\"><em>gpio-shutdown<\/em><\/a> option instead.<\/strong><\/p>\n\n\n\n<p>A very simple systemd service for Raspberry Pi that provides a software-controlled restart \/ shutdown button. Code: <a href=\"https:\/\/github.com\/scruss\/shutdown_button\">scruss\/shutdown_button<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a id=\"user-content-use\" class=\"anchor\" href=\"https:\/\/github.com\/scruss\/shutdown_button#use\" aria-hidden=\"true\"><\/a>Use<\/h2>\n\n\n\n<p>Default behaviour is:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>your Raspberry Pi will <em>reset<\/em> if the button is held for more than two seconds but fewer than five seconds;<\/li><li>your Raspberry Pi will <em>shut down<\/em> if the button is held for more than five seconds.<\/li><\/ul>\n\n\n\n<p>By default, the software assumes the switch is connected to pin <a href=\"https:\/\/pinout.xyz\/pinout\/pin13_gpio27#\">BCM 27<\/a>. Both the pin and the timing can be changed in the Python source file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a id=\"user-content-requirements\" class=\"anchor\" href=\"https:\/\/github.com\/scruss\/shutdown_button#requirements\" aria-hidden=\"true\"><\/a>Requirements<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><a id=\"user-content-hardware\" class=\"anchor\" href=\"https:\/\/github.com\/scruss\/shutdown_button#hardware\" aria-hidden=\"true\"><\/a>Hardware<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>A Raspberry Pi (tested on a model 2B, 3B and Zero, and on a model B after minor software modification)<\/li><li>A normally open, momentary contact button. I use surplus ATX power buttons (as used on desktop PCs), as they&#8217;re cheap and come with a handy set of wires and header connectors. Virtually any button will do the job, though. Just make sure it&#8217;s normally open (push to close).<a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/10\/atx_power2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-14441\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/10\/atx_power2.jpg\" alt=\"\" width=\"360\" height=\"240\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/10\/atx_power2.jpg 360w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/10\/atx_power2-160x107.jpg 160w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/10\/atx_power2-320x213.jpg 320w\" sizes=\"auto, (max-width: 360px) 100vw, 360px\" \/><\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><a id=\"user-content-software\" class=\"anchor\" href=\"https:\/\/github.com\/scruss\/shutdown_button#software\" aria-hidden=\"true\"><\/a>Software<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>A Debian-based operating system that uses systemd (tested on Raspbian Jessie and Stretch)<\/li><li>the <code>python3-gpiozero<\/code> package to provide <a href=\"https:\/\/gpiozero.readthedocs.io\/en\/stable\/\">GPIO Zero<\/a> (tested on version 1.4.0)<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><a id=\"user-content-installation\" class=\"anchor\" href=\"https:\/\/github.com\/scruss\/shutdown_button#installation\" aria-hidden=\"true\"><\/a>Installation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><a id=\"user-content-hardware-1\" class=\"anchor\" href=\"https:\/\/github.com\/scruss\/shutdown_button#hardware-1\" aria-hidden=\"true\"><\/a>Hardware<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">40-pin GPIO connector (B+, 2B, 3B, Zero)<\/h4>\n\n\n\n<p>Connect the button between GPIO 27 and GND. If you use an ATX power button and a Raspberry Pi with a 40-pin GPIO header, connect it across the seventh column from the left:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n            -\n\u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7|\u00c2\u00b7|\u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \n\u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7|\u00c2\u00b7|\u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \u00c2\u00b7 \n            -\n\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/10\/gpio.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/10\/gpio-1024x768.jpg\" alt=\"\" class=\"wp-image-14442\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/10\/gpio-1024x768.jpg 1024w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/10\/gpio-160x120.jpg 160w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/10\/gpio-320x240.jpg 320w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/10\/gpio-768x576.jpg 768w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/10\/gpio.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>This shorts GPIO 27 (physical pin 13) to ground (physical pin 14) when the button is pressed.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">26-pin GPIO connector (models B and A only)<\/h4>\n\n\n\n<p>GPIO 27 is not exposed on the original Raspberry Pi header, so <a href=\"https:\/\/pinout.xyz\/pinout\/pin11_gpio17#\">GPIO 17<\/a> 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:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> . . . . ._. . . . . . . .\n . . . .|. .|. . . . . . .\n          -\n<\/pre>\n\n\n\n<p>You will also need to change <a href=\"https:\/\/github.com\/scruss\/shutdown_button\/blob\/master\/shutdown_button.py#L7\">line 7 of shutdown_button.py<\/a> to read:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">use_button=17\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a id=\"user-content-software-1\" class=\"anchor\" href=\"https:\/\/github.com\/scruss\/shutdown_button#software-1\" aria-hidden=\"true\"><\/a>Software<\/h3>\n\n\n\n<p><a href=\"https:\/\/github.com\/scruss\/shutdown_button\">Download the software first<\/a>. I prefer to use <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git clone https:\/\/github.com\/scruss\/shutdown_button.git<\/pre>\n\n\n\n<p>but you can download the zip file. If you do that, though, make sure to<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">unzip shutdown_button-master\ncd shutdown_button-master<\/pre>\n\n\n\n<p>The software is installed with the following commands:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsudo apt install python3-gpiozero\nsudo mkdir -p \/usr\/local\/bin\nchmod +x shutdown_button.py\nsudo cp shutdown_button.py \/usr\/local\/bin\nsudo cp shutdown_button.service \/etc\/systemd\/system\nsudo systemctl enable shutdown_button.service\nsudo systemctl start shutdown_button.service\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><a id=\"user-content-troubleshooting\" class=\"anchor\" href=\"https:\/\/github.com\/scruss\/shutdown_button#troubleshooting\" aria-hidden=\"true\"><\/a>Troubleshooting<\/h2>\n\n\n\n<p>Enabling the service should produce output very similar to:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCreated symlink \/etc\/systemd\/system\/multi-user.target.wants\/shutdown_button.service \u00e2\u2020\u2019 \/etc\/systemd\/system\/shutdown_button.service.\n\n<\/pre><\/div>\n\n\n<p>You can check the status of the program at any time with the command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"color: #000080;\"><code><span style=\"color: #000080;\">systemctl status shutdown_button.service\n<\/span><\/code><\/span><\/pre>\n\n\n\n<p>This should produce output similar to:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"color: #000080;\"><code><span style=\"color: #000080;\">\u00e2\u2014\u008f shutdown_button.service - GPIO shutdown button\n   Loaded: loaded (\/etc\/systemd\/system\/shutdown_button.service; enabled; vendor \n   Active: active (running) since Sat 2017-10-21 11:20:56 EDT; 27s ago\n Main PID: 3157 (python3)\n   CGroup: \/system.slice\/shutdown_button.service\n           \u00e2\u201d\u201d\u00e2\u201d\u20ac3157 \/usr\/bin\/python3 \/usr\/local\/bin\/shutdown_button.py\n\nOct 21 11:20:56 naan systemd[1]: Started GPIO shutdown button.\n<\/span><\/code><\/span><\/pre>\n\n\n\n<p>If you&#8217;re seeing anything <em>other<\/em> than <strong>Active: active (running)<\/strong>, it&#8217;s not working. Does the Python script have the right permissions? Is it in the right place? If you modified the script, did you check it for syntax errors?<\/p>\n\n\n\n<p>The output from <code>dmesg<\/code> will show you any error messages generated by the service.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a id=\"user-content-modifications\" class=\"anchor\" href=\"https:\/\/github.com\/scruss\/shutdown_button#modifications\" aria-hidden=\"true\"><\/a>Modifications<\/h2>\n\n\n\n<p>If you use a HAT\/pHAT\/Bonnet\/etc. with your Raspberry Pi, check <a href=\"https:\/\/pinout.xyz\/\">pinout.xyz<\/a> to see if it uses BCM 27. If you do need to change the pin, best to pick one that doesn&#8217;t have a useful system service like serial I\/O or SPI. If you&#8217;re using an ATX button with a two pin connector, make sure you choose a pin physically adjacent to a ground pin.<\/p>\n\n\n\n<p>If you modify the timing, please ensure that you keep the shutdown button press duration <em>longer<\/em> than the reboot one. Otherwise you&#8217;ll only be able to shut down.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a id=\"user-content-notes\" class=\"anchor\" href=\"https:\/\/github.com\/scruss\/shutdown_button#notes\" aria-hidden=\"true\"><\/a>Notes<\/h2>\n\n\n\n<p>You should not need to reboot to enable the service. One machine of mine \u00e2\u20ac\u201d a Raspberry Pi Zero running Raspbian Stretch \u00e2\u20ac\u201d did need a reboot before the button worked.<\/p>\n\n\n\n<p>The reboot code is based on the <a href=\"https:\/\/gpiozero.readthedocs.io\/en\/stable\/recipes.html#shutdown-button\">Shutdown button<\/a> example from the GPIO Zero documentation.<\/p>\n\n\n\n<p>This is not the only combined shutdown\/reset button project to use GPIO Zero. <a href=\"https:\/\/github.com\/gilyes\/pi-shutdown\">gilyes\/pi-shutdown<\/a> also does so, but pre-dates the implementation of the various hold time functions in GPIO Zero.<\/p>\n\n\n\n<p>GPIO 27 was used, as it&#8217;s broken out onto a physical button on the Adafruit <a href=\"http:\/\/adafru.it\/2423\">PiTFT+<\/a> display I own.<\/p>\n\n\n\n<p>This is my first systemd service, and I&#8217;m still at the \u00e2\u20ac\u0153<em>amazed it works at all<\/em>\u00e2\u20ac\u009d stage. The service file may not contain the ideal configuration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Connector Pinouts<\/h2>\n\n\n\n<p>From GPIO Zero&#8217;s <code>pinout<\/code> command<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a id=\"user-content-40-pin\" class=\"anchor\" href=\"https:\/\/github.com\/scruss\/shutdown_button#40-pin\" aria-hidden=\"true\"><\/a>40 pin<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n   3V3  (1) (2)  5V    \n GPIO2  (3) (4)  5V    \n GPIO3  (5) (6)  GND   \n GPIO4  (7) (8)  GPIO14\n   GND  (9) (10) GPIO15\nGPIO17 (11) (12) GPIO18\nGPIO27 (13) (14) GND   \nGPIO22 (15) (16) GPIO23\n   3V3 (17) (18) GPIO24\nGPIO10 (19) (20) GND   \n GPIO9 (21) (22) GPIO25\nGPIO11 (23) (24) GPIO8 \n   GND (25) (26) GPIO7 \n GPIO0 (27) (28) GPIO1 \n GPIO5 (29) (30) GND   \n GPIO6 (31) (32) GPIO12\nGPIO13 (33) (34) GND   \nGPIO19 (35) (36) GPIO16\nGPIO26 (37) (38) GPIO20\n   GND (39) (40) GPIO21\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><a id=\"user-content-26-pin\" class=\"anchor\" href=\"https:\/\/github.com\/scruss\/shutdown_button#26-pin\" aria-hidden=\"true\"><\/a>26 pin<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n   3V3  (1) (2)  5V    \n GPIO0  (3) (4)  5V    \n GPIO1  (5) (6)  GND   \n GPIO4  (7) (8)  GPIO14\n   GND  (9) (10) GPIO15\nGPIO17 (11) (12) GPIO18\nGPIO21 (13) (14) GND   \nGPIO22 (15) (16) GPIO23\n   3V3 (17) (18) GPIO24\nGPIO10 (19) (20) GND   \n GPIO9 (21) (22) GPIO25\nGPIO11 (23) (24) GPIO8 \n   GND (25) (26) GPIO7 \n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Hey! This is obsolete. There are better solutions out there now. You should use the system-supplied and supported gpio-shutdown option instead. A very simple systemd service for Raspberry Pi that provides a software-controlled restart \/ shutdown button. Code: scruss\/shutdown_button Use Default behaviour is: your Raspberry Pi will reset if the button is held for more [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[2145,993,2510,3086],"class_list":["post-14440","post","type-post","status-publish","format-standard","hentry","category-computers-suck","tag-obsolete","tag-power","tag-raspberrypi","tag-systemd"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-3KU","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/14440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=14440"}],"version-history":[{"count":8,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/14440\/revisions"}],"predecessor-version":[{"id":16796,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/14440\/revisions\/16796"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=14440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=14440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=14440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}