Skip to main content

How to use a Reed Switch

What is a Reed Switch?

The Reed Switch is a type of magnetic switch that changes its state in the presence of a magnetic field. It contains two small ferromagnetic reeds sealed inside a glass tube. When a magnet comes close, the reeds attract each other and close the circuit; when the magnet is removed, the circuit opens again.

Reed sensors are commonly used in door/window sensors, speed detection, and magnetic limit switches.

This Adafruit Magnetic contact switch (door sensor) is also a reed switch.

Wiring

  1. One End to Pin 2
  2. One End to GND

Getting started

After uploading the code, you can use a magnet to trigger the switch, and the built-in LED will light up.

const int REED_PIN = 2; // Pin connected to reed switch
const int LED_PIN = 13; // LED pin - active-high

void setup() 
{
  Serial.begin(9600);
  // Since the other end of the reed switch is connected to ground, we need
  // to pull-up the reed switch pin internally.
  pinMode(REED_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
}

void loop() 
{
  int proximity = digitalRead(REED_PIN); // Read the state of the switch
  if (proximity == LOW) // If the pin reads low, the switch is closed.
  {
    Serial.println("Switch closed");
    digitalWrite(LED_PIN, HIGH); // Turn the LED on
  }
  else
  {
    digitalWrite(LED_PIN, LOW); // Turn the LED off
  }
}