Categories
TUTORIALS

Using a door sensor to sound an alarm when a door is left open

INTRO

In this Arduino tutorial, a method for sensing when a door is open or closed is discussed. The tutorial expands upon a tutorial by “Arduino Get Started” by adding a buzzer sensor to sound when the door has been open for more than 30 seconds.

REQUIRED HARDWARE

  • Arduino UNO or Genuino UNO (1)
  • Door sensor (1)
    • Includes two components:
      1. One reed switch with two pins
      2. One magnet
  • Passive speaker/ buzzer (1)
    • Typically has two pins:
      1. One negative which goes to the GND  (0V/ground)
      2. One positive pin to receive the control signal from Arduino
  • Breadboard (1)

ABOUT DOOR SENSORS

  • Door sensors, also referred to as “entry sensors” are most commonly used in security areas or places with perishable goods such as grocery stores. The magnet is attached to the door or window (moving part) while the reed switch is attached to the door frame (the fixed part).If the door is closed, the two components are in contact. If the door is open, the magnet moves away from the reed switch (the switch opens). Since the reed switch will not output LOW nor HIGH on its pins, it is necessary to use pull-up or pull-down resistors on the Arduino pin. (otherwise the reed switch only read the pin as closed or open which can be translated into a LOW, HIGH, or floating value– which is NOT what we want!) In the code, notice how instead of the normal INPUT it is now pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP).

SETUP

Set up:

  1. First connect both of the wires on the door sensor to a male to female wire. Make sure to take note of the fact that the magnet’s wire is the positive side of the sensor, which means its connected male to female wire needs to be connected to power in order to detect the status of the door.
  2. Connect the male to female wire connected to the positive side of the door sensor into pin 13. Next connect the male to female wire attached to the pin of the reed switch to the GND on the “Power Analog” side.
  3. Grab your passive buzzer. Make note of where the + sign is on the buzzer to determine which pin is positive and which pin is negative. Next connect two male to female wires into the buzzer pins-making note of the positive and negative pins. Then place the pins into the breadboard and add two pins to connect the wire receiving the voltage to pin 3, and the wire where the circuit ends to the ground on the “Digital PWM~ ” side. Feel free to use the diagram below as a guide for setting up the Arduino hardware:
  4. Finally run the code:

Take a look at the serial monitor. Here is how the serial monitor translates the data from the sensor:

Now the pins will be read as follows:

Closed door = an Arduino input pin as LOW

Open door = an Arduino input pin as HIGH

To check if the state of the door is open a closed:

If the state of the input pin is closed on the serial monitor =  the door is LOW

If the state of the input pin is open  on the serial monitor = the door is HIGH

To detect when the is door-opening / door-closing:

If the state of the input changes from LOW -> HIGH in the serial monitor = the door has opened

If the state of the input changes from HIGH -> LOW in the serial monitor = the door has closed

Sources:

  1. https://arduinogetstarted.com/tutorials/arduino-door-sensor
  2. https://dzone.com/articles/arduino-using-millis-instead-of-delay

Leave a Reply

Your email address will not be published. Required fields are marked *