PinoutSearch

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

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

  1. Wire the reed switch: one leg to D2, the other to GND.
  2. Wire the buzzer: + to D8, − to GND.
  3. Put the magnet next to the reed switch (door closed).
  4. Upload — moving the magnet away (opening the door) sounds the buzzer.