Arduino UNO Q - Door Sensor

A door sensor uses a reed switch and a magnet to detect whether a door or window is open or closed. On Arduino UNO Q, the MCU reads the reed switch state, and the Linux side can send Telegram alerts the moment a door opens or closes.

In this tutorial, you will learn:

Arduino UNO Q Door Sensor

Hardware Preparation

1×Arduino UNO Q
1×USB Cable for Arduino Uno Q
1×Door Sensor
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno
1×Recommended: Sensors/Servo Expansion Shield for Arduino Uno
1×Recommended: Breadboard Shield for Arduino Uno
1×Recommended: Enclosure for Arduino Uno
1×Recommended: Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

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 the Door Sensor

Pinout

The door sensor consists of two parts:

  • Reed switch — two metal connectors (no polarity)
  • Magnet — attached to the moving part (door or window)
Door Sensor Pinout

The two pins of the reed switch are interchangeable — there is no polarity to observe.

How It Works

The magnet is fixed to the door or window (the moving part). The reed switch is fixed to the door frame (the stationary part).

  • When the door is closed, the magnet is near the reed switch — the circuit closes
  • When the door is open, the magnet moves away — the circuit opens
Door Sensor How It Works

※ NOTE THAT:

The reed switch does NOT output HIGH or LOW directly. Connect one pin of the reed switch to GND and the other to an Arduino UNO Q input pin configured with INPUT_PULLUP. This avoids floating values:

  • Door closed (magnet near) → pin reads LOW
  • Door open (magnet away) → pin reads HIGH

Wiring Diagram

The wiring diagram between Arduino UNO Q Door Sensor

This image is created using Fritzing. Click to enlarge image

Door Sensor Pin Arduino UNO Q MCU
Pin 1 GND
Pin 2 D9 (with INPUT_PULLUP)

How To Program For Door Sensor

  • Set up the Arduino UNO Q MCU pin as a digital input with pull-up resistor:
pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP);
  • Read the door state:
int door_state = digitalRead(DOOR_SENSOR_PIN);
  • Check and respond:
if (door_state == HIGH) Serial.println("The door is open"); else Serial.println("The door is closed");

Arduino UNO Q Code

The Arduino UNO Q has two processors working together:

  • The STM32 MCU reads the reed switch via a digital pin — all sensing runs on the MCU
  • The Qualcomm MPU runs Debian Linux and handles Wi-Fi, Python, and cloud connectivity
  • In this section, only the MCU is programmed — the Linux side stays idle. A later section shows how both processors work together via Bridge.

The MCU reads the door sensor every 500ms and prints the state to the Serial Monitor.

/* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-door-sensor */ // COPYRIGHT newbiely.com // AUTHOR: newbiely // This code is made available for public use without restriction. // For complete instructions, tutorials, and further information, visit: // https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-door-sensor #define DOOR_SENSOR_PIN 9 int door_state; void setup() { Serial.begin(9600); pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); Serial.println("Arduino UNO Q Door Sensor ready"); } void loop() { door_state = digitalRead(DOOR_SENSOR_PIN); if (door_state == HIGH) Serial.println("The door is open"); else Serial.println("The door is closed"); delay(500); }

Detailed Instructions

First time with Arduino UNO Q? Follow the Getting Started with Arduino UNO Q tutorial before proceeding.

  • Install: Fix the magnet to the door and the reed switch to the door frame. Mount them so they are close when the door is closed.
  • Connect: Wire the door sensor to the Arduino UNO Q MCU as shown in the wiring diagram.
  • Open Arduino App Lab: Launch Arduino App Lab and wait until it detects your Arduino UNO Q.
  • Create a new App: Click the Create New App button.
Create New App in Arduino App Lab on Arduino UNO Q
  • Give the App a name, for example: DoorSensor
  • Click Create to confirm.
Arduino App Lab App folders and files on Arduino UNO Q
  • Paste the sketch: Copy the MCU code above and paste it into sketch/sketch.ino. Keep other files as default.
  • No library required — the door sensor uses only the built-in digitalRead() function.
  • Upload: Click the Run button in Arduino App Lab.
Click Run button in Arduino App Lab on Arduino UNO Q
  • Open and close the door while watching the Serial Monitor.

App Lab Console Output

DIYables_Apps
Stop
sketch.ino
1#include "Arduino_RouterBridge.h"
Serial Monitor
Python
Message (Enter to send a message to "Newbiely" on usb(2820070321))
New Line
9600 baud
[2026-04-29 09:00:01] Arduino UNO Q Door Sensor ready [2026-04-29 09:00:02] The door is closed [2026-04-29 09:00:02] The door is closed [2026-04-29 09:00:03] The door is open [2026-04-29 09:00:03] The door is open [2026-04-29 09:00:04] The door is closed

Bridge: Linux + MCU

This section shows how to program both processors of the Arduino UNO Q so the Linux side can detect door events via Bridge:

  • The door sensor is connected to the MCU digital pin — the MCU monitors state changes in loop() and sets event flags when the door opens or closes
  • The MPU cannot read the digital pin directly — it calls Bridge functions to query the current state or consume the door event
  • The MPU has Wi-Fi — running full Debian Linux, it can react to door events and send Telegram notifications instantly
  • Arduino_RouterBridge enables RPC communication between the two processors
  • ⚠️ /dev/ttyHS1 (Linux) and Serial1 (MCU) are RESERVED by the router — never open them in user code

In short: MCU detects door open/close events in loop() → MPU polls via Bridge → MPU triggers Telegram alerts over Wi-Fi.

MCU Code (Bridge)

/* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-door-sensor */ // COPYRIGHT newbiely.com // AUTHOR: newbiely // This code is made available for public use without restriction. // For complete instructions, tutorials, and further information, visit: // https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-door-sensor #include "Arduino_RouterBridge.h" #define DOOR_SENSOR_PIN 9 int last_state = LOW; bool door_opened_event = false; bool door_closed_event = false; String get_state(String arg) { int state = digitalRead(DOOR_SENSOR_PIN); return (state == HIGH) ? "open" : "closed"; } String get_event(String arg) { if (door_opened_event) { door_opened_event = false; return "opened"; } if (door_closed_event) { door_closed_event = false; return "closed"; } return "none"; } void setup() { Bridge.begin(); Monitor.begin(); pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); last_state = digitalRead(DOOR_SENSOR_PIN); Bridge.provide("get_state", get_state); Bridge.provide("get_event", get_event); Monitor.println("Arduino UNO Q Door Sensor Bridge ready"); Monitor.println(last_state == HIGH ? "Door is open" : "Door is closed"); } void loop() { int state = digitalRead(DOOR_SENSOR_PIN); if (state != last_state) { last_state = state; if (state == HIGH) { door_opened_event = true; Monitor.println("Door opened!"); } else { door_closed_event = true; Monitor.println("Door closed."); } } }

Python Code (Bridge)

/* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-door-sensor */ # COPYRIGHT newbiely.com # AUTHOR: newbiely # This code is made available for public use without restriction. # For complete instructions, tutorials, and further information, visit: # https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-door-sensor from arduino.app_utils import * import time def loop(): state = Bridge.call("get_state") print(f"Door state: {state}") time.sleep(0.5) App.run(user_loop=loop)

Detailed Instructions

  • Connect: Wire the door sensor to the Arduino UNO Q as shown in the wiring diagram.
  • Open Arduino App Lab: Launch Arduino App Lab and wait for the board to be detected.
  • Create a new App: Click Create New App, name it DoorSensorBridge, then click Create.
  • Paste the MCU sketch: Copy the MCU Bridge code above and paste it into sketch/sketch.ino.
  • Paste the Python code: Copy the Python Bridge code above and paste it into the Python file in the App.
  • Install the library: Click the Add sketch library button (the open book icon with a + sign) in the left sidebar.
Add sketch library in Arduino App Lab on Arduino UNO Q
  • Search for Arduino_RouterBridge created by Arduino and click the Install button.
My Apps / DIYables Apps
Run
Bricks
No bricks added...
Sketch Libraries
No sketch libra...
Files
python
sketch
.gitignore
README.md
app.yaml
sketch.ino
Add sketch library
Arduino_RouterBridge Arduino

This library provides a simple RPC bridge for Arduino UNO Q boards, allowing communication between the board and other devices using MsgPack serialization.

0.4.1
Install
More Info
  • Upload: Click the Run button in Arduino App Lab.
Click Run button in Arduino App Lab on Arduino UNO Q
  • Open and close the door. Watch state changes appear in the Python console.

App Lab Console Output

DIYables_Apps
Stop
sketch.ino
1#include "Arduino_RouterBridge.h"
Serial Monitor
Python
Message (Enter to send a message to "Newbiely" on usb(2820070321))
New Line
9600 baud
[2026-04-29 09:00:01] Arduino UNO Q Door Sensor Bridge ready [2026-04-29 09:00:01] Door is closed [2026-04-29 09:00:05] Door opened! [2026-04-29 09:00:08] Door closed.
DIYables_Apps
Stop
sketch.ino
1#include "Arduino_RouterBridge.h"
Serial Monitor
Python
[2026-04-29 09:00:02] Door state: closed [2026-04-29 09:00:05] Door state: open [2026-04-29 09:00:08] Door state: closed

Telegram

Get instant Telegram notifications when the door opens or closes.

