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?

Schematic drawing: a simple switch (single pole single throw) is wired from 5V on one side to digital input pin 2 on an Arduino on its other side.

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:

Schematic drawing: a simple switch (single pole single throw) is wired from 5V to one side to digital input pin 2 on an Arduino on its other side. Additionally, a 10kΩ resistor is wired from pin 2 on the Arduino to ground.

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;
    buttonState = digitalRead(BUTTONPIN);
    // 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:

Image of small momentary tactile pushbutton, with four legs, arranged so that the legs are somewhat flattened out and all pointing to the sides (the left two legs points to the left, and the right two legs to the right). The button is on a white background, and to the right of the button a schematic shows that at all times, the upper left leg is connected to the upper right leg, and and that all times the lower left leg is connected to the lower right leg. The switching action connects the upper and lower legs to each other.

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:

Image of a pushbutton tactile switch installed in a long breadboard. The switch is installed so it is straddling the center dividing valley in the board.

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

Image of a small one-row single-pole double-throw switch on a white surface. Its switch is positioned to the right, and so it is currenctly connecting its center pin to its right pin. A schematic diagram shows that the other possible configuration is that when the switch is to the left, it will connect the center pin to the left switch pin.

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.

Image of a small switch on its side with three legs. The top of the switch is a thin metal lever which hinges on the upper left corner of the switch, and swings at about an angle of 20º above the rest of the switch body. The leftmost leg of the switch is the "common" leg; the center leg is "normally open" (i.e. normally not connected to the common), and the right leg is "normally closed" (i.e. normally connected to the common). The switch has "C", "NO", and "NC" markings in small raised letters above the legs identifying them as such.

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.

Image of a medium-sized, red lever switch on its side. The bottom metal tab of the switch, labeled "COM," is the common lead, and on the right side of the switch there are two connections. The upper right connection is labeled "NC" for normally closed, and the bottom right connection is labeled "NO" for normally open. The schematic drawing illustrating switch's operation as described is visible in the raised plastic of the part.

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