Adding a Status Light and a Power Switch to the RetroPie
A little while back I wrote a post about creating a Raspberry Pi retro gaming machine. It looks pretty cool in the Lego Nintendo enclosure I made for it, but I've been meaning to make it a little more authentic. So I decided to wire up a status light and a power switch.
If you recall, the case looks like this:
Wouldn't it be great if there was an actual status light behind the red transparent bricks? And wouldn't it be great if those faux buttons on the front were actually buttons? So that's my goal here.
Adding the status light is pretty straightforward for anyone who's ever soldered before. In my case, I had not. So I had to do a bunch of research and watch a few videos before I felt confident that I could tackle this. I learned a bunch of things along the way!
I came across this article which talked about various ways to hook up an LED. The path of least resistance is hooking the LED up to the GPIO's serial port pins. It basically has voltage while the pi is running, and doesn't when it has been shut down.
The circuit is pretty basic as far as electronics go. The only thing that I didn't know ahead of time is that a resistor needs to be part of the circuit to prevent both the LED from burning out and damage to the Pi itself. The circuit looks like this:
I followed this calculator to figure out what resistor I needed. Just plug in the numbers: the Pi provides 3V on its GPIO pins, and the LED I found is rated for 1.8V at 5mA. The guide said I needed a 270 ohm resistor. I decided to play it safe and I stepped it up to 330 ohms.
Finally it came down to soldering. When I said I had never soldered before, I was not exactly truthful... in my high school physics class, I had to do some basic electronics, but my soldering was horrible. Turned out I was doing it completely wrong (thanks Mr. Ruttle). This article was extremely helpful and showed me the right way to do it. My first attempt turned out to be pretty good!
And after soldering the second wire, it was ready for a test!
Hmmm... I would get the LED to light up briefly at boot, but then stay off. Back to Google... Turns out, that on some newer Pis, the serial port GPIOs are disabled by default. This article tells you how to enable it. You can also do it through the raspi-config utility.
Now for a second test... Success!!
Finally, to finish it off nicely, I put some shrink wrap tubing over the solder joints and terminated the ends with Dupont connectors.
Here it is tucked away nicely inside the case:
And here's how it looks with everything closed up:
First objective complete!
Now for the power switch. On the original Nintendo console, the switch closest to the status LED was the power switch, and the second was the reset button. For this first attempt, I'm just going to be building the power switch. I need to do a little more research to figure out how to get a reset switch to actually communicate with the various emulators... but once I make one switch, making a second should be a walk in the park.
I wanted the outside of the enclosure to look more or less the same, with the difference being that I can actually press the bricks sticking out and have them do something. So I needed to MacGyver something together. I found this switch at a local electronics shop:
It's base is 6mm x 6mm, about the same size as one of these lego bricks:
So let's see if I can't doctor that brick to work with the switch... First, I cut off the right-angle stud, and drilled the hole out to bi big enough for the switch to poke through:
Then I had to cut half of the back off to make room for the switch:
Here is the switch dry-fitted with the lego brick:
Now to make it permanent!
|This is great stuff.
Took a little fiddling to get it straight, but now I have a micro-switch lego brick! Here it is as part of the case:
|Here's the switch in the front...
|...and from the inside!
You'll notice that I cut a barrel-shaped brick in order to make a little slot for the LED to go, also to create a bit of a shield between the LED and the switch contacts.
Time for a little more soldering to hook it up:
Making it work is part automatic, and part scripting... I've connected it to GPIO 3, which has the ability to wake the Pi from deep sleep (essentially from shut down). So powering up the Pi works without any more effort.
Using the same button to power down the Pi requires a bit of scripting. The steps to do it aren't all that complicated, but this dude has made the process completely automated! I just ran this line:
curl https://pie.8bitjunkie.net/shutdown/setup-shutdown.sh | bash
That's it! I needed to reboot the Pi to get it running, but now I can use the button on the front to shut it down. The nice part about this is that is initiates a safe shutdown instead of just a straight power cut. So no chance of corrupting the SD card in the process.
As it turned out, after I put the lid back on, there wasn't quite enough vertical space inside for the Dupont connectors, so I had to doctor them as well... the top part of the connector has a lot of extra space that can easily be cut off. I was able to shave off about 3 mm from the height to make it fit.
|Might not seem like much, but cutting off that little bit was enough to get the case to close flat.
...and here is the whole shebang in action!
Now to make that second switch and get a reset button working!
UPDATE - 20-Jun-2017
After having this running for a few days, I discovered that sometimes when I pressed the button to run the shut down script, nothing would happen. Sometimes I'd have to press it a couple of times, sometimes I'd have to hold it down a split second longer than what you'd expect.
It turns out that the way the script worked was that it would poll the GPIO pin every 0.5 seconds to see if the button is currently pressed. As you can imagine, this is vastly error prone. I'd have to have the button pressed at the exact moment the script polled the GPIO pin. And given that when you press the button, it's held down for far less than 0.5 seconds. So there was really only a 50/50 chance that the script would recognize the button press.
I turned to Google to find a better way. It turns out there is! This article explains how to use edge detection. I changed my script so that now it waits for a change in state on the GPIO pin (voltage or no voltage) and responds accordingly. Seems to work quite reliably now!