Magnetic Intelligence: The Arduino Nano Reed Switch Manual

The Reed Switch module (commonly the KY-021) is a definitive tool for non-contact proximity sensing. For the Arduino Nano, this sensor acts as a silent digital trigger. By utilizing the influence of a magnetic field, it allows a compact system to detect the opening of doors, the rotation of wheels, or the presence of an object without any mechanical wear or physical contact.

How it Works: Ferromagnetic Contacts

A reed switch consists of two thin, flexible metal blades (reeds) made of ferromagnetic material, enclosed in a hermetically sealed glass envelope. In its default state, the blades are separated. When a Magnet is brought close to the switch, the blades become polarized and attract each other, closing the circuit. Because the contacts are sealed in an inert gas, they are immune to corrosion, dust, and moisture.

Wiring the Reed Module to Arduino Nano

The Reed Switch module typically features three pins: Signal (S), VCC (+), and Ground (-). On the Arduino Nano, the Signal pin provides a clear digital HIGH or LOW depending on the magnetic presence. For the most efficient design, the Nano's internal pull-up resistors can be used to simplify the wiring, ensuring the signal is stable when the magnet is away.

Module PinFunctionArduino Nano Pin
S (Signal)Digital Switch OutputDigital Pin 2
+ (VCC)Power Supply (3.3V - 5V)5V
- (GND)GroundGND

Programming: Detecting Magnetic Events

The Arduino Nano monitors the digital state of the reed switch. The following code utilizes the INPUT_PULLUP mode, which causes the pin to read LOW when a magnet is present (closing the switch to GND) and HIGH when the magnet is removed.

// Define Pin Constants
const int reedPin = 2;
const int ledPin = 13;

void setup() {
  // Use internal pull-up to prevent floating signal
  pinMode(reedPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Magnetic Security System Active...");
}

void loop() {
  // Read the state (LOW = Magnet Detected, HIGH = No Magnet)
  int magneticState = digitalRead(reedPin);

  if (magneticState == LOW) {
    digitalWrite(ledPin, HIGH);
    Serial.println("STATUS: CONTACT CLOSED (Magnet Present)");
  } else {
    digitalWrite(ledPin, LOW);
    Serial.println("STATUS: CONTACT OPEN");
  }
  
  delay(200); // Small stability delay
}

Real-World Magnetic Scenarios

The Arduino Nano’s tiny footprint makes it the definitive choice for hidden or integrated magnetic sensing:

  • Door and Window Alarms: Placing a reed switch on a frame and a magnet on the door; the Nano triggers an alert if the magnetic bond is broken (door opened).
  • Bicycle Speedometers: Mounting a magnet on a wheel spoke and a reed switch on the fork; the Nano calculates speed by timing the interval between pulses.
  • Liquid Level Sensing: Using a floating magnet in a tank; as the water rises, the magnet triggers reed switches at specific heights to report volume.
  • Limit Switches for CNC: Providing a non-contact 'home' position for small robotic axes to prevent mechanical crashes without physical switch fatigue.

Common Pitfalls & Durability

  • Glass Fragility: The glass envelope of a reed switch is extremely fragile. If you are using a bare switch rather than a module, avoid bending the leads too close to the glass body, as it may crack and leak the inert gas.
  • Magnet Orientation: Reed switches are directional. If the sensor isn't triggering, try rotating the magnet 90 degrees or moving it along the length of the reeds rather than directly at the center.
  • Hysteresis: There is a small difference between the distance required to 'close' the switch and the distance required to 'open' it again. Account for this 1mm-2mm gap in your mechanical design.
  • Magnetic Interference: Avoid mounting the sensor near high-power motors or transformers, as the electromagnetic fields generated can cause 'false positives' in the reed switch.

Final Summary

Interfacing a Reed Switch with the Arduino Nano is a fundamental step in creating robust, non-contact interaction. By mastering the relationship between magnetic flux and mechanical closure, you bridge the gap between static hardware and a dynamic environment, enabling your projects to sense and secure their surroundings with frictionless precision.