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

90,492

308

45

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

About: just another electronics hobbiest trying to help you out

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 !!

5 People Made This Project!

Recommendations

  • For the Home Contest

    For the Home Contest
  • Game Design: Student Design Challenge

    Game Design: Student Design Challenge
  • Make It Bridge

    Make It Bridge

45 Comments

0
HandinaD
HandinaD

5 years ago

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

0
ShabuS1
ShabuS1

Reply 5 years ago

Did u add pir sensor?

0
ShabuS1
ShabuS1

5 years ago

How abt adding pir sensor to it!!

0
AbdulAziz AhmedS
AbdulAziz AhmedS

6 years ago

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!!!

0
sidaqdeep1
sidaqdeep1

6 years ago

Hi, can we make it with arduino nano?

0
Archit B
Archit B

6 years ago

You are truly amazing

0
MuhammadA420
MuhammadA420

7 years ago

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

0
Tejasm_4
Tejasm_4

7 years ago

My serial monitor is reading everything ok, but the buzzer is not sounding. Is that because i am using i different buzzer? My buzzer is the Active Buzzer. Pls help i really want to get this!

0
Tejasm_4
Tejasm_4

7 years ago

where is the code i am not able to find the code

0
UmarA8
UmarA8

7 years ago

Hi, if I'm using 3 units ultrasonic sensor and only 1 buzzer on my car remote control, while my car is on move I only need range within below 10 cm. Can I get a new coding for this?

0
UmarA8
UmarA8

7 years ago

Hi, if I'm using 3 units ultrasonic sensor and only 1 buzzer on my car remote control, while my car is on move I only need range within below 10 cm. Can I get a new coding for this?

0
UmarA8
UmarA8

7 years ago

Hi, if I'm using 3 units ultrasonic sensor and only 1 buzzer on my car remote control, while my car is on move I only need range within below 10 cm. Can I get a new coding for this?

0
UmarA8
UmarA8

7 years ago

Hi, if I'm using 3 units ultrasonic sensor and only 1 buzzer on my car remote control, while my car is on move I only need range within below 10 cm. Can I get a new coding for this?

0
UmarA8
UmarA8

7 years ago

Hi, if I'm using 3 units ultrasonic sensor and only 1 buzzer on my car remote control, while my car is on move I only need range within below 10 cm. Can I get a new coding for this?

0
UmarA8
UmarA8

7 years ago

Hi, if I'm using 3 units ultrasonic sensor and only 1 buzzer on my car remote control, while my car is on move I only need range within below 10 cm. Can I get a new coding for this?

0
UmarA8
UmarA8

7 years ago

Hi, if I'm using 3 units ultrasonic sensor and only 1 buzzer on my car remote control, while my car is on move I only need range within below 10 cm. Can I get a new coding for this?

0
UmarA8
UmarA8

7 years ago

Hi, if I'm using 3 units ultrasonic sensor and only 1 buzzer on my car remote control, while my car is on move I only need range within below 10 cm. Can I get new coding for this?

0
konosuke
konosuke

7 years ago

Great! Maybe I can connect it to the Raspberry Pi and send alert via e-mail or something.