Door Sensor with Piezo Buzzer – Arduino Tutorial

2,316 views

Introduction

A magnetic reed switch-controlled home security system is a type of security system that uses a magnetic reed switch and a piezoelectric buzzer to detect and alert homeowners of any potential intrusion. The system uses a magnetic reed switch, which is a type of switch that is activated by a magnetic field, and a piezoelectric buzzer, which is a type of buzzer that produces sound through the use of piezoelectric materials.

In this design scheme, The magnetic reed switch is placed on the door or window, and when the door or window is opened, the magnetic field is broken, activating the reed switch and sending a signal to the Arduino UNO microcontroller. The microcontroller then sends a signal to turn on the piezoelectric buzzer, producing a loud sound that alerts the homeowner of the intrusion.

Hardware Components

You will require the following hardware for Door Sensor Piezo Buzzer with Arduino.

S.noComponentValueQty
1.Arduino UNO1
2.USB Cable Type A to B1
3.Door Sensor1
4.Piezo Buzzer1
5.Power Adapter for Arduino9V1
6.Breadboard1
7.Jumper Wires1

Door Sensor Piezo Buzzer with Arduino

  1. Connect the Megatronix MGT Magnetic Reed Switch to the Arduino UNO. Connect one lead of the reed switch to digital pin 2 and the other lead to GND on the Arduino UNO.
  2. Connect the positive pin of the piezoelectric buzzer to digital pin 9 on the Arduino UNO, and the negative pin to GND.
  3. In the Arduino IDE, define the pin that the reed switch is connected to in the setup() function.
pinMode(2, INPUT);
  1. In the loop() function, use the digitalRead() function to read the input from the reed switch and store it in a variable.
int reedSwitchValue = digitalRead(2);
  1. Use an if-else statement to control the piezoelectric buzzer based on the input from the reed switch. For example, if the reed switch is closed, turn on the buzzer and print the status to the serial monitor.
if (reedSwitchValue == LOW) {
  digitalWrite(9, HIGH);
  Serial.println("Magnetic field detected, Buzzer On");
} else {
  digitalWrite(9, LOW);
  Serial.println("Magnetic field not detected, Buzzer off");
}
  1. Use a delay() function to prevent the program from reading the reed switch too quickly.
delay(100);

Schematic

Make connections according to the circuit diagram given below.

Wiring / Connections

ArduinoReed Switch
D13+ve
GNDGND

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

const int DOOR_SENSOR_PIN = 13; // Arduino pin connected to door sensor's pin
const int BUZZER_PIN      = 3;  // Arduino pin connected to Buzzer's pin

int doorState;

void setup() {
  Serial.begin(9600);                     // initialize serial
  pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(BUZZER_PIN, OUTPUT);            // set arduino pin to output mode
}

void loop() {
  doorState = digitalRead(DOOR_SENSOR_PIN); // read state

  if (doorState == HIGH) {
    Serial.println("The door is open");;
    digitalWrite(BUZZER_PIN, HIGH); // turn on Piezo Buzzer
  } else {
    Serial.println("The door is closed");
    digitalWrite(BUZZER_PIN, LOW);  // turn off Piezo Buzzer
  }
}

Working Explanation

In the setup() function, the configuration of the pin that the reed switch is connected to is done as an input pin. This is done using the pinMode() function, so the microcontroller can read the input from the reed switch. In the loop() function, the microcontroller reads the input from the reed switch using the digitalRead() function, and stores it in a variable. This input is then used to control the state of the piezoelectric buzzer. An if-else statement is used to check the state of the reed switch and based on that, the microcontroller sends a signal to turn on or off the piezoelectric buzzer.

The microcontroller also sends a message to the serial monitor to notify the status of the reed switch and the state of the piezoelectric buzzer. A delay() function is used to prevent the program from reading the reed switch too quickly, and missing any changes in its state. This ensures that the system can react in real-time to changes in the magnetic field.

Applications

  • Banking security: The system can be used in banks to detect unauthorized entry to restricted areas and alert security personnel.
  • Building security: Can be used in buildings to detect unauthorized entry to restricted areas and alert security personnel.
  • Perimeter security: The system can be used to detect intrusions in a perimeter and alert security personnel.
  • Child safety: The system can be used to monitor the movement of children in the home and alert parents if a child goes into an off-limits area.

Conclusion.

We hope you have found this Door Sensor Piezo Buzzer Circuit very useful. If you feel any difficulty in making it feel free to ask anything in the comment section.