Arduino - Door Sensor

The door sensor is widely used in security area, It is used to detect/monitor entrances (such as door, window ...). The door sensor is also known as "entry sensors", "contact sensors", or "window sensors".

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Door Sensor
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About Door Sensor

Pinout

Door sensor includes two components:

  • One reed switch with two pins
  • One magnet
Door Sensor Pinout

Just like a normal switch/button, we do NOT need to distinguish the two pins of the reed switch.

How It Works

The magnet is attached to the door/window (moving part), and the reed switch is attached to the door frame (fixed part). The two components are in contact when the door is closed

  • When the magnet is close to the reed switch, the reed switch circuit is closed
  • When the magnet is far from the reed switch, the reed switch circuit is open
Door Sensor How It Works Pinout

※ NOTE THAT:

The reed switch itself does NOT output LOW or HIGH on its pins. It is just a closed or open state. Depending on how we connect its pins to Arduino, the value on the pin can be the LOW, HIGH, or floating value (unpredicted value). To avoid the floating value, we MUST use the pull-up or pull-down resistor on the Arduino pin

If we connect a pin of the reed switch to GND, the other pin of the reed switch to Arduino's input pin with a pull-up resistor (internal or external):

  • When the magnet is closed to the reed switch, the value in Arduino's input pin is LOW
  • When the magnet is far from the reed switch, the value in Arduino's input pin is HIGH

So:

  • To check the state of the door, we just check the state of Arduino's input pin:
    • If the state is LOW, the door is closed
    • If the state is HIGH, the door is open
  • To detect the door-opening/door-closing events, we can detect the state change on the Arduino 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

Arduino Door Sensor Wiring Diagram

This image is created using Fritzing. Click to enlarge image

How To Program For Door Sensor

  • Initializes the Arduino pin to the digital input mode by using pinMode() function. For example, pin 13
pinMode(13, INPUT_PULLUP);
  • Reads the state of the Arduino pin by using digitalRead() function.
int doorState = digitalRead(13);

Arduino Code - Check Door Open and Close State

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-door-sensor */ const int DOOR_SENSOR_PIN = 13; // Arduino pin connected to door sensor's pin int doorState; void setup() { Serial.begin(9600); // initialize serial pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode } void loop() { doorState = digitalRead(DOOR_SENSOR_PIN); // read state if (doorState == HIGH) { Serial.println("The door is open"); } else { Serial.println("The door is closed"); } }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Move the magnet close to the reed switch and them move it far from the reed switch.
  • See the result on 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 Code - Detect Door-opening and Door-closing Events

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-door-sensor */ const int DOOR_SENSOR_PIN = 13; // Arduino pin connected to door sensor's pin int currentDoorState; // current state of door sensor int lastDoorState; // 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 currentDoorState = digitalRead(DOOR_SENSOR_PIN); // read state } void loop() { lastDoorState = currentDoorState; // save the last state currentDoorState = digitalRead(DOOR_SENSOR_PIN); // read new state if (lastDoorState == LOW && currentDoorState == HIGH) { // state change: LOW -> HIGH Serial.println("The door-opening event is detected"); // TODO: turn on alarm, light or send notification ... } else if (lastDoorState == HIGH && currentDoorState == LOW) { // state change: HIGH -> LOW Serial.println("The door-closing event is detected"); // TODO: turn off alarm, light or send notification ... } }
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Move the magnet close to the reed switch and them move it far from the reed switch.
  • See the result on 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

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Function References

The Best Arduino Starter Kit

※ OUR MESSAGES