lego (12) Linux (39) pi (20) Thinkpads (4)

Wednesday, 25 September 2013

Adding a Power Switch to a Raspberry Pi

Challenge:  The Raspberry Pi always remains powered-on as long as there is power coming in on the micro-USB power line.  A shutdown/halt/poweroff command will shutdown the OS and leave the system in a powered on but idle state.  The power consumption would be unchanged between idle and active on the Raspberry Pi.  Further, to turn the system back on, the USB cable needs to be unplugged or the power supply needs to be unplugged (reset).

Ideally we need....

  • something that powers the raspberry pi off when it is shutdown (power consumption becomes negligible / < 0.5 W) [POWER OFF]
  • something that can trigger a clean software shutdown [CLEAN SHUTDOWN]

Mediation:  All my TVs supply a "service" USB port, and when tested, the port was supplying enough power to power either a model B (3.5 W) or model A (1.5 W).  Therefore, I've been plugging in the raspberry pi directly into the TV for the power source.  I just remember that whenever I turn off the TV and I need to "shutdown" the raspberry pi (ssh into it to run the shutdown command or from XBMC).  If the raspberry pi is idling, it's not as risky to just cut the power, although, like any computer,  it is always best to shutdown safely (you stand the risk of corrupting the filesystem).  Also, whenever the TV is on, the raspberry pi is on.  In XBMC, you can enlist an idle timeout to shutdown after X minutes of idle use.  Remember the raspberry pi will continue to consume power from the TV even if it is shutdown.

Solution 1: Panic Button

