Switches and buttons are easy to wire up once you understand a few basics:

1. For reliable readings, some electrical input must always be attached to the input pin
2. Buttons and switches may have non-obvious internal wiring!
3. On an Arduino Uno, 5 volts means `HIGH` and 0 volts (ground) means `LOW`

# 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.

# Software

The command `digitalRead()` is used to detect a voltage at-or-near-zero, or at-or-near-five1, on any pin. The function will return either `HIGH` or `LOW` as per the documentation. For instance, to read the value of the button in the schematic above, you could run:

``````const int BUTTONPIN = 2;

void setup(){
pinMode(BUTTONPIN, INPUT);
}

void loop(){
int buttonState;
// now buttonState will be either HIGH or LOW, depending on the voltage present at pin 2.
}
``````

The values `HIGH` and `LOW` can be used for the purpose of comparison. For instance:

``````if (buttonState == HIGH) {
// some code that runs when the button is pressed
}
``````

# 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.

footnotes:

1. On the Arduino Uno, any voltage between 0V and ~1.5V will be rounded down to `LOW` by `digitalRead()`, and any voltage greater than ~3V and up to 5V will be rounded to `HIGH`