Security System Using Ultrasonic Sensor and Gsm Module

Published

Introduction: Security System Using Ultrasonic Sensor and Gsm Module

we used the following components:

· Aurdino Uno

· GSM Module,

· Ultrasonic Sensor

· Piezo Buzzer

· Breadboard and Jump Wires.

Step 1:

Step 2: CONNECTION OF ULTRASONIC SENSOR:

·

VCC -> Arduino +5V pin

· GND -> Arduino GND pin

· Trig -> Arduino Digital Pin 9

· Echo -> Arduino Digital Pin 10

Step 3: CONNECTION OF BUZZER:

·

s -> Arduino pin 8

·GND -> Arduino GND pin

Step 4: CONNECTION OF GSM:

GND -> Arduino GND pin

TX -> Arduino Digital Pin 2

RX -> Arduino Digital Pin 3

Step 5: Over View

At the first step, we are going to store the first distance as ‘Distance’ later in the loop we are going to check every time if the if the Distance is equal to the distance we got now if the distance is less than the distance we stored at the initial setup the trigger will goes on and the Buzzer will start buzzing and at the same time GSM module will send a message to the mobile number we given.

Step 6: Code ....

#include<SoftwareSerial.h>

#define trigPin 9

#define echoPin 10

#define piezoPin 8

SoftwareSerial mySerial(2, 3);

int normalDistance;

boolean triggered = false;

long duration, distance;

void setup()

{

mySerial.begin(9600);

Serial.begin (9600);

delay(100);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(piezoPin, OUTPUT);

digitalWrite(piezoPin, HIGH);

long duration, distance;

while (millis() < 5000)

{

digitalWrite(piezoPin, HIGH);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

normalDistance = distance;

Serial.print("Distance: ");

Serial.println(distance);

digitalWrite(piezoPin, LOW);

} }

void loop()

{

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

Serial.print("Distance: ");

Serial.println(distance);

if (distance < normalDistance-5)

{

triggered = true;

}

else

{

triggered = false;

}

if (triggered)

{

tone(piezoPin, 635);

delay(500);

tone(piezoPin, 912);

delay(250);

mySerial.println("AT+CMGF=1");

//Sets the GSM Module in Text Mode

delay(1000);

// Delay of 1000 milli seconds or 1 second mySerial.println("AT+CMGS=\"+91xxxxxxxxx\"\r");

// Replace x with mobile number

delay(1000);

mySerial.println("I am SMS from GSM Module");

// The SMS text you want to send delay(100);

mySerial.println((char)26);

// ASCII code of CTRL+Z

delay(1000);

} }

Share

    Recommendations

    • Planter Challenge

      Planter Challenge
    • Clocks Contest

      Clocks Contest
    • Casting Contest

      Casting Contest
    user

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

    Tips

    5 Questions

    how to set the distance? pls..

    i don`t want to use buzzer what change i have to made in code. please healp

    remove

    #define piezoPin 8

    and all piezoPin code where ever u find it remove that line

    How to store image in arudino and to send to mobile phone via GSM900 module ?

    please help us .....?

    23.png

    Hi,
    How would the code differ if you wanted to have two ultrasonic sensors?

    I don't know exactly...... we need 5v to run ultrasonic but we have only one 5v output if it is possible to connect both ultrasonic to one output of 5v there is a way to connect both trigger and echo pins of ultrasonic sensor to pin 9 and 10 so that there is no need to change code

    int trigPin = 9;



    int echoPin = 10;



    int led = 7;



    int led0 = 3;

     



    void setup() {



      Serial.begin(9600);



       pinMode(led, OUTPUT);

      

       pinMode (led0,OUTPUT);



       pinMode(trigPin, OUTPUT);



      pinMode(echoPin, INPUT);



      // put your setup code here, to run once:



     



    }



     



    void loop() {



      long duration, distance;



      digitalWrite(trigPin,HIGH);



      delayMicroseconds(10000);



      digitalWrite(trigPin, LOW);



      duration=pulseIn(echoPin, HIGH);



      distance =(duration/2)/29.1;



      Serial.print(distance);



      Serial.println("CM");



      delay(10);



    if((distance<=10))



      {



        digitalWrite(led, HIGH);

        digitalWrite(led,LOW);



    }



       else if(distance>10)



    {



         digitalWrite(led, LOW);

         digitalWrite(led0,HIGH);

       }



    int trigPin = 9;



    int echoPin = 10;



    int led = 7;



    int led0 = 3;

     



    void setup() {



      Serial.begin(9600);



       pinMode(led, OUTPUT);

      

       pinMode (led0,OUTPUT);



       pinMode(trigPin, OUTPUT);



      pinMode(echoPin, INPUT);



      // put your setup code here, to run once:



     



    }



     



    void loop() {



      long duration, distance;



      digitalWrite(trigPin,HIGH);



      delayMicroseconds(10000);



      digitalWrite(trigPin, LOW);



      duration=pulseIn(echoPin, HIGH);



      distance =(duration/2)/29.1;



      Serial.print(distance);



      Serial.println("CM");



      delay(10);



    if((distance<=10))



      {



        digitalWrite(led, HIGH);

        digitalWrite(led,LOW);



    }



       else if(distance>10)



    {



         digitalWrite(led, LOW);

         digitalWrite(led0,HIGH);

       }





    Sir in above programme i want msg using gsm modem 300
    If distance less than 10 cm how i can write programme

    If any one can able to write so pls do and send me to

    Sir pls writeup programme for me and mail to me pbhushan451@gmail.com

    19 Comments

    thankss bro its working

    Try one by one first only the buzzer later ultrasonic sensor then message you will get it

    the buzzer is always turn on w/c is very noisy even though there's no motion yet and also no sms is sent

    1 reply

    Try to change the code according to your need u can change the delay time

    This code is not run gets some error while compile

    1 reply

    it do worked for me have included the header file #include(SoftwareSerial.h)

    softwareserial myself (2, 3)' this error create problum

    bro what seems to be the problem?

    thanks for your tutorial in advance... but, could you tell me why the #include coding got error in arduino? what the coding used for? thanks

    5 replies

    For some reason it is not showing what iam typing in brakets u know how the #include works so
    Place
    SoftwareSerial.h
    Header file

    thank you so much :)

    For some reason it is not showing what iam typing in brakets u know how the #include works so
    Place
    SoftwareSerial.h
    Header file

    It looks good :) I need to set up a sensor on the garage door just so my husband will remember to close it, lol.

    1 reply