I purchased a Panic/Stress relief USB button from TheSource for clearance ($4.96 -  I knew it would feature essentially a USB interface to 2 i/o streams that would be either high or low.  I could then write a simple shell script that polls the stream and performs a clean poweroff when the button is pressed.


  • cheap and resourceful
  • kind of geeky (re-pursing a device)
  • the casing can be taken apart and just the logical board with usb cable can be deployed


  • a USB device that occupies a USB port
  • doesn't provide a full power off state
  • doesn't address the POWER OFF requirement

Solution 2: Brew your own circuit

Use a breadboard and construct a logical switch to perform the same function as solution 1.


  • extremely geeky


  • more expensive (the components are fairly cheap, but you need to buy them in qtys of 5-10, and the price starts to add up)
  • very susceptible to movement
  • size of the breadboard alone becomes impracticable for scenarios where the footprint of the device is a factor
  • doesn't address the POWER OFF requirement

Solution 3: Buy a PCB

Since this is a common problem, some smart people have come up with solutions

Essentially, this circuit board is place inline with the micro-USB power line, with a single connection to the GPIO.  It cuts the power to the


  • extremely geeky
  • very small
  • easy to integrate with any setup
  • doesn't affect your case design


  • very expensive (15 pounds converts to just under $25 Canadian, the price of a raspberry pi)
  • you need to construct the circuit yourself (solder the components onto the PCB)
  • would still need to add a separate button circuit or usb device to allow for triggering a shutdown
  • doesn't address the CLEAN SHUTDOWN requirement

Solution 4: Direct Shutdown PCB

The best solution I found was a fully assembled direct shutdown PCB from a hobbiest

He offers a wide range of solutions with various style switches (including add-your-own).  He will even work with your design requirements and alter the switch.

Essentially, this circuit board is place inline with the micro-USB power line, with two connections to the GPIO (1 input, 1 output).  A press of the button or a toggle of the switch takes the unit from off (complete power off for the raspberry pi) to on mode.  A shell script starts on bootup that passes a high/1 to the switch to instruct indicate the raspberry pi is on.  A low/0 signal passes from the switch to the raspberry pi to indicate the switch is neutral.  A toggle of the rocket switch to off or holding a press button switch for two seconds changes the output to high/1 which the polling script triggers a safe poweroff.  When the raspberry pi has finally fully shutdown, the GPIO goes from high/1 to low/0, which tells the switch the raspberry pi power can be cut safely.  The rocket-style switches also come with a reset button that can trigger a raspberry pi reset if it becomes frozen, etc -- something that normally would require to pull the power line.

The switch also detects software shutdowns invoked by the user (as the GPIO will toggle from high/1 to low/0, which the switch polls for).


  • very small
  • may need some modification to your raspberry pi case
  • addresses the CLEAN SHUTDOWN and POWER OFF requirements fully.
  • everything fully assembled


  • expensive but cheaper then any alternatives

I purchased a number of the switches from Mausberry Circuits.  The illuminated push button switch easily integrates into my lego design cases -- the added width is 1 lego block.

Here's what my case looked like before adding the switch:

I had to rip down the right-side wall to set it up.  But it is lego after all.  Time to integrate the switch into the case!

Solution 5RemotePi Board by MSL Digital Solutions  (added December 2013)

At its core, the RemotePi Board by MSL Digital Solutions promises the same functionality has the that intercept the micro USB power connector on the Raspberry Pi, which is to control the power on/off state of the Pi through a switch and through software.  But instead of intercepting the power flow through the micro USB power connector, it controls the power through the GPIO.  Instead of connecting your power to the micro USB power connector on the Raspberry Pi, you connect it to this board.  You then either toggle the power state by pressing the black button on the top, or better, you can use a IR remote to toggle the power.

  • very small
  • may need some modification to your raspberry pi case, but otherwise fits within the existing case (doesn't extend out from the board's footprint)
  • addresses the CLEAN SHUTDOWN and POWER OFF requirements fully
  • no issues with provided switch script (for other switches, I had problems and I had to make customizations to the scripts provided by other offerings)
  • fully integrated IR receiver that can be used to allow your IR remote to control XBMC
  • everything fully assembled


In action:

Integrated into a lego case:

The power button is accessible from the top:

My full review of this product is available in this post.


  1. Hi Dylan,

    You could add another power switch option to your list, the RemotePi Board is a power switch that allows to use an existing infrared remote control.
    More info at
    Thanks !

  2. Thanks, I will try buying one and review it. The video showing it's use clarifies how simplistic its operation is, plus you solve two problems with one solution -- power switch and remote. Just the other day I was sitting on the couch ready to do some XBMC watching (with TV already on), forcing me to get up and power on the pi.

  3. Hi Dylan,

    Thanks for sharing your lego case design and switch review. I'm considering using the illuminated button swtich with my Pi. Can you tell me if this switch has a reset button as well....or does it work by pressing long on the illuminated button to hard reset the Pi (not that I would condone doing this...but if it was frozen I'd prefer using a button than having to fiddle with the power plug).

    Thank you.

    1. Hi, What are you going to be using it for? My PIs rarely "freeze". My XBMC PIs appear to "freeze" often, but its actually just XBMC frozen (taking up 100% cpu in some endless loop), but I'm able to either SSH into it and do a reboot or toggle the power switch to power off (and then back on). Only real times I've had real freezes was when I was using incorrect overclocking settings -- which I try to avoid as they almost always eventually corrupt my SD card. To get back onto topic, the illuminated switch doesn't have a reset function in the button logic. But if your PI is truly frozen, then if you turn the switch to off, after a minute, the power would be cut to the PI regardless if it has shutdown safely.

      Some of the button logic is a little "messed up". A quick press of the button turns the switch on and a prolonged hold turns it off. But I have often held the switch too long trying to start it up so that it automatically tries to turn off after it has turned on. I actually had to modify the script to detect if the switch is in off mode on boot up, and therefore, ignore the switch, otherwise I get in an infinite loop of the system turning on and off. Looking on the board, I see there circuit there for the reset switch, so you could solder a separate switch or jumper.

      I also have his rocket switch, and I have that integrated into another PI that I haven't photographed yet. That one has a reset button. I actually find it more handy. It's the one I decided to use on my server PI that has integrated USB lighting, USB hub and 2.5 HD. A real nice setup. The switch will even turn off the PI if it is plugged into a hub.

      I also received a separate switch altogether the other week that I'm going to posting how I integrated it into one of my units. It has an integrated IR, so it can turn on/off by remote command. It also has some built in behaviour to avoid the endless on/off loop that the illuminated switch seems to have (this switch will ignore any repeated power on/off commands immediately for 10 seconds following the initial). It also has a LED to indicate the power state, which integrates nicely in a XBMC setup. No reset button per-say on this unit either, but the manual power on/off button may actually turn the PI off even if it is frozen, as the switch is actually running off a microcontroller itself (so it doesn't actually care about the readings given off by the PI).

    2. Hi Dylan,

      Great information here! If you don't mind sharing, I'd like to see your modified script that handles the shutdown/restart better. I've ordered both the illuminated and rocker switch from mausberry to test out which is best to use.

      I'm planning on using my Raspberry Pi behind my TV for a media controller (either XBMC or Openelec) so the infrared controller would be useful but I would leave the Raspberry Pi on all the time so unless it locks up the infrared control would only be useful for those times.

      Currently I'm using Raspberry Pi's in a small business as a sort of Thin Client to remote into Virtual Machines. Upon startup I have scripts that check the hostname which helps decide which workstation that Pi connects too. Currently I'm powering these Pi's using a USB y-cable to a USB powered hub. It works but sometimes they freeze. The USB powered hubs have an on/off switch so when they freeze the workers are instructed to turn the power off then on again. I was hoping this wouldn't be a common occurrence as this could corrupt the SD cards. It's happened more than I would like so I'm thinking my problem might be not enough power. I'd like to power these Pi's directly to a power plug but in the event they still freeze it's not easy to reach around and pull the there still is the problem with possible corruption of the SD cards. The workers who use these are not technically inclined so I can't expect them SSH into the frozen Pi and restart through the command line like I would do. Hence the need for a safe shutdown/restart switch or button.

      I'll keep an eye on your blog here Dylan as I'm very interested in the types of switches and scripts you are using on your Raspberry Pi. Thanks again for sharing!

    3. I probably jinxed myself because my headless server pi had its first freeze in months. Not only that, but I had problems getting it to boot up without freezing (I did a scan of the SD card in a laptop and the filesystem was intact). It finally booted up. Maybe the hub's power supply is on the way out? It's a 2amp. I had the pi and the HD plugged in during the initial freeze but subsequent ones trying to boot up, I had the HD unplugged.

      I did take the opportunity to do some testing. This pi has the rocket switch with the running. During the freezes, I was able to ping the device but ssh would not respond (not time out but simply hang trying to initiate a connection). No response from the stuff serving on port 80. The pi was pingable. So it was responding to network packets etc. I figured it would be a good time to test if the power switches would turn the system off in this state. To confirm, it didn't work. I toggled the rocket switch to off and let it sit for 2 minutes. The behaviour of both kinds of switches is that if you toggle them off, they will force try to shutdown the pi, and then wait up to 1 min before terminating the power to the pi. Regardless if the shutdown command worked (it either didn't trigger or hung on it), but the power to the pi remained on. The reset button on the rocket switch worked (as expected).

      I'm going to try play around with a USB panic switch, to see if it can be used in a hard freeze condition to reset the pi.

      I'll upload the scripts this weekend. I updated them again for the ir power switch, and it has been holding out during the tests this week, so they should be "good-to-go".

      Your deployment sounds really interesting. Since these are dumb terminals, I guess you won't be writing to the SD card much? My idea with some XBMC media deployments that I'm going to be pushing out to family's tvs is that I'll be relying very little on the data on the SD card, have it sync with the cloud periodically. This way, I can provide them with "backup SD cards", if they manage to corrupt the SD card. I had a lot of that happening when I accidentally pushed down some overclocking settings that I used on a model B down to a model A -- guess the settings won't work the same way as they were rock solid on the model B but ended up corrupting 2-3 different SD cards within a 24 hour period on the model A. Since dropping all overclocking on model As, I've had no hanging issues.

      On my headless server, I have no overclocking. So it must be related to power supply (its the only one that I supply power using a hub vs direct from tv or power brick).

      BTW, you *might* want to look at the watchdog. I haven't played around with it, but I'm definitely going to have a look at it. When properly working, it is supposed to detect freeze ups and automatically reboot the pi. Worth having a serious look ->

    4. Seems like my sudden onset of freezing is related to the SD card. It's a fairly new Ultra 32GB SanDisk bought from a reputable retailer (Best Buy) in April 2013. I had some more "freeze ups" this weekend. The most recent being after running apt-get and "freezing" when it got to "reading database". I don't do a lot of writing to the card normally, etc, so it is not dying from use. anyways, it is still just a theory at this point; it could be a power supply issue although I think the 2amp hub is not the culprit. I wanted to try booting of the USB hard drive for fun, so I was rsyncing the pi OS off the SD onto the hard drive on a laptop, where I encountered freezing from the mmc process while syncing. Had to eject and reload the card several times before I could get the whole OS copied over. I'm not booting off the HD; will see if I get my "stability" back. I've ran this system off the same power supply in the past (and same SD card) for months at a time without a freezeup before.

  4. Hi Dylan,
    I'm wondering if I have the same problem with lockups on one of my Pi's where the SD Card is the problem. The other 9 workstations haven't reported a problem.

    On another note...I've received my Mausberry Switches. I like the look of the illuminated switch better, but the lack of hard reset switch could be a problem. have reminded me that there is the watchdog timer as well. I had tried setting this up late last year and all it did was continuously reboot my Pi (stuck in a loop). I gave up looking into why at the time. But your reminder has me interested in trying the Watchdog Timer again. This could be the answer to my problem with a possible freezeup with a Pi while someone (non technical) is working with it at the office.

    As for the switches I bought..they do work, but I've noticed something strange. I was hoping to modify the script to do a soft reboot "sudo shutdown -r now" instead of a poweroff. I modified the script and rebooted the Pi. Upon restart I pressed the button. It goes through the shutdown, but it totally turns off instead of restarting. It like the switch is not allowing the power to return for the reboot. Have you noticed this with your switches? Any ideas on how I can modify the script to work better?

    Good luck with your SD card replacement that will hopefully solve your freezing problems.

    1. I'll be installing the Watchdog Timer this weekend in my soft-float Wheezy install on my Pi. I'll keep you posted on how it works for me. you have those mausberry switch script(s) to share? I wouldn't mind trying them out.


    2. No problem, I added it here:

      BTW, I videoed the pi with the rocker-switch and posted it here:

      On the weekend I also picked up a Belkin WeMo power switch. I've been trying it out hooked up to the Pi. Now I can control the Pi using IFTTT remotely from anywhere -- on the web or on my phone, gtalk, or even a simple "email". I can even send it an email with #pi-restart which will trigger the switch to power off and then back on (good hard-reset). I toggle this particular Pi & hub to power on at 2am using IFTTT, perform some work via crontab, shutdown at 7:55am via crontab, and then I poweroff the Pi & hub at 8am using IFTTT.

  5. Hi I’m using another switch, the which can be added to your list.


  6. To power off, you can add a bit of code to enable the remote's power button to turn off the pi...

    Ssh into the pi, then:

    $ sudo nano /etc/lirc/lircrc

    Then add:

    button = KEY_POWER
    prog = irexec
    config = sudo shutdown -h now

    Then save.

    Does a full proper shutdown of all services. :)