MCU sketch: Keep the same MCU sketch from the previous Bridge section.

Python Code (Telegram)

/* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-door-sensor */ # COPYRIGHT newbiely.com # AUTHOR: newbiely # This code is made available for public use without restriction. # For complete instructions, tutorials, and further information, visit: # https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-door-sensor from arduino.app_utils import * import requests import time TELEGRAM_BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN" CHAT_ID = "YOUR_CHAT_ID" last_update_id = 0 def get_updates(): global last_update_id url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/getUpdates" params = {"offset": last_update_id + 1, "timeout": 5} try: response = requests.get(url, params=params, timeout=10) data = response.json() if data["ok"]: return data["result"] except Exception as e: print(f"Error getting updates: {e}") return [] def send_message(chat_id, text): url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage" payload = {"chat_id": chat_id, "text": text} try: requests.post(url, data=payload, timeout=10) except Exception as e: print(f"Error sending message: {e}") def loop(): # Auto-notify on door events event = Bridge.call("get_event") if event == "opened": msg = "🚪 Door opened!" print(msg) send_message(CHAT_ID, msg) elif event == "closed": msg = "🔒 Door closed." print(msg) send_message(CHAT_ID, msg) # Handle Telegram commands updates = get_updates() for update in updates: last_update_id = update["update_id"] if "message" not in update: continue message = update["message"] chat_id = message["chat"]["id"] text = message.get("text", "").strip() print(f"Received: {text}") if text == "/start": send_message(chat_id, "Arduino UNO Q Door Sensor Bot\n" "/state - Read current door state\n" "/event - Check for a door event") elif text == "/state": result = Bridge.call("get_state") send_message(chat_id, f"Door state: {result}") elif text == "/event": result = Bridge.call("get_event") send_message(chat_id, f"Door event: {result}") else: send_message(chat_id, "Unknown command. Send /start for help.") time.sleep(0.5) App.run(user_loop=loop)

Detailed Instructions

  • Replace YOUR_TELEGRAM_BOT_TOKEN with your actual bot token from BotFather.
  • Replace YOUR_CHAT_ID with your Telegram chat ID.
  • Paste this Python code into your App's Python file (keep the same MCU sketch).
  • Click the Run button. Open and close the door — check Telegram for instant alerts.

App Lab Console Output

DIYables_Apps
Stop
sketch.ino
1#include "Arduino_RouterBridge.h"
Serial Monitor
Python
[2026-04-29 09:10:00] Waiting for Telegram messages... [2026-04-29 09:10:08] 🚪 Door opened! [2026-04-29 09:10:12] 🔒 Door closed. [2026-04-29 09:10:25] Received: /state
Telegram
Telegram 12:45
Welcome to Telegram!
ArduinoBot 10:19
Chatting with Arduino...
telegram-botfather
BotFather Yesterday
Your bot has been created.

ArduinoBot

bot
Today
/state
10:15 AM ✓✓
Door state: closed
10:16 AM
/event
10:17 AM ✓✓
Door event: none
10:18 AM
🚪 Door opened!
10:19 AM
🔒 Door closed.
10:20 AM

OpenClaw

...OPENCLAW

OpenClaw support for Arduino UNO Q Door Sensor is coming soon.

...OPENCLAW

Project Ideas

You can build many useful projects with the door sensor and Arduino UNO Q:

  • Home Security Alert: Mount the door sensor on your front door — the MPU sends an immediate Telegram alert whenever the door opens, with a timestamp logged to a Linux file for audit
  • Package Delivery Notifier: Fix the sensor to a package drop box — when the lid is opened, you receive a Telegram message notifying you a delivery arrived
  • Child Safety Monitor: Mount the sensor on a cabinet, gate, or restricted area — get Telegram alerts whenever the door opens, with optional auto-lock via a relay triggered through Bridge
  • Energy Saver: Detect when a window is opened and automatically turn off the HVAC system via a relay controlled through Bridge — saving energy and preventing unwanted temperature loss
  • Smart Access Log: Log every door open and close event with a timestamp using Python on the Linux side — generate daily or weekly access reports sent to Telegram

Challenge Yourself

Ready to go further with the door sensor on Arduino UNO Q? Try these challenges:

  • Easy: Add a door-open timer: the Python side records the timestamp when the door opens and sends a Telegram alert if the door remains open for more than 30 seconds.
  • Medium: Implement a daily entry count: the Python side counts the number of door-open events each day and sends a Telegram summary at midnight with the total count.
  • Advanced: Build a multi-zone security system: connect three door sensors to three different digital pins, expose each via a unique Bridge function, and have the Python side monitor all three simultaneously — sending a Telegram alert that identifies which zone was triggered.

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