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.

Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
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

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.

※ 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

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.
- It checks the status of the Arduino Nano 33 IoT pin using the digitalRead() function.
Arduino Nano 33 IoT Code - Check Door Open and Close State
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:
Arduino Nano 33 IoT Code - Detect Door-opening and Door-closing Events
- 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: