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.
- Ultrasonic Sensor
- 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 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.
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.
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:
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.
Pin no 10 is configured as output pin and is connected to buzzer. Other pin of buzzer is connected to GND of Arduino.
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
This Post / Project can also be found using search terms:
- arduino uno ultrasonic sensor door alarm