Vidit ShahUtkarsh Tiwari
Published © GPL3+

Ultrasonic Sensor Alarm

This is a simple project to make an alarm system using an ultrasonic sensor, buzzer, and LED.

BeginnerProtip1 hour105,267
Ultrasonic Sensor Alarm

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
HC-SR04 Ultrasonic Distance Measuring Sensor Module
×1
Buzzer
Buzzer
×1
LED (generic)
LED (generic)
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
MALE TO MALE & MALE TO FEMALE
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic

Code

Ultrasonic_Alarm.ino

Arduino
/* This simple project describes how to make an ultrasonic alarm system using 
LED, Ultasonic Sensor(HC-SR04) and a buzzer.*/ 

//Firstly the connections of ultrasonic Sensor.Connect +5v and GND normally and trigger pin to 12 & echo pin to 13. 

#define trigPin 12
#define echoPin 13
int Buzzer = 8; // Connect buzzer pin to 8
int ledPin= 6;  //Connect LEd pin to 6
int duration, distance; //to measure the distance and time taken 

void setup() {
        Serial.begin (9600); 
        //Define the output and input objects(devices)
        pinMode(trigPin, OUTPUT); 
        pinMode(echoPin, INPUT);
        pinMode(Buzzer, OUTPUT);
        pinMode(ledPin, OUTPUT);
}

void loop() {

    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2) / 29.1;
    //when distance is greater than or equal to 200 OR less than or equal to 0,the buzzer and LED are off
  if (distance >= 200 || distance <= 0) 
        {
        Serial.println("no object detected");
        digitalWrite(Buzzer,LOW);
        digitalWrite(ledPin,LOW);
        }
  else {
        Serial.println("object detected \n");
        Serial.print("distance= ");              
        Serial.print(distance);        //prints the distance if it is between the range 0 to 200
        tone(Buzzer,400);              // play tone of 400Hz for 500 ms
        digitalWrite(ledPin,HIGH);
  }
}

Credits

Vidit Shah

Vidit Shah

10 projects • 39 followers
Programmer
Utkarsh Tiwari

Utkarsh Tiwari

10 projects • 40 followers
Trying to FIND myself! :P Interested in DIY's and coding!

Comments