{"id":204,"date":"2016-04-20T23:51:49","date_gmt":"2016-04-20T23:51:49","guid":{"rendered":"http:\/\/www.bergrans.com\/site\/?p=204"},"modified":"2021-02-19T21:04:40","modified_gmt":"2021-02-19T21:04:40","slug":"atx-style-power-switch-for-raspberry-pi","status":"publish","type":"post","link":"http:\/\/www.bergrans.com\/site\/atx-style-power-switch-for-raspberry-pi\/","title":{"rendered":"ATX style power-switch for Raspberry Pi"},"content":{"rendered":"<p>The Raspberry Pi lacks a decent power ON\/OFF button. With a handful of cheap components a ATX style power-switch can be build.<\/p>\n<h2>Schematics<\/h2>\n<p>Main parts are a PIC microcontroller, a 5V relay, a push button and a LED.<\/p>\n<p><a href=\"https:\/\/easyeda.com\/bergrans\/Pi_ATX_power_switch-OFNVrlfxr\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft wp-image-247 size-full\" src=\"http:\/\/www.bergrans.com\/site\/wp-content\/uploads\/2016\/04\/Schematic_Pi-ATX-power-switch_Pi-ATX-power-switch_20190204223212.png\" alt=\"\" width=\"803\" height=\"629\" srcset=\"http:\/\/www.bergrans.com\/site\/wp-content\/uploads\/2016\/04\/Schematic_Pi-ATX-power-switch_Pi-ATX-power-switch_20190204223212.png 803w, http:\/\/www.bergrans.com\/site\/wp-content\/uploads\/2016\/04\/Schematic_Pi-ATX-power-switch_Pi-ATX-power-switch_20190204223212-300x235.png 300w, http:\/\/www.bergrans.com\/site\/wp-content\/uploads\/2016\/04\/Schematic_Pi-ATX-power-switch_Pi-ATX-power-switch_20190204223212-768x602.png 768w, http:\/\/www.bergrans.com\/site\/wp-content\/uploads\/2016\/04\/Schematic_Pi-ATX-power-switch_Pi-ATX-power-switch_20190204223212-624x489.png 624w\" sizes=\"(max-width: 803px) 100vw, 803px\" \/><\/a><\/p>\n<p><!--more Read on ... --><\/p>\n<h3>How does it work?<\/h3>\n<h4>Power UP<\/h4>\n<p>When S1 is pushed, the micro controller will be power through D4, D3 en D2. These diodes will reduce the supply voltage for U1 to around 3,3V so it can directly interface with the GPIO pins of the Raspberry Pi.<\/p>\n<p>The embedded PIC software will directly activate RA5 on power-up. This will activate the relay and 5V will be available on P2 to power the Raspberry Pi. The PIC is now powered through D5. Also RA1 is set high to tell the Raspberry Pi to keep running. The LED will blink until a high signal, from the Raspberry Pi, is received on RA0 (the Python script below will handle that).<\/p>\n<h4>Power DOWN<\/h4>\n<p>When S1 is pushed, to power down the Pi, high signal is received on RA2. Now RA1 is set low to tell the Pi to shutdown (the Python script below will handle that). The LED will start blinking again and we wait for a low on RA0 from the Pi, indicating the Pi has shutdown. Now, with a little delay, the relay is switched off and so is the Raspberry Pi.<\/p>\n<h2>Embedded software<\/h2>\n<p>I used <a href=\"http:\/\/www.microchip.com\/mplab\/mplab-code-configurator\" target=\"_blank\" rel=\"noopener\">MPLAB X<\/a> to write and compile the code using the <a href=\"http:\/\/www.microchip.com\/mplab\/compilers\" target=\"_blank\" rel=\"noopener\">free compiler from Microchip<\/a>. I also used the <a href=\"http:\/\/www.microchip.com\/mplab\/mplab-x-ide\" target=\"_blank\" rel=\"noopener\">MPLAB Code Configurator<\/a> that comes with MPLAB X, to setup all the registers and handy functions in no time. The code can be found on <a href=\"https:\/\/github.com\/bergrans\/ATX-Pi-switch\" target=\"_blank\" rel=\"noopener\">GitHub<\/a>. Clone the repo or download the ZIP. Open the project in MPLAB X to build and program the PIC microcontroller.<\/p>\n<p>The PIC can be programmed by using connector P3. I used an ICD3 but most programmers will work just fine.<\/p>\n<h2>Raspberry Pi software<\/h2>\n<p>For this part I started with the code from the <a href=\"https:\/\/www.pi-supply.com\/pi-supply-switch-v1-1-code-examples\/\" target=\"_blank\" rel=\"noopener\">Pi Supply website<\/a>. I used GPIO pin 11 as output and pin 13 as input. You can change them if you like to use other pins.<\/p>\n<p>Create a python file with the content below. Save it in your user root folder (probably <strong>\/home\/pi<\/strong>) and name it something like <strong>softshut.py<\/strong> or whatever looks logic to you.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"># Import the modules to send commands to the system and access GPIO pins\r\nfrom subprocess import call\r\nfrom time import sleep\r\nimport RPi.GPIO as gpio\r\n\r\n# Define a function to keep script running\r\ndef loop():\r\n  raw_input()\r\n\r\n# Define a function to run when an interrupt is called\r\ndef shutdown(pin):\r\n  sleep (0.1); # 100ms debounce delay\r\n  if not gpio.input(13):\r\n    # You might want to do something before shutting down the Pi\r\n    # add these code lines here (see example line below)\r\n    # call(&#x5B;'mpc', 'pause'], shell=False)\r\n    call('halt', shell=False)\r\n\r\ngpio.setmode(gpio.BOARD) # Set pin numbering to board numbering\r\ngpio.setup(13, gpio.IN) # Set up pin 13 as an input\r\ngpio.setup(11, gpio.OUT, initial=gpio.HIGH) # Set up pin 11 as an output\r\n\r\ngpio.add_event_detect(13, gpio.FALLING, callback=shutdown) # Set up an interrupt to look for button presses\r\n\r\nloop() # Run the loop function to keep script running<\/pre>\n<p>Next edit the file\u00a0<strong>\/etc\/rc.local<\/strong>, by adding the follow line:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">python \/home\/pi\/softshut.py<\/pre>\n<p>Just above the (last) line:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">exit 0<\/pre>\n<p>In some cases it might be necessary to add an ampersand, to run the script in the background, like so:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">python \/home\/pi\/softshut.py &amp;<\/pre>\n<h3>How does it work?<\/h3>\n<p>When the Raspberry Pi is powered up the <strong>rc.local<\/strong> file is executed and so is the <strong>softshut.py<\/strong> script. This script will only set the output pin (11) high on start-up and then &#8220;watch&#8221; for the input pin (13) to go low. When the input pin goes low the <em>halt<\/em> command will nicely shutdown the Pi.<\/p>\n<p>That&#8217;s it.<\/p>\n<h2>Connecting it to the Pi<\/h2>\n<p>Connector P2 will power the Raspberry Pi. You can use the USB connector for that or the 5V en GND pins on the GPIO header of the Pi.<\/p>\n<p>Next are the IO lines from P3.<\/p>\n<pre>P3 pin 2 RA1 ----------- &gt; --------- GPIO pin 13\r\n\r\nP3 pin 3 RA0 ----------- &lt; --------- GPIO pin 11\r\n\r\nP3 pin 4 GND ----------------------- GPIO GND<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Raspberry Pi lacks a decent power ON\/OFF button. With a handful of cheap components a ATX style power-switch can be build. Schematics Main parts are a PIC microcontroller, a 5V relay, a push button and a LED.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3],"tags":[19,16,18,20],"_links":{"self":[{"href":"http:\/\/www.bergrans.com\/site\/wp-json\/wp\/v2\/posts\/204"}],"collection":[{"href":"http:\/\/www.bergrans.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.bergrans.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.bergrans.com\/site\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bergrans.com\/site\/wp-json\/wp\/v2\/comments?post=204"}],"version-history":[{"count":40,"href":"http:\/\/www.bergrans.com\/site\/wp-json\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":256,"href":"http:\/\/www.bergrans.com\/site\/wp-json\/wp\/v2\/posts\/204\/revisions\/256"}],"wp:attachment":[{"href":"http:\/\/www.bergrans.com\/site\/wp-json\/wp\/v2\/media?parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.bergrans.com\/site\/wp-json\/wp\/v2\/categories?post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.bergrans.com\/site\/wp-json\/wp\/v2\/tags?post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}