Reading buttons and switches
Switches and buttons are easy to wire up once you understand a few basics:
- For reliable readings, some electrical input must always be attached to the input pin
- Buttons and switches may have non-obvious internal wiring!
- On an Arduino Uno, 5 volts means
HIGH
and 0 volts (ground) meansLOW
An easy mistake
Many beginners often build a circuit like the one below. Makes sense. When the button is pushed, the input pin sees 5V, and when it’s not pushed, it will see 0V so it will be grounded (because nothing is plugged in to it). Right?
Nope. As point #1 says above, for good readings you must wire some electrical input to an input pin at all times. When the button on the circuit above is not pushed, pin 2
is connected to nothing but a bit of wire. It will not reliably read 0 volts, because of the way digital inputs are read on the Arduino.
The solution? Add a “pull-down resistor” to make it so that when the button is not being pushed, the input pin will still be connected to ground, as shown here:
Why is called “pull-down?” Because when the input would have been in an ambiguous state (because it wasn’t connected to anything with a known voltage), the resistor “pulls” it down to ground. Typically, these are high value (high-resistance) resistors; 10kΩ is a standard choice.
Switch types
We have a variety of switches available in the Physical Computing Lab, and there is a truly huge range of them out in the world. (For instance, as of this writing, Mouser sells 24,602 types of toggle switches, and Digi-Key sells 25,378 types of pushbuttons.)
Here, we review the switches included in your course kit as well as some other common types.
momentary pushbutton (“tactile” buttons)
This four-legged little button makes a nice “click” sound when you push it. It does not have the internal wiring you may expect. Here’s the button next to a schematic elucidating the wiring:
Note that the upper right and upper left legs are always connected to each other, and the lower left and lower right legs are also always connected to each other. The action of the button is to connect the upper part of the switch to the lower part of the switch.
The button is designed with spacing specificically meant to fit across the center dividing valley in a standard breadboard, like so:
Keep the switch’s surprising internal wiring in mind when using these on a breadboard—the top row of the switch will act as though the electrical row goes across the breadboard’s valley (since the inside of the switch will bridge that gap), and likewise with the bottom.
single-pole double throw switch
This type of simple switch is referred to as “single pole double throw” because there is only one “pole” that the switch operates (the center pin in this case), and that single pole has two possible connections (“throws”) it can make. It is a “maintained” switch, as opposed to “momentary,” meaning that it will remain in the last position it was pushed into (it has no internal spring unlike a momentary switch).
The center pin will either always connect to the left pin, or to the right pin. The left and right pin will never connect to each other, unless there’s a mechanical fault inside the switch. (Which is possible!)
micro limit switch, or micro lever switch
These small switches can be used as “limit” switches, meaning they detect when something moving in a straight line is brought into physical contact with something else in a fixed position. This position would be defined as the “travel limit” of a physical part, and it is a common operation in electronically controlled motion systems to move something along an axis until it contacts a limit switch at the end.
The switches are called “lever switches” because of the way they’re built—that arm sticking out of the switch is a small lever, and the switches come in a variety of lever length shapes and sizes to accommodate different design needs.
If you look very closely, you can see the switch’s markings in raised letters on the plastic body; the legs are marked from left to right, C
, NO
, and NC
, which mean “common,” “normally open,” and “normally closed” respectively. (“Normal” refers to when the button/lever is not being pushed.)
mini lever switch
This switch is the slightly-bigger cousin of the micro lever switch. It is more robust mechanically, and designed to fit into standardized tabbed holders in various kinds of machinery. It has a roller at the end of its arm, which means that it could be used, for instance, as a cam follower, or some other application where the motion of the thing contacting the switch wouldn’t necessarily always be perpendicular to it.
Note that in the case of this switch, there was enough space on the plastic housing that the manufacturer was able to draw a small, albeit legible schematic of the switch.