Arduino Nano - Door Sensor

The door sensor is a popular choice for security purposes. It is used to detect and monitor entrances, such as doors and windows. This device is also referred to as an entry sensor, contact sensor, or window sensor.

This tutorial instructs you how to use Arduino Nano with the door sensor. In detail, we will learn:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Door Sensor
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor 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. We appreciate your support.

Overview of Door Sensor

The Door Sensor Pinout

A door sensor consists of two parts:

  • A magnet.
  • A reed switch that has two pins: Similar to a regular switch/button, it is not necessary to differentiate the two pins.
Door Sensor pinout

How It Works

The magnet is affixed to the door/window (the part that moves), and the reed switch is secured to the door frame (the static element). The two components come into contact when the door is shut:

  • When the magnet is near the reed switch, the reed switch circuit is closed
  • When the magnet is away from the reed switch, the reed switch circuit is open
Door Sensor how it works

※ NOTE THAT:

The reed switch does not provide an output of either LOW or HIGH on its pins. It is either closed or open. Depending on how we connect it to Arduino Nano, the pin can have a value of LOW, HIGH, or a floating value (unpredictable). To prevent the floating value, a pull-up or pull-down resistor must be used on the Arduino Nano pin.

If we connect one pin of the reed switch to GND and the other pin of the reed switch to an Arduino Nano input pin with a pull-up resistor (internal or external):

  • When the magnet is near the reed switch, the value in the Arduino's input pin is LOW
  • When the magnet is away from the reed switch, the value in the Arduino's input pin is HIGH

To determine the state of the door, we simply look at the state of the Arduino's input pin:

  • If the state is LOW, the door is closed
  • If the state is HIGH, the door is open

We can also detect when the door is opened or closed by looking for a change in the state of the Arduino's input pin:

  • If the state changes from LOW to HIGH, the door-opening event is detected
  • If the state changes from HIGH to LOW, the door-closing event is detected

Wiring Diagram

The wiring diagram between Arduino Nano and Door Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For Door Sensor

  • Initialize the Arduino Nano pin to digital input mode by using the pinMode() function. For example, the following code can be used to set pin 2 as an input.
pinMode(2, INPUT_PULLUP);
  • Utilizes the digitalRead() function to ascertain the state of the Arduino Nano pin.
int door_state = digitalRead(2);

Arduino Nano Code - Check Door Open and Close State

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-door-sensor */ #define DOOR_SENSOR_PIN 2 // The Arduino Nano pin connected to door sensor's pin int door_state; void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. 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

  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Bring the magnet close to the reed switch and then move it away.
  • Check out the result on 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 Nano Code - Detect Door-opening and Door-closing Events

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-door-sensor */ #define DOOR_SENSOR_PIN 2 // The Arduino Nano 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 the Serial to communicate with the Serial Monitor. 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 event 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 event is detected"); // TODO: turn off alarm, light or send notification ... } }
  • Copy the code and open it in Arduino IDE.
  • Click the Upload button to transfer the code to Arduino Nano.
  • Bring the magnet close to the reed switch and then move it away.
  • View the outcome on the Serial Monitor.
COM6
Send
The door-closing event is detected The door-opening event is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Function References

※ 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!