Okay, so I lied last time. In 20/20 hindsight, it would be rude to dump my Gentle Reader into the world of analogue before discussing binary. Which, of course, is what switches are all about. And the majority of potential inputs (and all of the outputs) on an Arduino are binary.
First things first: What is it good for? In the proverbial nutshell, a switch controls whether or not current flows through a circuit. In electronics parlance, it's HIGH or LOW. No nuance -- the current is either ON or OFF and we (mostly) don't care about how much. For the purposes of micro-controller units (MCUs), I'm going to talk about switches as sensors. All the MCU needs to know is "Is current flowing to this pin when I check it?"
Switches can be incredibly fancy -- to the point where it's easy to forget that they're switches under the hood. Eventually, we'll take a deeper dive into widgets like ultrasonic range-finders and DHT (Digital Humidity & Temperature) sensors, which pulse data in the form of micro- or milli-second bursts of ON and OFF. But as an introduction, we're going to look their much simpler, entirely mechanical, cousins.
Example 1: Reed Switch
First things first: What is it good for?
Basically any project where you need to detect a magnetic force within
a small radius. (Depending on the strength and orientation of the
magnet, you can figure 10cm as a maximum.)
This is probably the simplest one in my stash and has a delightful old-school retro-sci-fi vibe. It's also a little more straightforward to understand. For tasters, it's transparent -- literally and figuratively. Basically, it's comprised of two ends of a switch hermetically sealed inside a tiny glass bubble. One of those ends is susceptible to magnetic force.
When an appropriate magnetic force -- in
our case, the pull from a rare-earth magnet -- is applied, the
switch opens or closes, depending on both its design and the polarity of
the magnetic force.
Reed-switches come in two flavours:
- Normally Open -- i.e., by default the two ends do not touch and have to be pushed together by a magnet.
- Normally Closed -- i.e. the ends touch by default and have to be forced apart by a magnet.
Of
the two styles, the Normally Open (abbreviated "NO") version is more useful when you're
trying to conserve battery power. But there are definitely useful cases
for the Normally Closed (NC) style, namely in something known as a "deadman
switch." There, the switch is watching for a drop of current from some
source to indicate that, for instance, a door is open.
Example 2: Momentary Push-button
First things first: What is it good for? Very brief human interaction: "Reset" buttons, console buttons, highly un-ergonomic keyboards.
This form-factor should look pretty familiar; it's the kind of button you smash for video games or hold down and count alligators to reboot your laptop. Depending on how you wire your project, these can be configured as Normally Open or Normally Closed.
Our human model of
this kind switch probably runs something like this: Somewhere in the
middle of the switch's descent from up to down (or back), the switch crosses a
decisive cut-off point between Closed and Open.
Only that the Laws of Physics(TM) plus the realities of mass-manufacturing make that hypothesis more...interesting. Especially when combined with our human reflexes. Compared to micro-controllers like the ones that go into Arduinos, we humans are about as responsive as a tree-sloth on quaaludes (thanks, Dave Berry!).
When the MCU is checking the button's ON/OFF state hundreds or thousands of times per second, what we in Meatspace(TM) perceive as "instantaneous" is a quasi-eternity of quantum ambiguity for the micro-controller.
Mainly because, with these inexpensive buttons, we're not actually making an unambiguous downward/upward movement. That "springiness" we feel in the button translates to a semi-random sequence of ONs and OFFs that rivals the relationship status of celebrity couples by orders of magnitude.
Hardware-wise, you might be able to work around it with higher-quality switches and maybe some current-dampening via a resistor. In this case, you're better off with fixing it at the software level with what's known as "de-bouncing code." At some point, I will do a deep-dive into momentary push-buttons with a de-bouncing code example. Just not today.
Example 3: Latched Push-button
This version of a push-button should also be familiar, except that once the button snaps into the "down" state, it stays there ("latched") until it's pressed again.
First things first: What is it good for? Human interaction, but mainly in a set-it-and-forget-it way.
Again, from the micro-controller's perspective, the transition from "Latched" to "Unlatched" will not seem
so binary. And thus will still require some amount of de-bouncing code
to handle the ambiguities of being smooshed by apes with social media
accounts. The main difference, of course, is that once the switch
"clicks' into place it's set. That removes some -- but not all --
ambiguity. From a software perspective, it's best to treat momentary
and latched push-buttons more or less identically in code.
But this particular model packs one extra feature in that it has six pins vs. the four of the "momentary" style. Which, for all practical purposes, allows you to switch two circuits with the same button. Which, as we'll eventually see, opens up all sorts of possibilities.
Before then, however, I do need to cover the fundamentals of resistors and the basic algebra that goes into pretty much any home-brew circuitry. Until then: Tchein ton siault d'beluets!