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

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

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

Comments

harshvardhanraj37 (author)2017-07-05

veryyyyyyyyy good

HandinaD (author)2017-04-10

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

AbdulAziz AhmedS (author)2017-01-09

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

sidaqdeep1 (author)2016-09-24

Hi, can we make it with arduino nano?

Archit B (author)2016-04-12

You are truly amazing

Lintang_Wisesa made it! (author)2016-04-04
MuhammadA420 (author)2016-01-25

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

Tejasm_4 (author)2016-01-04

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!

Tejasm_4 (author)2016-01-04

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

UmarA8 (author)2016-01-02

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?

UmarA8 (author)2016-01-02

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?

UmarA8 (author)2016-01-02

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?

UmarA8 (author)2016-01-02

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?

UmarA8 (author)2016-01-02

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?

UmarA8 (author)2016-01-02

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?

UmarA8 (author)2016-01-02

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?

UmarA8 (author)2016-01-02

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?

konosuke (author)2015-12-27

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

diymaster44 (author)2015-12-26

Hi, I would like to not use a fixed distance value, but compare the last measurement to the latest measurement, and if the distance has changed more than for example 5cm it should trigger the alarm. How would the code look like? I tried this but it didn't work reliably:

newdistance = (distance/olddistance)*100;
olddistance = distance;
if (newdistance >= 50 || distance <= 0){

kmutema19 (author)2015-11-15

can you make like a circuit diagram because your photo image of the wires is upside down make better photos please

Anand Kachale (author)2015-10-30

can anyone please tell me ...

what can i do to make the same WATERPROOF!!! please help me.

Anugreh (author)2015-10-25

Arduino: 1.6.5 (Windows 8), Board: "Arduino/Genuino Uno"

Sketch uses 4,994 bytes (15%) of program storage space. Maximum is 32,256 bytes.

Global variables use 243 bytes (11%) of dynamic memory, leaving 1,805 bytes for local variables. Maximum is 2,048 bytes.

avrdude: ser_open(): can't open device "\\.\COM1": The system cannot find the file specified.

Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#up... for suggestions.

This report would have more information with

"Show verbose output during compilation"

enabled in File > Preferences.

I am getting this error! What to do?

AY Electronics (author)Anugreh2015-10-28

what board are you using?

LawM1 (author)2015-09-25

Is it okay to use a 5 V 4 Ohms speaker?
or if 8 ohms, what is the possible voltage for the speaker?

Skiept made it! (author)2015-09-11

Simple and useful :) thanks for the tutorial

AY Electronics (author)Skiept2015-09-11

U r more than welcome, thnx for the feedback

nguyentruong17 (author)2015-05-29

That amazing

http://www.last.fm/user/nguyentruong17

phil.pickering.9 (author)2015-04-21

Hi

I am using a Piezo buzzer without the wires, just the points like this,

http://www.hobbytronics.co.uk/arduino-tutorial7-pi...

when I connected everything it just beeps continually, any ideas please

I may be completely oblivious to your specific circumstance, but it sounds like the object in front of the sensor may be too close, triggering the alarm.

i donot see a reason for it not to work.

are you sure you copied the code correctly

and can you provide me with the specs you know about your piezo buzzer

sorry for the late reply

dalmasetto (author)2015-03-24

Hi there,

Would it be possible for this alarm to trigger a recordable sound module instead of a piezo? Something like this -> http://www.ebay.com.au/itm/141147691353?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

hey,
yes u can all u have to do is
1_remove the last part of the code abot the note and replace it by digital write high
2_use a transistor to control the power reaching the sound recording module where its base ia connected to the digital pin

Thank you for the prompt reply!

I am very new to programming. Can you give me an example of what the code would look like to trigger the sound module?

Also, what type of transistor would I need to use to control the power? I intend to sun the project on a 9v battery as it needs to be portable.

Thank you in advance for any advice you can offer.

hi again
sorry for the late reply
for the code the last part in the else after the serial prind delete everything and write a digital write.
if u want me to write the code for u tell me
and for the transistor any npn transistor should do it maybe a bc547 and u should connect the base throigh a 10k resistor to the pin specified by the digital write then collector to power supply and emitte to the module

Thank you very much for the reply. Do you think the code would look like this? I am very new to this!

else {

Serial.println("object detected");

digitalWrite(Buzzer, HIGH);

}

yes that seems fine

OK, so just to confirm you think that the following code would work for my project.

____________________________

#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");

digitalWrite(Buzzer, HIGH);

}

delay(300);

}

______________________

Also, as my project needs to be small and portable I am thinking of using an arduino nano. Would it work the same on that unit?

Thank you once again for your assistance.

you should add a delay after the digital write buzzer high with the amount of time you want the alarm to stay on for
otherwise it seems fine

Could you please show me what that code would look like?

#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");

digitalWrite(Buzzer, HIGH);
delay (2000);//change this value depending on how long you want the module to stay on

}

delay(300);

}

amnamrza901 (author)2014-07-01

i attached with laptop windows7 y is this matters for ultrasonic sensor? please tell me thank u

Hi,

is the vcc pin of the ultrasonic sensor connected to 5v or the Vin of the arduino?

ok thank u so much my circuit is now working good but i had to attach 2 black funnels so it can work properly thank u

amnamrza901 (author)2014-06-30

ok i have a question that is so stupid but please tell me i want a range of 400 cm means more then 10 feet i have object from 9 feet to 11 feet i put all code like above and made if condition but my circuit don't get the object all the time i have to take a path so it can get me from anywhere and sometime it don't get object at all what is wrong i means can't it work for 9 to maximum 10.5 feet please tell me

Hi,

I think I have an idea of what is wrong

what are you using to power your arduino

About This Instructable

70,010views

295favorites

License:

Bio: just another electronics hobbiest trying to help you out
More by AY Electronics:Dark Sensor(LDR-Transistor Circuit/Automatic Light)||EXPLAINED IN DETAILS {EASY!!}READ THIS BEFORE BUYING AN ARDUINO!!LED music sync / how to make LEDs dance to music [EASY!!]
Add instructable to: