Introduction: Door Alarm Using Arduino

About: hIOTron is an IoT Based company that offers IoT Platforms, IoT Solutions, IoT Training.

Security is a major concern so we have designed a door alarm system to recognize the presence of anybody at the door.


Hardware Components

Arduino Mega

Ultrasonic Sensor-HC SR04


Jumper Wires

USB cable for Arduino

Software Components

Arduino IDE

Step 1: About Project

Ultrasonic Sensor

Ultrasonic sensor HC-SR04 is utilized here to recognize the presence of any person at the door. The sensor module involves an ultrasonic transmitter, receiver as well as a control circuit. It includes two circular eyes out of which one is utilized to transmit the ultrasonic wave and the other to receive it.

We can estimate the distance of the object depends on the time taken by the ultrasonic waves to return back to the sensor. With the help of formula:

Distance = (Time x Speed of Sound) / 2

Trigger pin of the ultrasonic sensor is attached to pin no. 12 of Arduino and Echo pin of sensor is attached to pin no 11 of Arduino. Vcc of the sensor is attached to the 5V pin of Arduino and the GND of the sensor is attached to the GND of Arduino. One pin of the buzzer is connected to the GND of Arduino and the other pin is connected to the 8th pin of Arduino.

Working of Project

Whenever anyone comes within the range of Ultrasonic Sensor, the microcontroller recognizes the distance of the object from the sensor and if the object is within the defined range, it sends the High signal to the buzzer and buzzer will start beeping.

We can test the entire circuit by simply putting anything in front of the sensor within a specific range.

IoT Training Online will help you to build Customized IoT Solutions.

Step 2: Run a Program


#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on ping sensor.

#define ECHO_PIN 11 // Arduino pin tied to echo pin on ping sensor.

#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). The maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

unsigned int pingSpeed = 50; // How frequently are we going to send out a ping (in milliseconds). 50ms would be 20 times a second.

unsigned long pingTimer;

int flag = 0; // Holds the next ping time.

void setup()


Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.

pingTimer = millis();

pinMode(10, OUTPUT); // Start now.

// Start now.


void loop()


// Notice how there are no delays in this sketch to allow you to do other processing in-line while doing distance pings.

if (millis() >= pingTimer) { // pingSpeed milliseconds since last ping, do another ping.

pingTimer += pingSpeed; // Set the next ping time.

sonar.ping_timer(echoCheck); // Send out the ping, calls "echoCheck" function every 24uS where you can check the ping status.


if (flag == 1)


digitalWrite(10, HIGH);


digitalWrite(10, LOW);


digitalWrite(10, HIGH);


digitalWrite(10, LOW);





digitalWrite(10, LOW);



void echoCheck() { // Timer2 interrupt calls this function every 24uS where you can check the ping status.

if (sonar.check_timer()) { // This is how you check to see if the ping was received.

// Here's where you can add code.

Serial.print("Ping: ");

Serial.print(sonar.ping_result / US_ROUNDTRIP_CM); // Ping returned, uS result in ping_result, convert to cm with US_ROUNDTRIP_CM.


if ((sonar.ping_result / US_ROUNDTRIP_CM) < 50)

flag = 1;

else if ((sonar.ping_result / US_ROUNDTRIP_CM) > 50)

flag = 0;