Introduction: HC-SR04 Based Door Unlock Detection System

Lately I have replaced my smoke alarm, the buzzer still operates (at least it makes enough noise even though there is no smoke… haha). I decided to adapted it so it can be a door opening detector. Well it could be a cheap alternative to all the expensive door opening alarm system on the market... you can change the buzzer with a regular one, the architecture and the program will stay the same, even though the one i used is noisy enough to make any intruder changed his mind. Using an Ultrasonic Ranging Module HC-R04 I was able to detect the presence of the door doorknob.

Step 1: What You Will Need...

To build this project you need:

  • An Arduino UNO or compatible.
  • 1 buzzer in my case i have used a smoke alarm buzzer.
  • Ultrasonic Ranging Module HC-SR04
  • A mini breadboard.
  • Some male to male jumper wires

Step 2: Preparing the Buzzer...

I removed the battery of the smoke detector and replace it with DIY 9 volt battery connector, attached to two male jumper wires, as shown in the picture

Warning: some of the smoke alarm contains radioactive material such as the AMERICIUM 241. Precaution is needed when manipulating them.

Step 3: Connecting the HC-SR04

the HC-SR04 connection is given in the pictures above.

Step 4: Programming the Arduino

Past the following program into your Android software, and transfer it to your Arduino UNO


HC-SR04 Ping distance sensor:

VCC to arduino 5v

GND to arduino GND

Echo to Arduino pin 7

Trig to Arduino pin 8


#define echoPin 7 // Echo Pin

#define trigPin 8 // Trigger Pin

#define BuzzerPin 13 // Onboard LED, Buzzer

int maximumRange = 200; // Maximum range needed

int minimumRange = 0; // Minimum range needed

int secumaximumRange = 10; // secure Maximum range needed

int secminimumRange = 0; // secure Minimum range needed

long duration, distance; // Duration used to calculate distance

void setup() {

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)


void loop() {

/* The following trigPin/echoPin cycle is used to determine the

distance of the nearest object by bouncing soundwaves off of it. */

digitalWrite(trigPin, LOW);


digitalWrite(trigPin, HIGH);


digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.

distance = duration/58.2;

if (distance >= maximumRange || distance <= minimumRange){

/* Send a negative number to computer using Serial protocol.*/



else {

/* Send the distance to the computer using Serial protocol. */



if (distance >= secumaximumRange || distance <= secminimumRange){

digitalWrite(BuzzerPin, HIGH);


else {

digitalWrite(BuzzerPin, LOW);


//Delay 50ms before next reading.



Step 5: Installing the HC-SR04 Module

Place the HC-SR04 near your doorknob, it should measure a distance lower than 10cm, this distance can be read in the serial monitor of the Arduino software. you can change this distance in the program if you need to.

Step 6: Testing...