ESP32 - Door Sensor

The door sensor (also known as entry sensor, contact sensor, or window sensor) is widely used in many kinds of application, especially for security. It is used to detect/monitor entrances (such as door, window ...). This tutorial instructs you how to use ESP32 with the door sensor.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Door Sensor
1×(Optional) DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

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 in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables.

Introduction to Door Sensor

Door Sensor Pinout

The door sensor has two components:

  • One magnet
  • One reed switch, which has two wires
Door Sensor Pinout

Just like the switch/button, we do NOT need to differentiate the two wires of the reed switch.

How Door Sensor Works

The magnet and the reed switch are installed on the door/windows as follows:

  • The magnet is the movable part. It should be attached to the door/window
  • The reed switch is the fixed part. It should be attached to the door frame

The two components are in contact when the door is closed.

  • The reed switch circuit is closed when it is near to the magnet
  • The reed switch circuit is open when it is far from the magnet
Door Sensor how it works

※ NOTE THAT:

Just like a button, we MUST use the pull-up or pull-down resistor on the ESP32 pin, which connects to the reed switch

If we connect reed switch as follows: one wire to GND, the other to ESP32's input pin with a pull-up resistor:

  • The ESP32's input pin is LOW when the magnet is near to the reed switch
  • The ESP32's input pin is HIGH when the magnet is far from the reed switch

So:

  • If the ESP32's input pin is LOW, the door is closed
  • If the ESP32's input pin is HIGH, the door is opened
  • If the ESP32's input pin changes from LOW to HIGH, the door is opening
  • If the ESP32's input pin changes from HIGH to LOW, the door is closing

Wiring Diagram between Door Sensor and ESP32

  • How to connect ESP32 and door sensor using breadboard
ESP32 Door Sensor Wiring Diagram

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and door sensor

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

How To Program Door Sensor

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

ESP32 Code - Check Door Open and Close State

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-door-sensor */ #define DOOR_SENSOR_PIN 19 // ESP32 pin GPIO19 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 Instructions

  • 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.
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
  • 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

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-door-sensor */ #define DOOR_SENSOR_PIN 19 // ESP32 pin GPIO19 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.
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
  • 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

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES