ESP8266 - Door Sensor

The door sensor is a popular choice for security systems, as it can detect and monitor entrances such as doors, windows, and more. It is also referred to as an entry sensor, contact sensor, or window sensor.

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

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Door Sensor
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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 reed switch with two pins
  • A magnet
Door Sensor pinout

Similar to a regular switch/button, there is no need to differentiate between the two pins of the reed switch.

How It Works

The magnet is affixed to the door/window (the movable component), and the reed switch is attached to the door frame (the stationary part). When the door is shut, the two components are in contact:

  • If the magnet is close to the reed switch, the reed switch circuit is closed
  • If the magnet is distant from the reed switch, the reed switch circuit is open
Door Sensor how it works

※ NOTE THAT:

The reed switch does not produce a LOW or HIGH signal on its pins. It is either closed or open. Depending on how we connect it to the ESP8266, the value of the pin can be LOW, HIGH, or an unpredictable floating value. To avoid this, a pull-up or pull-down resistor should be used on the ESP8266 pin.

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

  • When the magnet is close to the reed switch, the value in the ESP8266 input pin is LOW
  • When the magnet is distant from the reed switch, the value in the ESP8266 input pin is HIGH

To determine the status of the door, we simply check 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 opened

We can also detect when the door opens or closes by monitoring the state changes on the ESP8266 input pin:

  • If the state changes from LOW to HIGH, The door-opening has been detected
  • If the state changes from HIGH to LOW, The door-closing has been detected

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and Door Sensor

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

How To Program For Door Sensor

  • Sets the ESP8266 pin to digital input mode by utilizing the pinMode() function. As an example, pin D7 is set as an input.
pinMode(D7, INPUT_PULLUP);
  • Utilizes the digitalRead() function to assess the state of the ESP8266 pin.
int door_state = digitalRead(D7);

ESP8266 Code - Check Door Open and Close State

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-door-sensor */ #define DOOR_SENSOR_PIN D7 // The ESP8266 pin D7 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); // Configure the ESP8266 pin to the 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

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button in the IDE to send the code to the ESP8266.
  • Bring the magnet close to the reed switch, then move it away.
  • Check the Serial Monitor for the result.
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  

ESP8266 Code - Detect Door-opening and Door-closing Events

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

Video Tutorial

Function References