I’ve told you that if you want to build a small embedded hardware project, automate things in your household, or just purchase a cheap computer for its possibilities, you should turn to the Raspberry Pi. I’ve also shown you some of the different models.
A while ago, when I wanted to connect my first arcade button (it is massive), I had no idea where to begin.
Every tutorial on the Internet shows the process with a breadboard. I had a million questions:
- Do we need to use a breadboard anyway?
- Why is there even a resistor for an input?
- Which pins can actually be used for Input/Output?
- Can someone help me figure out what wires go to what lugs on the microswitch?
It’s okay if you didn’t understand some of those questions. Why? Because you’ll have those questions after buying your first piece of hardware anyway.
After you’ve soldered a General Purpose Input Output, or GPIO male (preferred) header in (or not soldered a header in) if required, you may notice that there are no fancy diagrams telling you what pins can be used for ground, for example.
This is the part where I contradict myself. pinout.xyz provides that diagram, plus an interactive pin “browser”, so to speak.
Screenshot of pinout.xyz.
Let’s understand this. There are three main types of numberings for the actual IO pins.
- BCM (Broadcom SOC channel)
- The location of the pin on the header, indicated by the numbers next to the pin slots on the diagram.
- Used by RPi.GPIO, upon request.
So, any given pin is either for supplying or diverting power to/from the Raspberry Pi, configurable as an input or output for any kind of devices, should be reserved for a specific kind of device interface, or can be used for ground. Let’s look at a practical example instead of these confusing words, shall we?
Connecting a Device
To do this, you should be using jumper leads in your header. To provide the strongest connection, use a male header and female jumpers. The other end can go to whatever you want.
If you’re just digesting this information, you may ask why I’m not using a breadboard (a plastic board with goodies such as a ground rail, typically used for prototyping). The answer is, it’s not compact enough for me, and for most small scale projects a full-sized breadboard is overkill.
Mind you, on a breadboard you can hook up a lot more electronics (because of the rails).
If you’ve never touched electronics before, think of it like this: one side for input or output, and another for ground (to discharge unused voltage).
Button Microswitch Time!
This is a microswitch.
This one is two-legged, because there’s no LED. Source: Adafruit.
This is another microswitch.
It’s got a built-in LED with a resistor, four legs, and a whole heck of confusion. Source: Adafruit.
They need to be attached to the button somehow, and if that means screwing it in tight, don’t be shy!
If you actually read the image captions, you would know that the only reason the second button (100mm)’s microswitch looks so much more complex is because this one has a built in LED.
It’s actually pretty straightforward to get these things hooked up, as long as you know which side is which, that is.
Identifying Switch Legs
Sometimes it’s just trial and error, and other times there are hints and indicators written in the plastic. If you look closely at this image, you’ll see that one set of microswitch legs are offset from each other.
That’s for the button. How do I know? Look closely at the diagram engraved near the pins and you’ll see something that looks like a push switch.
How do I really know?
The jig is up.
Source: Adafruit Learning System.
It makes sense, doesn’t it? Here’s another example of when documentation can save your life:
Source: Adafruit Learning System.
This diagram applies to the four-legged button above. However, common sense can still apply: the one towards the ground goes to ground.
As for the LED polarity (negative and positive), if it’s not too obvious, you can just swap the wires around until it lights with your code.
A Note on Resistors
To avoid damage to your Pi or LED, you’ll need at least a 330-ohm resistor wired up (you can just twist the wires) on the positive (longer end) of the LED to make sure nothing explodes. Or just doesn’t work.
From GPIO Pins to Wires
If you really want this:
The Pi side is plug-and-play…
…but to get a button hooked up, you can:
- Solder a stripped metal connector terminal to each leg.
- As Mike Stone (2017) of Adafruit Support said to me: “…pull the plastic housing off the female terminal at one end of the wire, flatten the female terminal with a pair of pliers, then solder the flattened part to the switch lug…”
- If the button is small enough (unlike the one pictured): take advantage of quick connects.
Simply solder bare copper wire to the legs.
Source: Adafruit Learning System
gpiozeroon Raspbian with
apt-get install python-gpiozero python3-gpiozero, connect the positive end of the LED (if there is one) and button to BCM 21 and BCM 13 respectively (or just change the values in the script). Use the adjacent ground pins to make wiring convenient.
Run this Python script:
from gpiozero import LED, Button led = LED(21) button = Button(13) button.when_pressed = led.on button.when_released = led.off pause()
The button will light when pressed, and you can replace
led.on with any custom function.
Stay tuned everyone!
Stone, M. (2017, December 3). Re: Quick Connects + Massive Arcade button = Works with Rpi [Forum post]. Retrieved from https://forums.adafruit.com/viewtopic.php?f=50&t=127392#p635313.
All uncredited images were created exclusively for this post by me. All screenshots of pinout.xyz taken by me.
Arcade Button - 30mm Translucent Red [Digital image]. (n.d.). Retrieved from https://www.adafruit.com/product/473
Massive Arcade Button with LED - 100mm Red [Digital image]. (n.d.). Retrieved from https://www.adafruit.com/product/1185
Mini LED Arcade Button - 24mm Green [Digital image]. (n.d.). Retrieved from https://www.adafruit.com/product/3433
3d_printing_arcade-button-pinout.jpg [Digital image]. (n.d.). Retrieved from https://learn.adafruit.com/assets/42961
gaming_press-your-button-wiring-diagram.png [Digital image]. (n.d.). Retrieved from https://learn.adafruit.com/assets/28806
3d_printing_button-pwr-solder.gif [Digital image]. (n.d.). Retrieved from https://learn.adafruit.com/assets/42969