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 build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

36 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

      Reply
      • 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!.

        Reply
  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.

    Reply
    • 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.

      Reply
  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.

    Reply
  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?

    Reply
  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?

    Reply
  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

    Reply
    • 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 🙂

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

      Reply
  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’?

    Reply
    • 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

      Reply
  7. I need to control up to 10 different doors (very close to each other, a multiple shelve storage with one door to each shelve).
    Can this be done using only 1 Arduino, or do I really need 1 Arduino for each door?
    I want the system to show a GREEN light when door is open, and RED light when door is closed.

    Reply
  8. I don’t know if you still have access to this or are you still access it tutorial
    What I would like to ask do you know how I would make a battery operated loud siren to connect to contacts on outbuilding doors of my farm so that I would be alerted with the loud extra loud siren when someone broken or opened a door. Just a simple magnetic contact switch connected to a siren and a battery that would go off when the door was open no one seems to know without going into a big rigmarole putting boxes together in all this other stuff that I don’t really need I need about five of them and so do other people on my farm area they would really like this we discussed it but nobody has the technology or know how and when we contact alarm companies they want to sell us these big complicated things that require electricity don’t need it hooked to Wi-Fi I don’t need it hooked to phone systems just loud just asking
    I’m a little bit savvy when it comes to putting things together but every thing that I’ve looked up gets way too complicated for the simple situation. Many many thanks if you get this an answer me
    Lynn

    Reply

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.