Arduino Mega - Door Sensor

This guide shows you how to use an Arduino Mega and a door sensor to check if your door or window is open or closed. We'll learn how to install the door sensor, connect it to the Arduino, and how to write a program for the Arduino to read the sensor's status.

Arduino Mega door sensor

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Door Sensor

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Door Sensor

Pinout

The door sensor has two parts.

  • One reed switch with two metal terminals
  • One magnet
Door Sensor Pinout

Like a normal switch or button, you don’t need to tell the two pins of the reed switch apart.

How It Works

A magnet is attached to the door or window, which moves. The reed switch is attached to the door frame, which stays still. When the door closes, the magnet and the reed switch come together.

  • When the magnet is close to the reed switch, the circuit closes.
  • When the magnet is far from the reed switch, the circuit opens.
Door Sensor How It Works Pinout

※ NOTE THAT:

Reed switch doesn’t give a low or high signal by itself. It only shows open or closed. The way you connect it to the Arduino Mega decides if the pin reads LOW, HIGH, or floats (unpredictable). To avoid floating, add a pull-up or pull-down resistor to the pin on the Arduino Mega.

If we connect one pin of the reed switch to ground, and the other pin to an input pin on the Arduino Mega with a pull-up resistor (it can be built-in or added separately):

  • When the magnet is close to the reed switch, the Arduino Mega's input pin shows LOW.
  • When the magnet is far from the reed switch, the Arduino Mega's input pin shows HIGH.

So:

  • To see if the door is open or closed, look at the pin on the Arduino Mega.
    • If the pin is LOW, the door is closed.
    • If the pin is HIGH, the door is open.
  • To know when the door opens or closes, watch for changes on that pin:
    • A change from LOW to HIGH means the door is opening.
    • A change from HIGH to LOW means the door is closing.

Wiring Diagram

The wiring diagram between Arduino Mega Door Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For Door Sensor

  • On the Arduino Mega, a pin is set as a digital input using the pinMode() function. For example, pin 9.
pinMode(9, INPUT_PULLUP);
  • Reads the pin status on the Arduino Mega using the digitalRead() function.
int door_state = digitalRead(9);

Arduino Mega Code - Check Door Open and Close State

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-door-sensor */ #define DOOR_SENSOR_PIN 9 // The Arduino UNO R4 pin connected to door sensor's pin int door_state; void setup() { Serial.begin(9600); // initialize serial pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode } void loop() { door_state = digitalRead(DOOR_SENSOR_PIN); // read state if (door_state == HIGH) { Serial.println("The door is open"); } else { Serial.println("The door is closed"); } }

Detailed Instructions

Follow these steps one by one.

  • Connect the parts following the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the correct board: Arduino Mega, and the right COM port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button to send the code to the Arduino Mega.
  • Bring the magnet close to the reed switch, then move it away.
  • Check the results in the Serial Monitor.
COM6
Send
The door is open The door is open The door is closed The door is closed The door is closed The door is closed The door is closed The door is open The door is open
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Arduino Mega Code - Detect Door-opening and Door-closing Events

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-door-sensor */ #define DOOR_SENSOR_PIN 9 // The Arduino UNO R4 pin connected to door sensor's pin int door_state; // current state of door sensor int prev_door_state; // previous state of door sensor void setup() { Serial.begin(9600); // initialize serial pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode door_state = digitalRead(DOOR_SENSOR_PIN); // read state } void loop() { prev_door_state = door_state; // save the last state door_state = digitalRead(DOOR_SENSOR_PIN); // read new state if (prev_door_state == LOW && door_state == HIGH) { // state change: LOW -> HIGH Serial.println("The door opening is detected"); // TODO: turn on alarm, light or send notification ... } else if (prev_door_state == HIGH && door_state == LOW) { // state change: HIGH -> LOW Serial.println("The door closing is detected"); // TODO: turn off alarm, light or send notification ... } }
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to upload the code to your Arduino Mega.
  • Bring a magnet near the reed switch, then move it away.
  • Check the results in the Serial Monitor.
COM6
Send
The door closing is detected The door opening is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!