Built-in project · Arduino Uno R3

Door alarm on an Arduino Uno

A reed switch and a magnet on pin 2: open the door and a buzzer and a lamp latch on until the arcade button resets them. Half-size board. The whole build, an Arduino Uno and 5 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

BeginnerRuns in your browser. Free, and no account needed.

Door alarm · Arduino Uno R3live0.000 s 0.00x
Hold the button
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Door alarm: a reed switch on the frame, a magnet on the door.
//
//   pin 2  the reed switch, other leg to GND, internal pull-up
//   pin 3  the reset button, other leg to GND, internal pull-up
//   pin 8  the buzzer, other leg to GND
//   pin 9  the lamp, through 220 ohm to GND
//
// A reed switch is two steel blades in a glass tube. A magnet near it pulls
// them together and the pin reads LOW; take the magnet away and the blades
// spring apart and the pull-up takes the pin HIGH. So on a door: magnet on the
// door, switch on the frame, closed door is LOW, open door is HIGH. Drag the
// slider on the switch to move the magnet.
//
// # Two things the part will teach you if you let it
//
// The blades bounce. Not for a hundred milliseconds like the ball in a tilt
// switch, but for a fraction of one, and it is still enough to count an
// opening twice if the sketch believes the first edge it sees. So the reading
// has to hold for SETTLE_MS before it counts.
//
// The switch does not let go where it grabbed. It closes when the magnet is
// about 15 mm away and holds on out to nearly 19 mm, because once the blades
// are touching the magnetic circuit through them is better. That gap is
// hysteresis, it is on every reed switch datasheet as an operate and a release
// figure, and it is the reason a door that creaks an inch does not set this
// off.
//
// # Latching
//
// Shutting the door again does not stop the alarm: it has seen the door open,
// and that is the thing worth knowing. Press the button to reset it.

const int REED = 2;
const int BUTTON = 3;
const int BUZZER = 8;
const int LAMP = 9;

const unsigned long SETTLE_MS = 20;
const unsigned long BEEP_MS = 200;

bool alarm = false;
bool openNow = false;
bool lastRead = false;
unsigned long changedAt = 0;
unsigned long beepAt = 0;
bool beepOn = false;
int opened = 0;

void setup() {
  pinMode(REED, INPUT_PULLUP);
  pinMode(BUTTON, INPUT_PULLUP);
  pinMode(BUZZER, OUTPUT);
  pinMode(LAMP, OUTPUT);
  digitalWrite(BUZZER, LOW);
  digitalWrite(LAMP, LOW);
  Serial.begin(115200);
  Serial.println("Mokxi Uno: door alarm, reed switch on pin 2");
  // Start from where the door is now, so only a change after Run counts. A
  // door that is already open at power-up is not a break-in.
  lastRead = openNow = digitalRead(REED) == HIGH;
}

void loop() {
  // HIGH is the pull-up with the blades apart, which is the door open.
  bool raw = digitalRead(REED) == HIGH;
  if (raw != lastRead) {
    lastRead = raw;
    changedAt = millis();
  } else if (raw != openNow && millis() - changedAt >= SETTLE_MS) {
    openNow = raw;
    if (openNow) {
      opened++;
      alarm = true;
      Serial.print("door opened, count ");
      Serial.println(opened);
    } else {
      Serial.println("door closed");
    }
  }

  if (digitalRead(BUTTON) == LOW && alarm) {
    alarm = false;
    Serial.println("alarm reset");
  }

  if (alarm) {
    if (millis() - beepAt >= BEEP_MS) {
      beepAt = millis();
      beepOn = !beepOn;
      digitalWrite(BUZZER, beepOn ? HIGH : LOW);
      digitalWrite(LAMP, beepOn ? HIGH : LOW);
    }
  } else if (beepOn) {
    beepOn = false;
    digitalWrite(BUZZER, LOW);
    digitalWrite(LAMP, LOW);
  }
}

Parts list

7 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

6 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 9; Resistor, 220 Ω pin 1
  • Arduino Uno R3 pin 8; Buzzer pin 1
  • Arduino Uno R3 pin 3; Arcade button pin 2
  • Arduino Uno R3 pin 2; Reed switch pin 1
  • Ground: Arduino Uno R3 pin GND; Reed switch pin 2; Arcade button pin 1; Buzzer pin 2; LED, red pin C
  • Resistor, 220 Ω pin 2; LED, red pin A

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.