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:
- One reed switch with two pins
- One magnet
- Includes two components:
- Passive speaker/ buzzer (1)
- Typically has two pins:
- One negative which goes to the GND (0V/ground)
- One positive pin to receive the control signal from Arduino
- Typically has two pins:
- 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:
- 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.
- 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.
- 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:
- Finally run the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
/* Door Motion Sensor by Celine Apollon * Ever wonder how you can make sure your fridge is closed and doesn't start smellin' funky?? * Here below is the Arduino code to upload and notify you when the door is open, and sound a buzzer when * the door has been open for more than 30 seconds. * **This code was modified from the following code at "Arduino Get Started" * - Tutorial page: https://arduinogetstarted.com/tutorials/arduino-door-sensor-piezo-buzzer */ #include "millisDelay.h" //make sure to include the millisDelay library in order //to keep track of how much time has passed in between loops #include "pitches.h"// load the Pitches library in order to use functions of //"tone" and "noTone" to sound the buzzer int period = 30000; //This creates a variable to store the threshold for elapsed time for when to sound the buzzer, in this case it is 30,000 ms or 30 seconds unsigned long time_now = 0;// create a variable for the current time. An unsigned //long variable is a variable with an extended sizes for storing more bytes of //information. const int DOOR_SENSOR_PIN = 13; // Create a variable for the Arduino pin connected //to door sensor's pin const int BUZZER_PIN = 3; // Create a variable for the Arduino pin connected to //Buzzer's pin int doorState; //creates a variable for storing the detected state of the /////////////////////////door (ie whether the door is open or closed) int currentDoorState; // creates a variable for the current state of door sensor int lastDoorState; // creates a variable to store the state of the previous ////////////////////////state of door sensor void setup() { Serial.begin(9600); // initialize serial at 9600 baud pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // assign the DOOR_SENSOR_PIN to input /////////////////////////////////////////////pull-up mode. pinMode(BUZZER_PIN, OUTPUT); // assign the BUZZER_PIN to output mode currentDoorState = digitalRead(DOOR_SENSOR_PIN); // read state of the current ///////DOOR_SENSOR_PIN in order to establish the initial currentDoorState } void loop() { doorState = digitalRead(DOOR_SENSOR_PIN); // read the state and store this /////////////////////////////////////////////variable under the doorState variable lastDoorState = currentDoorState; // save the last state under lastDoorState currentDoorState = digitalRead(DOOR_SENSOR_PIN); // read the new state of the //door sensor if (doorState == HIGH) { //if the doorState is HIGH (door is open) Serial.println("The door is open"); //the serial monitor will print "The door is open" if(millis() > time_now + period){ //if the total time that has elapsed while the door is open is more than 30000 ms time_now = millis(); //reset time_now to be equal to the current elapsed time. This new status of the current elapsed time //used for the next loop tone(BUZZER_PIN,1000); // turn on Buzzer //sound the buzzer if the door /////////////////////////////////has been open for more than 30000 ms } else { noTone(BUZZER_PIN); // turn off/ keep Buzzer off if the door has been ///////////////////////////////open for last than 30000ms, silence the buzzer; }} else { Serial.println("The door is closed"); // otherwise if the door state is closed, print "The door is closed" noTone(BUZZER_PIN); // keep Buzzer off //silence the buzzer } } |
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:
- https://arduinogetstarted.com/tutorials/arduino-door-sensor
- https://dzone.com/articles/arduino-using-millis-instead-of-delay