SIMPLE ARDUINO ULTRASONIC SENSOR DOOR ALARM / Motion Detector NO PIR !!!!

72,970

296

35

Posted

Introduction: SIMPLE ARDUINO ULTRASONIC SENSOR DOOR ALARM / Motion Detector NO PIR !!!!

HELLO EVERYONE.

before i start, be sure to check my video if you prefer a video tutorial

Click here to go to my channel for more awesome projects

Today I decided i wanted to build an alarm for my room, when I searched online all door alarms used the PIR sensor, unfortunately mine was faulty.

and that"s when I decided to build my own alarm using an ultrasonic sensor (HC-SR04),and I found out it was really simple.

In this instructable i will walk you through the build an explain the code.
if you have any question make sure to leave a comment or e-mail me at (ahmedyasserahmed@gmail.com) and be sure that i will answer.
ENJOY, AND DON'T FORGET TO SUBSCRIBE AND FAVORITE THIS, Its just a button but will help me allot ,Thank you
MATERIALS REQUIRED:-
1- ARDUINO ( i used Arduino uno REV. 3)

2-HC-SR04 Ultrasonic Sensor(http://www.ebay.com/itm/1pcs-Ultrasonic-Module-HC-SR04-Distance-Measuring-Transducer-Sensor-for-Arduino-/261009210866?pt=LH_DefaultDomain_0&hash=item3cc55c7df2)

3- A Piezo/Speaker/Buzzer something like this( http://www.ebay.com/itm/2-x-PIEZO-ELECTRONIC-TONE... )

4-a few jumper wires

5- a breadboard

Step 1: Connecting the Sensor !!

connect the Vcc to Vin, GND to GND, Trigger pin to pin 12, Echo pin to pin 13,
Note:- if you are using a 9v battery i found out that youhave to connect the Vcc pin of the sensor to 5v ,while if you are using your computer as a power source you have to connect the Vcc pin to the Vin of your arduino

Step 2: Connect the Piezo/Speaker

for this you can either use a piezo buzzer or an 8-ohm speaker, Basically connect ground to ground and power to pin 8

Step 3: THE CODE !!!!(......)!!!!

i will be breaking down the code and explaining it in details, again any questions are welcome, code is attached in this page!!

CODE:-

#define trigPin 12
#define echoPin 13
int Buzzer = 8;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(Buzzer, OUTPUT);
}
EXPLANATION:-
we define the trigger pin as pin 12, the echo pin as pin 13 and the piezo (buzzer) as pin 8,Then we define the trigger and piezo (buzzer) pins as output, the echo pin as input.

CODE:-

void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

EXPLANATION:-
so the way this ultrasonic sensor works is:
when we flash the trigger pin high for a small amount of time (in this case 1000 microseconds), the sensor would send an ultrasonic wave at a known time (let's say t1), the wave will reach the object and reflect back to the sensor at another known time (t2) , lets assume t3 =t2 - t1, (t3 is equal to the time taken for the wave to reach the object and comeback, so t3/2 is the time needed for the wave to reach the object) we know the speed of sound which is 340 m/s or 29.1cm/ms so we are able to get the distance in cm

CODE:-
if (distance >= 80 || distance <= 0){
Serial.println("no object detected");
digitalWrite(Buzzer, LOW);
]
EXPLANATION:-
my doors length is 80 cm (use ur own value) so as long as there is nothing less than 80 cm away the buzzer is off onc something passes this will cause the buzzer to sound

CODE:-
else {
Serial.println("object detected");
tone(Buzzer, 400); // play 400 Hz tone for 500 ms
delay(500);
tone(Buzzer, 800); // play 800Hz tone for 500ms
delay(500);
tone(Buzzer, 400); // play 400 Hz tone for 500 ms
delay(500);
tone(Buzzer, 800); // play 800Hz tone for 500ms
delay(500);
tone(Buzzer, 400); // play 400 Hz tone for 500 ms
delay(500);
tone(Buzzer, 800); // play 800Hz tone for 500ms
delay(500);
noTone(Buzzer);
}
delay(300);
}
EXPLANATION:-
this is just a tone similar to a police siren !!

**I am sorry i couldn't upload the code so please copy it, there you go:-**
THE CODE STARTS HERE
/*
HC-SR04 Door Alarm
Code edited by :- Ahmed Yasser
*/
/*Circuit Connections:-
Trigger pin to pin 12
Echo pin to pin 13
piezo Buzzer to pin 8
Ground pin of piezo and sensor to ground
Note:- if you are using a 9v battery i found out that you
have to connect the Vcc pin of the sensor to 5v ,while if
you are using your computer as a power source you have to
connect the Vcc pin to the Vin of your arduino
Hope you find this helpfull
for more info. visit my instructable account at:-
https://www.instructables.com/member/ahmedyasser/
*/



#define trigPin 12
#define echoPin 13
int Buzzer = 8;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(Buzzer, OUTPUT);
}

void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 80 || distance <= 0){
Serial.println("no object detected");
digitalWrite(Buzzer, LOW);

}
else {
Serial.println("object detected");
tone(Buzzer, 400); // play 400 Hz tone for 500 ms
delay(500);
tone(Buzzer, 800); // play 800Hz tone for 500ms
delay(500);
tone(Buzzer, 400); // play 400 Hz tone for 500 ms
delay(500);
tone(Buzzer, 800); // play 800Hz tone for 500ms
delay(500);
tone(Buzzer, 400); // play 400 Hz tone for 500 ms
delay(500);
tone(Buzzer, 800); // play 800Hz tone for 500ms
delay(500);
noTone(Buzzer);
}
delay(300);
}
THE CODE ENDS HERE:-

HOPE YOU ENJOYED THIS INSTRUCTABLE, DONT FORGET TO HIT THAT FAVOURITE BUTTON AND U'VE GOT NO EXCUSE NOT TO SUBSCRIBE FOR MORE, THIS SHOWS GREAT SUPPORT FOR ME !!

2 People Made This Project!

Recommendations

  • Remote Control Contest 2017

    Remote Control Contest 2017
  • Arduino Contest 2017

    Arduino Contest 2017
  • LED Contest 2017

    LED Contest 2017

We have a be nice policy.
Please be positive and constructive.

Tips

Questions & Answers

48 Comments

Thank you so much!!! You saved my group's life

Did u add pir sensor?

How abt adding pir sensor to it!!

Hi

I would like to ask How to use ultrasonic sensor for objection detection using proteus??? Virtual terminal shows only "object not detected"

Kindly Help!!!

Hi, can we make it with arduino nano?

You are truly amazing

why my buzzer keep making sound non stop,although ive change the distance?please help mee!!!