If you’ve ever encountered a laptop or a cellphone that opens and closes like a clamshell, you’ve likely observed its automatic power on/off function corresponding to the opening and closing motion. But how does it manage this? If you presume there’s a switch connected to the hinge sensing these movements, you’re correct!
These devices employ an inexpensive and highly dependable sensor known as a “Reed Switch,” which activates or deactivates in the presence of a nearby magnet.
Reed switches find application in various innovative uses such as door sensors, anemometers (to measure wind speed), and more. They’re ideal for projects requiring contactless control. So, let’s delve deeper into their functionality and origins!
Similar to numerous other remarkable innovations, the reed switch originated at Bell Laboratories, credited to Walter B. Ellwood in the mid-1930s. His patent application for an electromagnetic switch was submitted on June 27, 1940, and officially granted on December 2, 1941. However, the original design of his reed switch differs slightly from the modern version we utilize today.
Parts Required
Overview of Reed Switches
A typical reed switch consists of a pair of metal reeds made from a ferromagnetic material, which easily magnetizes but loses magnetism when removed from a magnetic field. To ensure longevity through numerous cycles of activation and deactivation, the surfaces of the reed contacts are coated with durable metals like rhodium, ruthenium, palladium, or iridium.
Contained within a tubular glass casing, the reeds are hermetically sealed to protect them from dust and debris. This airtight seal makes reed switches appropriate for use in environments where even minor sparks from traditional switches could pose a danger. Usually, the glass tube is filled with an inert gas such as nitrogen or maintained in a vacuum to prevent the contacts from oxidizing.
The contacts are typically made of a nickel-iron alloy that is easily magnetized (displaying high magnetic permeability) but loses magnetization rapidly (exhibiting low magnetic retentivity). As mechanical devices, they require some time to respond to changes in the magnetic field, resulting in a relatively slow switching speed (typically 0.6 ms turn-on time, 0.2 ms turn-off time) compared to electronic switches.
When subjected to a magnetic field, both contacts move simultaneously, creating a flat, parallel contact area. This feature enhances the longevity and reliability of the reed switch.
How Does a Reed Switch Work?
Understanding the functionality of reed switches entails recognizing that they function within both a magnetic and electrical circuit—magnetism flows through them alongside electricity.
As a magnet approaches the reed switch, the entire switch becomes integrated into the “magnetic circuit,” encompassing the magnet (as depicted by the dotted line in the image).
The two contacts of a reed switch assume opposing magnetic poles, leading them to attract and swiftly come together. Regardless of which end of the magnet is brought closer, the contacts polarize in opposite directions and draw towards each other.
Upon removing the magnet, the contacts disengage and revert to their initial positions.
Typically, a reed switch like this remains in a normally open (NO) state. This signifies that under normal conditions, when unaffected by a magnetic field, the switch remains open and does not conduct electricity. However, when a magnet approaches to activate the switch, the contacts close, allowing current to flow through.
In the provided illustrations, the movement of the contacts is exaggerated. Actual reed switches feature contacts that are only a few microns apart—approximately ten times thinner than a human hair—making their movement imperceptible to the naked eye. Credit to Zátonyi Sándor for sharing macroscopic photos of the reed switch.
Wiring up a Reed Switch to an Arduino
Setting up the circuit for our demonstration is straightforward. Begin by bending both legs of the switch to create a perpendicular orientation away from the switch’s body, forming a “U” shape.
Insert the reed switch into the breadboard, then utilize jumper wires to establish connections: one end of the switch should link to ground, while the other end connects to pin D2 on the Arduino.
If you opt for this connection method, activating the Arduino’s ‘built-in’ pull-up resistor for the input pin is necessary. Alternatively, an external 10K pull-up resistor can be employed in your circuit.
Here’s an example of the circuit:
Handle the legs of the reed switch delicately, as the glass envelope enclosing the switch is prone to breakage if twisted.
Arduino Code – Reading a Reed Switch
Below is a fundamental Arduino sketch based on the aforementioned circuit. This sketch operates the built-in LED (connected to pin 13), illuminating it when a magnet is brought near the switch and extinguishing it when the magnet is moved 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 } }
Once the sketch is uploaded, retrieve your magnet and bring it closer to the switch. It should trigger when the magnet reaches a distance of 1 cm from the reed switch’s body. Experiment with mapping the entire activation range of the reed switch and observe how far you can move the magnet!
Code Explanation:
The code is fairly straightforward. Initially, two constants are defined to specify 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 set as an output. Additionally, the internal pull-up resistor 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 activated if the reed switch pin reads LOW; otherwise, it is deactivated.
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); } }
Related article
- Track Heart Rate: A Guide to Pulse Sensor and Arduino Integration
- Using TMP36 Temperature Sensor: Arduino Interfacing Guide
- Arduino Water Quality Testing: Utilizing the TDS Sensor
- Interfacing Arduino with TCS230/TCS3200 Color Recognition Sensor
- Arduino BMP388 Barometric Pressure Sensor Integration Guide