Skip to main content

    How to build something ‘useful’ with a Raspberry Pi

    How to build something ‘useful’ with a Raspberry Pi

    Share this story

    If you buy something from a Verge link, Vox Media may earn a commission. See our ethics statement.

    In honor of Pi Day, Chaim Gartenberg and I cooked up a tiny little Raspberry Pi project for yesterday’s episode of Circuit Breaker Live.

    We started with a simple concept: a button that says “Why?” when you press it, in honor of our favorite podcast. So we knew we’d need a button, some sound files, a little bit of Python code, and, of course, a Raspberry Pi.

    A new Pi is $35, but we found an old Raspberry Pi 2 in my desk drawer, which was up to the task. (Newer Pis have built-in Wi-Fi and faster processors, but for our simple button project we didn’t need internet or extra horsepower.)

    Chaim put a fresh copy of the Raspbian Linux distro on a microSD card and we booted up the Pi. Our Circuit Breaker Live producer, Creighton DeSimone, recorded some of our colleagues saying “Why” in various languages and handed us a folder of MP3s. Then we bought a Massive Arcade Button on Amazon for $13.

    Now the “challenge” begins: programming the Pi to play random MP3 files from a folder when the button is pressed. (I put “challenge” in quotes because our approach to programming anything in Python is liberal copy and pasting from Stack Overflow and various tutorials we find on the internet.)

    Liberal copy and pasting from Stack Overflow

    Because we had MP3 files instead of WAV files, we had to use the pygame library’s “music” function instead of its “sound” playback function. Our other hurdle was to choose a good method for reading the button press. There are multiple ways to do this with the Raspberry Pi’s GPIO Python library, but we settled on the “add_event_detect” method, which can take a callback as an argument.

    The ultimate result is that when GPIO detects a button press, our custom play function is called, which, in turn, loads and plays a random MP3 from our folder of recordings.

    Here’s the actual code we ended up with:

    And that’s it! Chaim wired up the button to GPIO 4 and ground and hooked up the button’s LED light to power and ground. If this electricity stuff is confusing to you, I recommend the official guide on the subject and this handy interactive GPIO chart.

    We click run in the Python IDE and our program begins. Slam the button and a sound plays. The Pi can send audio over HDMI or through its aux jack. Everybody congratulates us on being so smart. It’s a great life, being a hardware .hacker

    If you’re intrigued by the Pi life, make sure to check out some of the other Pi-based things Chaim and I have built, such as Chaim’s DIY Amazon Echo and my wearable computer.

    Also, make sure to check out the rest of this week’s episode of Circuit Breaker Live. It includes a call-in segment called “Emotional Tech Support,” which I enjoyed immensely.