Shutdown button for my Pi
May 18, 2014
It has been around a year now since my first Pi arrived and that first Pi has always been used as a media centre for my trailer and at home.
The problem being…
I use the remote app on my phone and iPad as the primary control there is no keyboard attached to the Pi for input. On the rare occasion there is a problem where either XBMC hangs up or the Pi drops from wifi I have no way to power it off other than unplugging it.
At home when it is just the wifi problem no biggie, the physical remote over HDMI works but at my trailer I have an older TV and this doesn’t work.
Anyone who has used a Pi knows you do an unclean shutdown enough times and you have to re image the OS because the file system damage is not always repairable. With lower quality cards this even sometimes leads to the SD card becoming unbootable permanently but I have only had that happen with one particular brand.
The solution
So I have some buttons left over from my original Arcade project and I decided to solve the problem by adding a shutdown button to my Pi. This way even if XBMC locks the physical button can execute a shutdown. It would take a full hardware lock of the Pi to cause a forced power off again and the only time I have seen that occur is when over clocking to 1Ghz so this should in almost all cases solve the problem!
Hardware
In this particular case I am using a red sanwa style arcade button for the shutdown, red seemed appropriate I also had blue and white available. I got this button from Adafruit last year along with the easy connector cable that will be used to connect to the GPIO on the Pi.
I connected the cable to the last pair of pins on the GPIO board as it was the easiest place to place the jumper and manipulate the cable in a manner to fit through the GPIO slot on my Pibow case.
I tried to keep as much of the cable within the case as possible, this is meant to be functional not pretty. Maybe I’ll get a smaller button in the future but this is what I had on hand.
Software
I’m using retrogame to handle the GPIO input detection and also the shutdown activation. Recent versions of retrogame seem to have taken this out but I forked an earlier version that has what I want for my projects.
For the Vulcan nerve pinch code to work with just a single button I just modified the bitmask to look for the same button instead of 2: (1L << 5) | (1L << 5)
Results
It works! hold the button for about 2 seconds and the Pi shuts down. This hopefully will extend the life of my OS and the SD cards.
Facebook Comments
Categorized as: What's up
Does retrogame start up automatically in the background?
You have to add it to rc.local for it to be started automatically
Hmm it seems like your site ate my first comment (it was extremely long)
so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog.
I as well am an aspiring blog writer but I’m still new to
everything. Do you have any tips and hints for novice blog writers?
I’d certainly appreciate it.
Write what you love
it would be great to have a few more details here for newbies. how this the switch wired to the GPIO? how is game software installed and modified. thanks in advance.
Sure, I’ll write up a follow up details post
there you go: http://www.philtopia.com/?p=1823
would this work for an in car mounted airplay server?
I want the pi to shutdown when the power is switched off.
Hmm not sure how you’d do that to be honest. You might need to get fancy with electronics and capacitors or something.
I was thinking about putting a USB battery between my Pi and the car’s USB port so that I’d have time to press my shut down button (that would be an easy way) I suppose a more complex way would be to have the Pi somehow detect it is on battery power and automatically shutdown somehow? but without research I don’t have an accurate answer on that front.
I’ve been researching this heavily today. This is what you want for automotive use: https://mausberry-circuits.myshopify.com/collections/car-power-supply-switches
The description on the website isn’t very helpful, what does that do? Does it draw of battery until you actually shut if off? if so that’s cool.
[…] time ago I made a post about adding a shut down button to my Raspberry Pi which I then followed up with how to set it up to also be the power on […]