Arduino Nano 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 provides instructions on how to use Arduino Nano ESP32 with the door sensor.

Hardware Preparation

1×Arduino Nano ESP32
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 Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter For Arduino Nano 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 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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of 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 Arduino Nano 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 Arduino Nano ESP32

The wiring diagram between Arduino Nano ESP32 and Door Sensor

This image is created using Fritzing. Click to enlarge image

How To Program Door Sensor

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

Arduino Nano ESP32 Code - Check Door Open and Close State

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-door-sensor */ #define DOOR_SENSOR_PIN D2 // The Arduino Nano ESP32 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 ESP32 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

  • If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduino IDE.
  • Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to Arduino Nano 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.
  • Check out the result on the 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  

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

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-door-sensor */ #define DOOR_SENSOR_PIN D2 // The Arduino Nano ESP32 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 ESP32 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 above code and paste it to Arduino IDE.
  • Compile and upload code to Arduino Nano 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.
  • Check out the result on the 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

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