Raspberry Pi safe shutdown button

What is a Raspberry Pi

A Raspberry Pi is a small computer that runs everything from an SD memory card. It has input and output pins that can be configured in software. It is designed as a cheap computer (around $40) to run things and act on input, like special embedded systems. The programming environment for it is easy to learn, yet powerful, offering many possibilities.
A Pi is not really designed to be used as a home computer, or a system that you start up and shutdown when you need it, but mostly as a system to do one specific task, and start once and let it running..
Shutting it down by suddenly removing the power can cause problems: the SD memory card can become corrupted because of this. A safe shutdown requires you to go into the desktop with the mouse and select shutdown (and thus requires you to have a screen and mouse connectedà.

Using a Pi in arcades or pinball machines

Pi computers are, because of their low price and versatility, popular for small hardware projects.
RetroPi is one of these popular projects - it allows you to play thousands of original arcade games. You can install it in a large arcade cabinet or make a small arcade cabinet yourself.

In pinball machines the Pi is useful for people who create their own homebrew games, or who adapt the rules of existing machines using a P-Roc board. A P-Roc board replaces the original pinball cpu with a board that you can send instructions from a computer.
Cactus Canyon Continued is one of the more popular projects using P-Roc.

I've installed a P-Roc with the CC Continued software in my Cactus Canyon pinball machine. Instead of using a computer, it's all installed on a Raspberry Pi 3B.

CCC already exists over 7 years now, most documentation you'll find about installing it uses a small computer setup. That was the only way, as older Raspberry Pi versions aren't powerful enough to run it, especially if you want to add a color monitor too. Only from the Pi 3B it is possible to run all the software.
Running it from a Pi has the advantage that it's cheap.

This article is not about installing the software in the Pi (I didn't do this myself, a friend did it for me) but about configuring a safe hardware shutdown button.

Pi safe shutdown button

If you install a Pi as a retropi or in a pinball machine, you don't want to keep the pinball machine or arcade cabinet runningon 24/24, there needs to be a way to shut it down without risking a corrupt card. But you don't have a full desktop screen or a mouse or keyboard available.

There is a hardware solution for this - dedicated boards are for sale that send a shutdown signal when they notice the power goes down. Problem is that they're sometimes more expensive than what the Pi itself cost. So it defeats the principle of using a Pi because its cheap..

As the Pi has input pins you can configure, a logical solution is to connect a physical button that initiates a safe shutdown. If you search around there are solutions that run a python script in the background, which will check like every second if a certain input has happened and then initiate the shutdown. Nice idea, but this is making it overly complicated - it's another script that has to run and takes processing power away.
Why do this when a safe shutdown is built in the software itself ?

The magic trick to do the shutdown is, once your Pi is started, to edit the config.txt file and add a line:

To do this - in the desktop, open a command window, and type: sudo nano /boot/config.txt
This will open a text editor, use the arrow keys to go to the bottom of the file, add the line and save the file.
Once you're done, shutdown or reboot your Pi again, and from then on the Pi will listen to input on gpio port 3. When it gets a signal there, the Pi will automatically shutdown.

Connecting the switch is easy if you have some experience with arcade or pinball machines. Just take a push button (I had a spare arcade button laying around), solder wires to it that are long enough (and insulate them with heat shrink tubing or electrical tape).

To connect the wires to the board - you need female Dupont style connectors. That's how these connectors are called, they fit over 1 pin of the rpi connector.

Connect the switch to pins 5 and 6 of the Pi and you're set. Note how the pins are numbered on a Pi board, they are not sequential (so don't start counting on one side and continue). Pin 5 and 6 are the 3rd row - see image.

With this setup I now have a hardwired safe shutdown switch for the Pi. When I'm done playing, I open the coindoor, push the button, the Pi shuts down within a few seconds, and then I can turn off the pinball machine itself.

Raspberry Pi shutdown button
Pin 5 and 6 connected to the pushbutton, using Dupont wires.

Raspberry Pi shutdown button
This is how it's all set up inside the pinball machine. Power comes from the metal box that holds the on/off switch. The Raspberry Pi uses a pc speaker to output the sounds, and is connected with an usb cable to the P-Roc board in the backbox.
Now it's tested and everything works, the wiring can be cleaned up and fixed more permanently.