Multipurpose Raspberry Pi Switch Box
by voidf1sh in Circuits > Raspberry Pi
696 Views, 4 Favorites, 0 Comments
Multipurpose Raspberry Pi Switch Box
This Instructable will show you how to build a Raspberry Pi-powered switch box. This box can be used in many ways for many things. I am building my box to use as an input device to control Microsoft Flight Simulator 2020. This Instructable does not go into the programming to add functionality to this box. This guide will have instructions on building the circuit, building the box, connecting to the Raspberry Pi, and some basic code to interact with the switches and buttons.
Supplies
I'll include links to what I'm using or a general recommendation, however this project is highly customizable and I encourage you to explore and find hardware that suits your needs.
1 Raspberry Pi (model doesn't matter, just make sure to double check your Pi's pinout and adjust accordingly. Please don't just blindly follow this guide as your Pi may have a slightly different pinout)
1 Project Enclosure (size depending on how many switches/buttons you need)
0-25 Toggle Switches, Buttons, etc. (most Pi's have 25 usable GPIO pins, so 25 switches and buttons is the limit without adding a multiplexer, which isn't in the scope of this guide)
Spool of 22-24awg wire (getting a black and red pair is very useful)
Screwdriver for taking apart and putting together the project enclosure
Useful depending on your setup:
- Solder, soldering iron, helping hands (if you plan to solder all/some connections)
- DuPont connector kit (this is for making female pin header connectors for connecting to GPIO pins on a Raspberry Pi B+ model, or a Zero with header pins)
- Spade terminal connectors (makes it much easier to connect wires to the switches and buttons, size according to what your project needs)
- Heat shrink tubing
Plan the Physical Project
You can do this in any manner of ways, but plan out how you want the box to be laid out and what size it should be. Take into consideration future upgradability: I got a larger box than I needed because I intend to add much more to this box in the future. Take a look at what switches you want to use and what size they are. You can then use this to determine what size box you need. You should only have to worry about the size of the top surface of the box, the box depth shouldn't matter much as long as its deep enough to hold a Pi and the switches.
Browse Amazon, Mouser, Digikey, and other electronics retailers to find whatever hardware you need. With some research on how pigpio works you could even add more complicated things like I2C screens, potentiometers, and rotary encoders.
You can plan out the project on paper or in CAD software like Fusion 360. I made a model of my project in Fusion 360 using 3D models available from McMaster-Carr. Before starting this project I had never used Fusion 360 and was able to create this model within a couple hours. Fusion 360 can be used for free for hobbyists and students.
Once you've planned out your project, purchase the supplies necessary.
Prepare the Enclosure
Create holes necessary for the switches and buttons. Measure the top of the box and drill holes at even intervals for the switches. Bear in mind the back of the switch is often larger than the front, so take into account the width of the body of the switch.
After drilling the holes, use a razor blade to remove any excess material and burrs to create a smooth finish.
Create Connections to the Switches and Buttons
- Using a soldering iron or spade connectors, create connections to the switches. Connect one side of the switches and buttons together to create a common Vcc to provide power to the switches.
- Add individual wires to the other side of the switches and buttons.
- Add DuPont connectors to the other end if connecting to pin headers.
Connect to the Pi
Connect the switches to the Raspberry Pi according to the circuit diagram. Please compare my diagram to the diagram for your Pi and adjust accordingly. I am not responsible for damaged hardware!
Use the DuPont connectors created in the previous step, or solder directly to the Raspberry Pi if yours doesn't have headers.
Connect a wire to 5V/Vcc on the Raspberry Pi to a 1K ohm or similar resistor, then connect this to the common Vcc side of the switches.
Set Up the Software
First, you must set up your Raspberry Pi. I am using Raspberry Pi OS Lite to get the best performance out of a headless setup. I use a Pi 2 B+ with a USB WiFi adapter. Setting up for a headless device was quite easy following these instructions.
There are many ways to interact with the GPIO on the Raspberry Pi. Since I have experience with Node.js and the other part of my project will also be in Node.js, I've decided to use Node and the pigpio npm package, which is a wrapper for the pigpio C library. This will allow us to easily interact with the GPIO and execute commands based on it.
After setting up SSH, install Node.js. If you wish to, you can clone my example repository here and modify the code to suit your needs. Make sure to install pigpio using npm.