Mechanical switches allow the flow of current to be turned on and off by flipping a toggle from one position to another or by pressing a button. They are so simple that they require little in the way of explanation other than to describe the various types of switches available and to highlight their limitations.
Relays long predate transistors as a means of using a small current to switch to a much bigger one. However, their flexibility and separation of the control side from the switching side mean that they are still in use today.
Switches generally work by mechanically bringing together two metal contacts. Figure 6-1 shows how this might work.
When the push button is pressed, it presses the sprung metal contact at the top to the fixed contact in the base of the switch.
There are several things that can make switching less straightforward than you might expect:
For these reasons switches have a maximum voltage and current, and there are often separate maximums for switching AC and DC.
For a discussion of the various types of switch, see Recipe 6.2.
Figure 6-2 shows a selection of switches.
The switches from left to right are:
If you are designing a project, then you are probably most likely to be using a microcontroller of some sort, in which case a simple push switch is all that is required. Even if you have a situation where you might want to use a switch to select between two options, chances are you would use a pair of push switches and some feedback as to which is selected in a display.
Slide and toggle switches have mostly been relegated to the role of switching power on and off to the project, although you will occasionally find them on circuit boards as “selector” switches.
Toggles (and other switches) are available in many different switching configurations. Some of those described include DPDT, SPDT, SPST, and SPST. These letters stand for:
A DPDT switch is double pole, double throw. The word pole refers to the number of separate switch elements that are controlled from the one mechanical lever. So, a double-pole switch can switch two things on and off together. A single-throw switch can only open or close a contact (or two contacts if it is a double pole). However, a double-throw switch can make the common contact be connected to one of two other contacts. Figure 6-3 shows some of these configurations.
As seen in Figure 6-3 when drawing a schematic with a double-pole switch, it is common to draw the switch as two switches (S1a and S1b) and connect them with a dotted line to show that they are linked mechanically.
The matter is further complicated because you can have three poles or more on a switch, and double-throw switches are sometimes sprung, and do not stay in one or both of these positions. They may also have a center-off position where the common contact is not connected to any other contact.
Another kind of switch that you may encounter is the rotary switch. This has a knob that can be set to a number of positions. If you have a multimeter, then this is the kind of switch that you will use to select the range. Such switches are not often used. Rotary encoders coupled with a microcontroller (Recipe 12.2) are the more modern way of making a selection using a control knob that you turn.
To use switches with an Arduino or Raspberry Pi, see Recipe 12.1.
A reed switch is made up of a pair of contacts running parallel to each other that are close but not touching. The contacts are enclosed in a glass capsule and when a magnet is brought close to the contacts they are drawn together, closing the switch.
Figure 6-4 shows a reed switch with and without a magnet next to it.
Reed switches are often found in intruder alarms on doors and windows. A fixed magnet is attached to the door and the reed switch to the door frame. When the door is opened, the magnet moves away from the reed switch, triggering the alarm.
Reed switches are also found in reed relays but are rarely used these days.
An electromechanical relay has two parts, a coil that acts as an electromagnet and switch contacts that close when the coil is energized. Figure 6-5 shows a relay, along with the pinout of the relay and a photo of an actual relay.
Although somewhat old-fashioned, relays are still in use today. They are equally happy switching AC or DC and are easily interfaced to a microcontroller.
If you take a relay apart, you will probably see something like Figure 6-6.
Here you can clearly see the coil that forms the electromagnet that when energized will move the contacts seen at the top.
The electromagnet of a relay is a big coil of wire and when released will produce a voltage spike that should be snubbed using a diode (see Recipe 11.9).
See Recipe 11.9 for information on using a relay with an Arduino or Raspberry Pi.
Today SSRs are often used in place of electromechanical relays (see Recipe 11.10).