Arduino UNO R4 - Door Sensor

This tutorial instructs you on how to use an Arduino UNO R4 and a door sensor to monitor the open or closed state of your door or window. We will learn how to install the door sensor and connect it to the Arduino, and then how to program the Arduino to read the state from the door sensor.

Arduino UNO R4 door sensor

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×Door Sensor
1×(Optional) 9V Power Adapter for Arduino UNO R4
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

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. We appreciate your support.

Overview of Door Sensor

Pinout

The door sensor has two parts.

  • One reed switch with two metal connectors
  • One magnet
Door Sensor Pinout

Like a regular switch or button, we do NOT need to distinguish between the two pins of the reed switch.

How It Works

The magnet is fixed to the door or window, which moves, and the reed switch is fixed to the door frame, which doesn't move. When the door is closed, these two parts is closed to each other.

  • When the magnet is near the reed switch, the reed switch circuit closes.
  • When the magnet is away from the reed switch, the reed switch circuit opens.
Door Sensor How It Works Pinout

※ NOTE THAT:

The reed switch does NOT give LOW or HIGH signals directly. It only shows open or closed states. How we connect it to the Arduino UNO R4 affects whether the pin reads as LOW, HIGH, or has a floating value (which is unpredictable). To prevent this floating value, we need to connect a pull-up or pull-down resistor to the pin on the Arduino UNO R4.

If we connect one pin of the reed switch to GND, and the other pin to an input pin on the Arduino UNO R4 with a pull-up resistor (it can be built-in or added separately):

  • When the magnet is near the reed switch, the Arduino UNO R4's input pin reads LOW.
  • When the magnet is away from the reed switch, the Arduino UNO R4's input led reads HIGH.

Thus:

  • To find out if the door is open or closed, look at the input pin of the Arduino UNO R4:
    • If the pin shows LOW, the door is closed.
    • If the pin shows HIGH, the door is open.
  • To know when the door opens or closes, watch for changes in the state of the Arduino UNO R4 input pin:
    • A change from LOW to HIGH means the door is opening.
    • A change from HIGH to LOW means the door is closing.

Wiring Diagram

The wiring diagram between Arduino UNO R4 Door Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For Door Sensor

  • The Arduino UNO R4 pin gets set to digital input mode using the function called pinMode(). For instance, pin 9.
pinMode(9, INPUT_PULLUP);
  • Reads the state of the Arduino UNO R4 pin using the digitalRead() function.
int door_state = digitalRead(9);

Arduino UNO R4 Code - Check Door Open and Close State

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-door-sensor */ #define DOOR_SENSOR_PIN 9 // The Arduino UNO R4 pin connected to door sensor's pin int door_state; void setup() { Serial.begin(9600); // initialize serial pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set arduino 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

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the code above and open it in Arduino IDE.
  • Click the Upload button in Arduino IDE to upload the code to the Arduino UNO R4.
  • Bring the magnet near the reed switch and then move it away from the reed switch.
  • Check the results on the 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 UNO R4 Code - Detect Door-opening and Door-closing Events

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-door-sensor */ #define DOOR_SENSOR_PIN 9 // The Arduino UNO R4 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 serial pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set arduino 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 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 is detected"); // TODO: turn off alarm, light or send notification ... } }
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to transfer the code to your Arduino UNO R4.
  • Bring a magnet near the reed switch, then move it away from the reed switch.
  • Check the results on the Serial Monitor.
COM6
Send
The door closing is detected The door opening is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Learn More

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