Hello friends! I haven't written here in a while but I hope this post gives me the momentum to do more posts. Thanks for being here, I appreciate you 💖! Now let's talk about this quick make – a magic button for the raspberry pi!

The Raspberry Pi is a great platform for making your own DIY devices, especially when you need something small and more compact than a regular portable computer. The thing is, they need to be shutdown properly to help make sure that your filesystem doesn't get corrupted.

I'm preparing a couple of Raspberry Pi's for an upcoming event (more on that later!) and I wanted a super easy way to shut these devices down safely without having to click around or type anything.

So I came up with this tiny USB device – a button that you can click that can pull up the terminal and type out the shutdown command for you! The best part is, the board is off-the-shelf from Adafruit: a Neokey trinkey!

The code for this board takes advantage of the default keyboard shortcut for pulling up the terminal on the default Raspberry Pi OS, which is ctrl + alt + t. I've only tested this on the latest RPi OS (Bookworm) but I believe it works for other Linux versions as well.

What I loved about it was that I could use any mechanical keyboard switch, so it could be as clickety-clackity as I wanted it to be! I settled on one of my fave switches, which happens to be on my dream keyboard I use daily.

The TKC Dragonfruit Switch with a Astrolokeys keycap!

Now I can simply plug this via USB, press the button, et voila! Easy safe shutdown, usable across multiple Raspberry Pi devices. It's like using a car key, but it causes a shut down instead of a start up and has less vroom vroom noises. 🏎️

If you need to make one yourself, here's the code I wrote! I've referenced the original Adafruit tutorials in the gists themselves. If you somehow cannot view the embedded code snippet below, here's a link.

The file boot.py will be run first, and it will prevent the CIRCUITPY drive from appearing as a USB drive unless you're pressing and holding the button while you plug it into the USB port. This makes it so that you can simply plug and remove without having to eject. Then code.py runs, which contains all of the logic to:

  1. Trigger the keyboard shortcut for the terminal: ctrl + alt + t
  2. Waits one second to let the terminal pop up
  3. Type the shutdown command: sudo shutdown -h now

This project was so easy because of these tutorials, check them out for more details, especially if you want to learn more about CircuitPython:

Customizing USB Devices in CircuitPython
You can now enable or hide the USB devices that CircuitPython provides. You can also define new custom HID devices to emulate a digitizer, gamepad, joystick, or other special input device.
Adafruit NeoKey Trinkey
NeoKey Trinkey, the circuit board with a Trinket M0 heart, a NeoPixel glow, and a Cherry MX-compatible key for input.

That's it! I hope this helps someone who needs some magic in their life ✨. If you make one of these, please let me know by tagging me @chardane on Instagram. Subscribe below for more (occasional) creative makes like this.

Also! I'll be exhibiting my art pieces at Open Sauce 2024 in SF, and Teardown 2024 in Portland, OR! If you're going to be at these events as well, please do let me know, I would love to meet you.

Thanks for reading, friend. 🥰