ARDUINO

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!

header-200.png?w=828&quality=100&strip=all&ssl=1

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);
}
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now