Summary of Door Alarm using Arduino and Ultrasonic Sensor
This article details an Arduino-controlled door alarm project using an ultrasonic sensor to detect intruders. When a person enters the sensor's range, a buzzer activates. The system utilizes the NewPing library for simplified distance calculation and allows adjustable detection thresholds via code.
Parts used in the Arduino Controlled Door Alarm:
- Breadboard
- Ultrasonic Sensor HC-SR04
- Buzzer
- Arduino Mega
- Jumper Wires
- USB cable or 12v, 1A adapter
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:
- Laser Security Alarm Circuit
- IR Based Security Alarm
- Burglar Alarm using PIR
- GSM Based Security System
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:
- Arduino Based Distance Measurement using Ultrasonic Sensor
- Distance Measurement using HC-SR04 and AVR Microcontroller
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.
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
- How does the Ultrasonic Sensor detect a person?
The sensor transmits an ultrasonic wave and receives the echo to calculate distance based on the time taken for the wave to return. - Can the detection range be adjusted?
Yes, you can change the distance value in the code according to your door size or requirement. - Which library is recommended for this project?
The NewPing.h library developed by Tim Eckel is used to simplify coding and handle lower-level complexity. - What pins are used for the Trigger and Echo connections?
The Trigger pin connects to Arduino pin 12 and the Echo pin connects to Arduino pin 11. - How is the buzzer triggered in the circuit?
The microcontroller sends a High signal to pin 8 (or pin 10 as per code configuration) when an object is within the defined range. - What is the maximum distance defined in the example code?
The MAX_DISTANCE variable is set to 500 cm or 5 meters in the provided code. - Can this system function as a motion detector?
Yes, the text states that this system can also serve the purpose of a Motion Detector. - Is it possible to use this sensor without the NewPing library?
Yes, the article mentions that the sensor can be used without the library, but the library makes coding easier.


