03 / 20
Guard the fridge
A magnetic reed switch tells you when a door has been open too long.
Run this experiment →Why it’s here
Open the door and wait for the alarm; close it to reset the timer.
How it works
A magnetic reed switch tells you when a door has been open too long. A persistent open state must exceed the delay. Closing the door resets the elapsed time. This guided model targets the original ESP32 and Arduino-ESP32 3.x; radio, acoustics, loads and mechanics are simplified.
Try it
- Open this experiment
- Open the door and wait for the alarm; close it to reset the timer.
Connections
- GPIO 27 / GND — Reed switch · INPUT_PULLUP
- GPIO 25 — Passive buzzer signal
Example sketch
unsigned long openedAt = 0;
// Reed switch between GPIO 27 and GND; magnet closes it.
void setup() {
pinMode(27, INPUT_PULLUP);
ledcAttach(25, 2000, 8);
}
void loop() {
bool open = digitalRead(27) == HIGH;
if (!open) openedAt = millis();
ledcWriteTone(25, open && millis() - openedAt >= 3000 ? 880 : 0);
}Check your understanding
Why does the alarm wait?
Show the answer
A persistent open state must exceed the delay. Closing the door resets the elapsed time.