If you’ve seen a laptop or a cellphone that flips open like a clamshell, you’ve probably noticed that when you open and close it, it turns on or off accordingly. But how does it know? If you think there is some kind of switch attached to the hinge that detects this opening and closing movement, you are right!
These devices use a cheap and very reliable sensor called a “Reed Switch” that turns on or off when the magnet is nearby.
Reed switches are used in all kinds of creative applications like door sensors, anemometers (determine the wind speed) etc. They are perfect for any project that requires non-contact control. So let’s take a closer look at what they are and how they work!
Who invented reed switches?
Like many other great inventions, the reed switch was born at Bell Laboratories, invented by Walter B. Ellwood in the mid-1930s. His patent application for an electromagnetic switch was filed on June 27, 1940, and was officially granted on December 2, 1941.
However, the design of his original reed switch is slightly different from the one we use these days.
Reed Switch Overview
A typical reed switch contains a pair of metal reeds made of a ferromagnetic material (something that gets magnetized easily but loses magnetism when it leaves a magnetic field). The surfaces of the reed contacts are plated with hardwearing metals such as rhodium, ruthenium, palladium or iridium to give them a longer life as they switch on and off millions of times.
The reeds are hermetically sealed inside a tubular glass envelope to keep them free of dust and dirt. The hermetic sealing of reed switches makes them suitable for use in explosive environments where small sparks from conventional switches would constitute a hazard. The glass tube is filled with an inert gas, usually nitrogen, or a vacuum to prevent oxidation of the contacts.
Typically, contacts are made of a nickel-iron alloy that is easy to magnetize (has high magnetic permeability) but does not stay that way for long (has low magnetic retentivity). Being a mechanical device, they take some time to respond to changes in the magnetic field – in other words, their switching speed is low (typically 0.6 ms turn-on time, 0.2 ms turn-off time) compared to electronic switches.
In the presence of a magnetic field, both contacts move (instead of just one) and they form a flat, parallel area of contact with each other. This helps to increase the life and reliability of the reed switch.
A reed switch only detects the presence of a magnetic field and does not measure its strength. If you are interested in measuring strength, an analog Hall Effect sensor is a better option to consider.
How Does a Reed Switch Work?
The key to understanding how reed switches work is to realize that they are part of a magnetic circuit, as well as an electrical one – magnetism flows through them as well as electricity.
As you bring a magnet closer to the reed switch, the entire switch becomes a part of the “magnetic circuit” including the magnet (the dotted line in the image shows part of the magnetic field).
The two contacts of a reed switch become opposite magnetic poles, which is why they attract and snap together. It doesn’t matter which end of the magnet you bring closer: the contacts still polarize in opposite ways and attract each other.
When you take the magnet away, the contacts separate and return back to their original position.
A reed switch like this is normally open (NO). This means ‘normally’ when the switch is unaffected by the magnetic field, the switch is open and does not conduct electricity. When a magnet comes close enough to activate the switch, the contacts close and current flows through.
In these illustrations, the movement of the contacts is largely exaggerated. Real reed switches have contacts that are only a few microns apart (about ten times thinner than a human hair). So the movement is not visible to the naked eye. Thanks to Zátonyi Sándor for sharing macroscopic photos of the reed switch.
Wiring up a Reed Switch to an Arduino
The circuit set up for our example is as simple as it can be. First bend both legs of the switch to point perpendicularly away from the body of the switch, so that they form a “U” shape.
Insert the reed switch into the breadboard. Then use jumper wires to connect one end of the switch to ground and the other end to the D2 pin of the Arduino.
If you connect the switch this way, you will need to activate the arduino’s ‘built-in’ pull-up resistor for the input pin. Otherwise, you must use an external 10K pull-up resistor in your circuit.
Here’s an example circuit:
Caution:
The glass envelope that encloses the reed switch breaks easily if twisted, so use caution when bending the legs.
Arduino Code – Reading a Reed Switch
Here is a very basic Arduino sketch based on the circuit above, that will switch-on the built-in LED (attached to pin 13) when you bring a magnet close to the switch, and switch-off when you move it away.
const int REED_PIN = 2; // Pin connected to reed switch
const int LED_PIN = 13; // LED pin
void setup() {
Serial.begin(9600);
pinMode(REED_PIN, INPUT_PULLUP); // Enable internal pull-up for the reed switch
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int proximity = digitalRead(REED_PIN); // Read the state of the switch
// If the pin reads low, the switch is closed.
if (proximity == LOW) {
Serial.println("Switch closed");
digitalWrite(LED_PIN, HIGH); // Turn the LED on
}
else {
Serial.println("Switch opened");
digitalWrite(LED_PIN, LOW); // Turn the LED off
}
}
With the sketch uploaded, grab your magnet, and bring it closer to the switch. It should trigger when the magnet reaches a distance of 1 cm from the body of the reed switch. Try mapping the entire activation region of the reed switch. See how far you can take the magnet!
Code Explanation:
The code is quite self-explanatory. Initially two constants are defined which declare the Arduino pins to which the reed switch and built-in LED are connected.
const int REED_PIN = 2;
const int LED_PIN = 13;
In the setup()
, the reed switch pin is configured as an input while the LED pin is configured as an output. Also the internal pull-up is enabled for the reed switch pin.
void setup() {
Serial.begin(9600);
pinMode(REED_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
}
In the loop()
, the built-in LED is turned ON if the reed switch pin reads LOW, otherwise turned OFF.
void loop() {
int proximity = digitalRead(REED_PIN);
if (proximity == LOW) {
Serial.println("Switch closed");
digitalWrite(LED_PIN, HIGH);
}
else {
Serial.println("Switch opened");
digitalWrite(LED_PIN, LOW);
}
}