Arduino Nano 33 IoT - Door Sensor

The door sensor, also called an entry sensor, contact sensor, or window sensor, is used in many applications, especially for security. It help detect when a door, window, or similar entry is opened. This guide shows you how to use the Arduino Nano 33 IoT with a door sensor.

Arduino Nano 33 IoT Door Sensor

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Door Sensor
1×Optionally, 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

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 is made up of two parts:

  • One magnet
  • One reed switch with two wires
Door Sensor Pinout

Just like any regular switch or button, we don't need to tell the two wires of the reed switch apart.

How Door Sensor Works

The magnet and the reed switch are placed on doors and windows like this:

  • The magnet is the part that moves. It should be placed on the door or window.
  • The reed switch is the part that stays in one place. It should be attached to the door frame.

When the door is closed, the two parts touch each other.

  • When the reed switch is close to the magnet, it connects the circuit. When it is far away from the magnet, the circuit is disconnected.
Door Sensor how it works

※ NOTE THAT:

Just like with a button, you need to use a pull-up or pull-down resistor on the Arduino Nano 33 IoT pin that connects to the reed switch.

Connect one wire of the reed switch to GND and the other wire to an input pin on the Arduino Nano 33 IoT using a pull-up resistor.

  • The input pin on the Arduino Nano 33 IoT goes LOW when the magnet is close to the reed switch.
  • The input pin on the Arduino Nano 33 IoT goes HIGH when the magnet is far from the reed switch.

So:

  • When the Arduino Nano 33 IoT's input is LOW, the door is closed.
  • When the Arduino Nano 33 IoT's input is HIGH, the door is open.
  • When the input changes from LOW to HIGH, the door is opening.
  • When the input changes from HIGH to LOW, the door is closing.

Wiring Diagram between Door Sensor and Arduino Nano 33 IoT

The wiring diagram between Arduino Nano and 33 IoT Door Sensor

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. Although these pins can be used as digital input pins, it is recommended to avoid using them for digital input. If you must use them, do not use internal or external pull-down resistors for these pins

How To Program Door Sensor

  • This code sets up an Arduino Nano 33 IoT pin to receive digital signals using the [pinMode()] function. For example, it sets pin D2 as the input.
pinMode(2, INPUT_PULLUP);
  • It checks the status of the Arduino Nano 33 IoT pin using the digitalRead() function.
int door_state = digitalRead(D2);

Arduino Nano 33 IoT Code - Check Door Open and Close State

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-door-sensor */ #define DOOR_SENSOR_PIN 2 // The Arduino Nano 33 IoT 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 Arduino Nano 33 IoT 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 you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile the code and send it to the Arduino Nano 33 IoT board.
  • Bring the magnet close to the reed switch, then move it away.
  • Look at the results in the Serial Monitor. It should look like this:
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 33 IoT Code - Detect Door-opening and Door-closing Events

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-door-sensor */ #define DOOR_SENSOR_PIN 2 // The Arduino Nano 33 IoT 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 Arduino Nano 33 IoT 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 code above and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and send the code to your Arduino Nano 33 IoT board.
  • Bring the magnet near the reed switch, then move it away.
  • Look at the result in the Serial Monitor. It should look like this:
COM6
Send
The door-closing event is detected The door-opening event 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!