Door Alarm (Reed Switch + Arduino)
Sound a buzzer when a door opens, using a magnetic reed switch. Learn magnetic sensing and INPUT_PULLUP.
Beginner15 min
What you need
- · Magnet
- · Breadboard
- · jumper wires
Wiring
Hover a pin or a wire to trace the connection.
Code
door_alarm.ino
// Buzz when the door opens (magnet moves away from the reed switch).
const int reedPin = 2;
const int buzzer = 8;
void setup() {
pinMode(reedPin, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
}
void loop() {
// INPUT_PULLUP: magnet present (closed door) = LOW; door open = HIGH.
if (digitalRead(reedPin) == HIGH) tone(buzzer, 1200);
else noTone(buzzer);
}
Steps
- Wire the reed switch: one leg to D2, the other to GND.
- Wire the buzzer: + to D8, − to GND.
- Put the magnet next to the reed switch (door closed).
- Upload — moving the magnet away (opening the door) sounds the buzzer.