Monitor Your Door Using Magnetic Reed Switch and Arduino

This tutorial demonstrates how to use a magnetic reed switch. I’ll do a quick overview on how it works and show a project example using an Arduino.

Description

A magnetic contact switch is basically a reed switch encased in a plastic shell so that you can easily apply them in a door, a window or a drawer to detect if the door is open or closed.

The switch that we are going to use has two parts: the switch itself, that usually comes opened, and the magnet. When you buy this switch, it also comes with 4 screws, so that you can attach it to your door.

label

How does it work?

It’s very very simple.

The electrical circuit is closed when a magnet is near the switch (less than 13 mm (0.5’’) away). When the magnet is far away from the switch, the circuit is open. See the figure below.

magnetic_reed_switch_howitworks

Where to buy?

These switches are very cheap. You can buy them on ebay here.

Project Example

In this example, we will turn on a red LED if your door is open and a green LED if your door is closed.

You’ll need the following components:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematics

Here’s the schematics for this example.

magnetic contact switch_bb

Uploading Sketch

For this example, upload the following code:

/*
  
 Created by Rui Santos
 
 All the resources for this project:
 https://randomnerdtutorials.com/
 
*/

int ledOpen=8;
int ledClose=10;
int switchReed=6;

void setup(){
  pinMode(ledOpen, OUTPUT);
  pinMode(ledClose, OUTPUT);
  pinMode(switchReed, INPUT);
  Serial.begin(9600);
}

void loop(){
  
  if (digitalRead(switchReed)==HIGH){
    digitalWrite(ledOpen, LOW);
    digitalWrite(ledClose, HIGH);
    Serial.println("Your Door is Closed");
  }
  else {
    digitalWrite(ledOpen, HIGH);
    digitalWrite(ledClose, LOW);
    Serial.println("Your Door is Open");
  }
  delay(1);
}

View raw code

Demonstration

Here’s a video demo showing this project working.

I hope you found this tutorial useful.

Are you thinking of including a magnetic reed switch in one of your projects? Share your thoughts writing a comment below.

Share this post with a friend that also likes electronics!

You can contact me by leaving a comment. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my Facebook Page.

Thanks for reading,

-Rui Santos


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

31 thoughts on “Monitor Your Door Using Magnetic Reed Switch and Arduino”

    • Hi Rod,
      Thank you for the suggestion that was exactly my thought and that’s why I’ve created this companion guide.
      I think I’ll post the ESP8266 + Reed Switch in the next week or two.

      Have a great day,
      Rui

      • Looking forward to that thanks Rui. Of course there are many other applications for a switch input texter!!
        I already have your Home Automation pdf – excellent!.

  1. Hello ,
    Use loads of magnetic reed switches ( enclosed in glass tubes) on my model railway operated by small magnets under the locos. They prompt Arduinos to change points and signals, accelerate and decelerate the trains. I use Schmitt triggers to get rid of the bounce on the switches.

    • You can use this sample code. It should be able to read the sensor just fine.
      Just keep in mind that you can’t use the pins that require SPI communication between your Arduino and your Ethernet shield.

  2. Hi Rui.

    You really do an excelent work, i have a question like how much current pass from the magnetic swicth, i want to do this, but a little different for example when turn on and off a ligth.

    PD. Sorry for my english xD greetings from Honduras, Centroamérica.

  3. Hey, quick Q. Every now and then (e.g. a few times every few seconds) i get the wrong reading from my input pin (it is checked in the loop function, so it is checked very frequently). My input pin mode is set to INPUT_PULLUP. When the magnetic reed switch activates, it connects to ground which pulls the input pin down to low. What could cause it to read as low when it shouldn’t? My wire is about 1m long. Could it be that the wire is so long that it is able to drag the pin down to low intermittently somehow?

  4. Hi this is great. I have an ESP8266–01, the new black one. Can I use this so I set pin 0 and 2 as output and pin 3 as input and use this below? Is there a way to run deepsleep with this if I were to use a battery? Also use MQTT to send open and close notifications?

  5. hi and thanks for the tutorial, just learning, i am trying to figure out how the leds get power to light them up. the output from pins 8 and 10 is digital so 1 or 0 or is it a voltage, apols if this is a stupid question, cheers paddy

    • Hi Paddy.
      When you send 1 to a GPIO, it is the same of sending a HIGH signal which means 5V.
      When you send 0, it is the same as LOW.
      I recommend you follow our Arduino mini course to get started with the basics. The course is completely free, and you can access the course in the link below:
      https://randomnerdtutorials.com/arduino-mini-course/
      I hope this helps.
      Regards
      Sara 🙂

    • Hi.
      That refers to the pin that is connected to the reed switch. In this case it is connected to pin 6.
      Regards,
      Sara

  6. Hi,

    I have another question. It is possible to show only one message?
    So ‘door is open’ 1 time and not every second? And then when I close it, once ‘Door is closed’?

    • Hi.
      The reed switch is like a button.
      So, you can use a debounce sketch.
      In your Arduino IDE, you can go to File > Examples > Digital > Debounce, or you can chose the StateChangeDetection example.
      Then, you just need to make a few modifications to assign the right pins.
      Regards,
      Sara

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.