Switches
How switches work mechanically and electrically, switch types, and the contact bounce problem.
What a Switch Does
A switch controls current flow by making or breaking a connection in a circuit. When closed, current flows. When open, it doesn’t. That’s it — mechanically simple, but there’s more nuance than I expected.
Switch Terminology
Poles and Throws:
- Pole — the number of separate circuits the switch controls simultaneously
- Throw — the number of positions each pole can connect to
| Type | Meaning |
|---|---|
| SPST | Single Pole Single Throw — simplest on/off switch |
| SPDT | Single Pole Double Throw — one input, two outputs (use one or the other) |
| DPST | Double Pole Single Throw — two separate circuits, same on/off control |
| DPDT | Double Pole Double Throw — two circuits, each with two throw positions |
An SPDT is useful for switching between two sources, or for reversing motor direction.
Normally Open vs Normally Closed
- NO (Normally Open): default state = open (no current). Switch press = closes circuit.
- NC (Normally Closed): default state = closed (current flows). Switch press = opens circuit.
Matters a lot for relays and safety circuits. A fail-safe circuit often uses NC — if the switch mechanism fails, the safe state is preserved.
Switch Types
Toggle switch — mechanical lever, stays in position. Good for power on/off.
Slide switch — slides between positions. Common in small electronics.
Push button (momentary) — only conducts while held. Returns to default on release. Keyboard keys, game controllers.
Push button (latching) — click once to lock on, click again to release.
Rotary switch — one pole, many throw positions. Volume knobs on older gear.
DIP switch — row of tiny switches in a package, used for configuration.
Contact Bounce
This surprised me. When a mechanical switch closes, the metal contacts physically bounce against each other for a few milliseconds before settling. During this bounce period, the circuit rapidly opens and closes dozens of times.
For human use this is invisible. For a microcontroller reading at MHz speeds, it looks like many rapid button presses.
The bounce problem in software:
// Naive: reads every bounce as a separate press
if (digitalRead(BUTTON) == LOW) {
count++;
}
// Debounced: ignore changes for 50ms after first detection
unsigned long lastPress = 0;
if (digitalRead(BUTTON) == LOW && millis() - lastPress > 50) {
count++;
lastPress = millis();
}
Hardware debounce: an RC circuit (resistor + capacitor) smooths out the bounces before the signal reaches the microcontroller.
Pull-up and Pull-down Resistors
A floating input pin is a problem — it picks up noise and reads unpredictably. Switches need a defined voltage when open.
Pull-up: connects the pin to Vcc through a resistor. Open switch = HIGH. Closed switch = LOW (pulled to GND).
Pull-down: connects the pin to GND through a resistor. Open switch = LOW. Closed switch = HIGH.
Most microcontrollers have internal pull-ups you can enable in software, so you often don’t need an external resistor.
What I Built
Wired an SPDT switch to redirect current between two LEDs — one red, one green. Flipping the switch kills one and lights the other. Clean demonstration of how a single pole can route to two different paths.