ESP32 - 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×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Door Sensor
Please note: These are affiliate links. If you buy the components through these links, We may 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 ESP32, 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 ESP32 pin

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

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

So:

  • To check the state of the door, we just check the state of ESP32'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 ESP32 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

ESP32 Door Sensor Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

How To Program For Door Sensor

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

ESP32 Code - Check Door Open and Close State

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-door-sensor */ const int DOOR_SENSOR_PIN = 13; // ESP32 pin connected to door sensor's pin int doorState; void setup() { Serial.begin(9600); // initialize serial pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set ESP32 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

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Move the magnet close to the reed switch and them move it far from the reed switch.
  • See the result on Serial Monitor. It looks like the below:.
  • 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  

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

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-door-sensor */ const int DOOR_SENSOR_PIN = 13; // ESP32 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 ESP32 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 paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Move the magnet close to the reed switch and them move it far from the reed switch.
  • See the result on Serial Monitor. It looks like the below:.
  • 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

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere