Door Alarm using Arduino and Ultrasonic Sensor

Security has always been a major concern for all of us and there are many Hi tech and IoT based security and surveillance system are available in the market. Intruder or Burglar Alarm is one of the classic and popular project among the Electronics students and hobbyists. We have also built many Burglar Alarms based on various technologies:

Door Alarm using Arduino and Ultrasonic Sensor

Today we are adding one more Security Alarm in our list which is based on Ultrasonic Sensor. This Arduino Controlled Door alarm can be installed near the door to detect the presence of anybody at the door. Whenever somebody comes in the range of Ultrasonic sensor, buzzer starts beeping. You can adjust the sensor detection range according to your door. This system can also serve the purpose of Motion Detector.

Required Components:

  • Breadboard
  • Ultrasonic Sensor
  • Buzzer
  • Arduino Mega (any model)
  • Jumper Wires
  • USB cable for Arduino or 12v, 1A adapter.

Ultrasonic Sensor Module:

Ultrasonic sensor HC-SR04 is used here to detect the presences of any person at the door. The sensor module consists of ultrasonic transmitter, receiver and the control circuit. Ultrasonic Sensor consists of two circular eyes out of which one is used to transmit the ultrasonic wave and the other to receive it.

We can calculate the distance of the object based on the time taken by ultrasonic wave to return back to the sensor. Since the time and speed of sound is known we can calculate the distance by the following formulae.

  • Distance = (Time x Speed of Sound) / 2

The value is divided by two since the wave travels forward and backward covering the same distance. But in this project we have used NewPing.h library, and this library takes care of this calculation and we just need to use some key words, explanation is given in programing section below.

Check the below project to measure the distance of any object and to properly understand the Ultrasonic sensor working:

Circuit Diagram and Explanation:

Circuit connections for this Ultrasonic Alarm are very simple. Trigger pin of ultrasonic sensor is connected to pin no. 12 of Arduino and Echo pin of sensor is connected to pin no 11 of Arduino. Vcc of sensor is connected to 5V pin of Arduino and GND of sensor is connected to GND of Arduino. One pin of buzzer is connected to GND of Arduino and the other pin is connected to 8th pin of Arduino.

Working Explanation:

Working this Arduino Door Alarm is very easy. Whenever anyone comes in the path/range of Ultrasonic Sensor, microcontroller detects the distance of object from the sensor and if the object is in the defined range, it sends the High signal to the buzzer and buzzer starts beeping.

You can test the circuit by putting any thing in front of sensor within the range, check the Video for demonstration. This function of Ultrasonic Sensor can also be used to build Obstacle Avoiding Robot.

Programming Explanation:

In this project we have used NewPing.h Library for Ultrasonic sensor, developed by Tim Eckel. Although we can use Ultrasonic sensor without this library like we did in our previous project, but this Library provides many good features for Ultrasonic sensor and it becomes easy to Code for ultrasonic sensor using this library. We can use ultrasonic sensor’s functions easily using this library without writing too many lines of code; it’s like other libraries which are used to handle the complexity at lower level.

#include <NewPing.h>

You can check all the features, uses and sample codes of this Library by following this link. Also check the official Arduino page of this Library.

Latest release of Library can be downloaded from the above given link. Further, we have downloaded the Library from below link, which is modified for Teensy:

https://github.com/PaulStoffregen/NewPing

You should first test the sensor by burning Example Codes given at its page. We have also used the Example Codes for our project and modified them according to our Door Alarm Project.

Trigger pin is connected to the Pin 12 of Arduino and Echo pin is connected to pin 11 of Arduino. MAX_DISTANCE means that the distance up to which the sensor can detect the obstacle is 500 cm or 5m.

#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 500

Below line states the Baud Rate at which the data is sent to the Arduino serial port from ultrasonic sensor.

Serial.begin(115200);

Pin no 10 is configured as output pin and is connected to buzzer. Other pin of buzzer is connected to GND of Arduino.

Door Alarm using Arduino and Ultrasonic Sensor schematic

pinMode(10, OUTPUT);

In void echoCheck() function, sonar.ping_result / US_ROUNDTRIP_CM is used to calculate the distance of  obstacle from the sensor. flag is used to execute the buzzer when obstacle is in 50 cm range from the ultrasonic sensor. You can change this ‘distance’ according to your requirement or your door size.

if ((sonar.ping_result / US_ROUNDTRIP_CM) < 50)
      flag = 1;
    else if ((sonar.ping_result / US_ROUNDTRIP_CM) > 50)
      flag = 0;

The Code is very well commented by the Author of NewPing.h library and can be easily understood. Further you can check the library page itself to get better understanding of it and can use this library to make complex project using ultrasonic sensor. Full code for this Door Alarm project is given below.

Primarily Ultrasonic sensor is used to measure distance from any object, but here we can see that it can be used as Security alarm or Door alarm with Arduino. Likewise we can create many useful projects using this like: Automatic Water Level Indicator and Controller using Arduino

Read more: Door Alarm using Arduino and Ultrasonic Sensor


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

Scroll to Top