#375BeginnerIoT

Pico W IoT Door Sensor (Reed Switch)

Did you leave the door or window open? Check on your phone wherever you are. This project is the step-by-step guide to the Door / window IoT template in the app, built with a Raspberry Pi Pico W, a reed switch and a small magnet using two wires. Go to Workshop › Devices › Add a device, pick Pico W as the board and Door / window as the template; Ohmi prepares the code with your own device key. The code on this page is an example: the key and Wi-Fi details are placeholders.

0.0(0)
Pico W IoT Door Sensor (Reed Switch)
Have you ever left home and wondered whether you closed the balcony door? Or wanted to know if your little brother went into your room? This project is for you. You stick a small reed switch on the door frame and a magnet on the door itself; when the door opens the magnet moves away and the app shows Open right away. The app prepares the code for you; you only connect two wires and upload it. Not even a resistor is needed, the board's built-in pull-up is enough: the simplest start for your first IoT sensor.
IoT · remote controlYour first device is free

Control this device from the app

Connect your ESP32 or Raspberry Pi Pico W to Wi-Fi, then switch it on and off and watch live readings from your phone. Ohmi prepares the code; you just enter your Wi-Fi details and upload it.

Template in the app: Door / window

  • Where is it in the app?Add a new device from the Tools tab or Workshop › Devices and pick this template.
  • Arduino libraryOpen the Library Manager in the Arduino IDE, search for BlueGraysIoT and install it.
Download the app for free

The free plan includes 1 device, 1 chart and 24 hours of history. Premium adds more devices, more charts and longer history.

Circuit Diagram

Pico W IoT Door Sensor (Reed Switch) - Circuit

Source Code

C++ (Arduino IDE)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   Pico W IoT Door Sensor (Reed Switch)
4// ─────────────────────────────────────────
5// The "Door / window" IoT template from the BlueGrays app: see on your
6// phone whether a door or window is open or closed, with a reed switch and
7// a magnet. Ohmi prepares the real code in the app with your own device
8// key (Workshop › Devices › Add a device); the code here is an example,
9// the device key and Wi-Fi details are placeholders. Board package:
10// Raspberry Pi Pico (Earle Philhower). Library Manager: BlueGraysIoT (with
11// its dependencies). The board only joins 2.4 GHz Wi-Fi networks. With the
12// magnet close, the reed closes and the door counts as closed.
13//
14// Pins:
15//   GP14 -> One leg of the reed switch
16//   GND -> Other leg of the reed switch
17//   ESP32: reed GPIO27
18
19/*
20 * Door / window · BlueGrays IoT
21 * BlueGrays · bluegrays.com
22 * Do not share this code: it contains your device key and Wi-Fi password.
23 */
24
25#include <BlueGraysIoT.h>
26
27const char *DEVICE_KEY = "YOUR_DEVICE_KEY";
28const char *WIFI_NAME = "YOUR_WIFI_NAME";
29const char *WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
30
31BlueGraysIoT bg(DEVICE_KEY);
32
33const int REED_PIN = 14;
34bool lastRaw = false, stableOpen = false;
35unsigned long changedAtMs = 0;
36
37void setup() {
38  Serial.begin(115200);
39  pinMode(REED_PIN, INPUT_PULLUP);
40  bg.setDebug(Serial);
41  bg.setLanguage(BG_LANG_EN);
42  bg.begin(WIFI_NAME, WIFI_PASSWORD);
43}
44
45void loop() {
46  bg.loop();  // avoid long delay() calls here
47  bool raw = digitalRead(REED_PIN) == HIGH;
48  if (raw != lastRaw) { lastRaw = raw; changedAtMs = millis(); }
49  if (millis() - changedAtMs > 50) stableOpen = raw;
50  bg.send("kapi", stableOpen ? "acik" : "kapali");
51}
52

FAQ

Can I upload the code on this page straight to my board?

You can, but it will not connect: the device key and Wi-Fi details in it are placeholders. In the app go to Workshop › Devices › Add a device, pick Pico W as the board and the Door / window template; Ohmi prepares the code with your key, network name and password. Copy that code and upload it. You can see the full code later in Device › Code and key.

How do I upload to the Pico W? Is it MicroPython?

This project runs on Arduino C++, not MicroPython. In Arduino IDE install the "pico" package (Raspberry Pi Pico/RP2040, Earle Philhower) from Boards Manager and pick Raspberry Pi Pico W under Tools › Board. If it is the board's first upload or no port shows up, hold the BOOTSEL button while plugging in the USB cable; Arduino IDE sees the board as a drive and uploads to it. Install BlueGraysIoT with its dependencies from Library Manager.

How do I fit the reed switch and the magnet?

Stick or screw the reed switch to the door frame and the magnet to the door so that it sits right opposite the switch when the door is closed; keep the gap under 1 cm. With the magnet close the reed closes and the door shows Closed; when the door opens and the magnet moves away, it shows Open. The reed's two legs have no polarity, so it does not matter which one goes to GP14. If you need longer wires, a thin two-core cable is enough. Glass reeds are fragile, so do not bend the legs right next to the body.

Does it work with an ESP32?

Yes. Pick ESP32 as the board when you add the device; the code stays the same and only the pin changes: one leg of the reed goes to GPIO27 and the other to GND. In Arduino IDE install the "esp32" package from Boards Manager. The circuit and photo on this page were made with a Pico W.