Home
Door sensor
Beeps when a door or drawer opens and sends a notification to your phone.
- Easy
- about 35 min
- ESP32-C3
- 6 parts
- 4 wires
- 61 lines of code
- Uses Wi-Fi
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
The switch has no electronics in it: a magnet pulls two thin metal strips together inside a glass tube. The notification goes through ntfy, a free service with no account.
Before you start
Anyone who knows your ntfy topic name can read its messages. Choose a long name nobody could guess, and do not rely on this as a burglar alarm.
What you learn
Detecting a change (opened, closed) and sending a message to a phone over HTTPS.
How it works
input
Door switch
decides
ESP32-C3 · Wi-Fi
output
Buzzer
Notification on your phone
Powered with 5 V through the board's USB-C port.
It joins your home Wi-Fi. You type the network name and password into the sketch before uploading; they stay on the board.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 6 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 4
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
door-sensor.ino
// Door sensor
// Beeps when the door opens and sends a notification to your phone.
// Phone setup: install the free "ntfy" app and subscribe to the same topic
// name you type below. Choose a long name nobody could guess: anyone who
// knows the topic can read its messages.
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
const char* NTFY_TOPIC = "change-this-to-your-own-secret-topic";
const int PIN_DOOR_A = 3;
const int PIN_BUZZER_POS = 10;
bool wasOpen = false;
void notify(const char* text) {
if (WiFi.status() != WL_CONNECTED) return;
WiFiClientSecure client;
client.setInsecure();
HTTPClient http;
if (http.begin(client, String("https://ntfy.sh/") + NTFY_TOPIC)) {
http.addHeader("Title", "Door sensor");
http.POST(text);
http.end();
}
}
void setup() {
// The switch joins the pin to GND while the magnet is beside it (door shut).
pinMode(PIN_DOOR_A, INPUT_PULLUP);
pinMode(PIN_BUZZER_POS, OUTPUT);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 20000) delay(250);
wasOpen = digitalRead(PIN_DOOR_A) == HIGH;
tone(PIN_BUZZER_POS, 1500, 80); // one short beep: ready
}
void loop() {
bool open = digitalRead(PIN_DOOR_A) == HIGH;
if (open != wasOpen) {
delay(50); // ignore the bounce as the magnet passes
open = digitalRead(PIN_DOOR_A) == HIGH;
}
if (open && !wasOpen) {
tone(PIN_BUZZER_POS, 1800, 120);
delay(180);
tone(PIN_BUZZER_POS, 1800, 120);
notify("The door was opened.");
}
if (!open && wasOpen) {
tone(PIN_BUZZER_POS, 900, 100);
notify("The door was closed.");
}
wasOpen = open;
delay(20);
}
Libraries
None. It uses only what the Arduino IDE has built in.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Before uploading, type your Wi-Fi name and password into WIFI_SSID and WIFI_PASS at the top of the sketch.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32C3 Dev Module (USB CDC On Boot: Enabled), then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.