How to make a Security alarm using Arduino Uno with Ultra sonic sensor
An ultrasonic sensor is an electronic device that measures the distance of a target object by emitting ultrasonic sound waves, and converts the reflected sound into an electrical signal. Ultrasonic waves travel faster than the speed of audible sound (i.e. the sound that humans can hear). Ultrasonic sensors have two main components: the transmitter (which emits the sound using piezoelectric crystals) and the receiver (which encounters the sound after it has travelled to and from the target).
In order to calculate the distance between the sensor and the object, the sensor measures the time it takes between the emission of the sound by the transmitter to its contact with the receiver. The formula for this calculation is D = ½ T x C (where D is the distance, T is the time, and C is the speed of sound ~ 343 meters/second). For example, if a scientist set up an ultrasonic sensor aimed at a box and it took 0.025 seconds for the sound to bounce back, the distance between the ultrasonic sensor and the box would be:
D = 0.5 x 0.025 x 343 |
or about 4.2875 meters.
Now we move to our Project which is "Security alarm using Arduino Uno with Ultra sonic sensor".
Components Qty
Arduino Uno Board 01
Breadboard 01
Jumper Cables 08
The HC-SR04 ultrasonic sensor 01
LED 01
(you can get any color as you wish)
Active buzzer 01
Circuit Diagram
1st step prepare a Arduino Uno board
Ultra sonic sensor has 4 pins. Name as vcc,trigger pin, echo pin, Ground pin. In this circuit vcc pin connect to 5v pin in Arduino, And also other connectors are given below.
Ultrasonic sensor pins Arduino board pins(which connect with Uno board)
vcc pin 5v pinn
Echo pin Digital 10 Pin
Trigger pin Digital 9 Pin
Ground pin GND pin
The real view of circuit
2nd step prepare bread board
Now we should connect buzzer and ultra sonic sensor to bread board via male to male jumper cables.
3rd step Codding ant upload code to Arduino board
Arduino Code
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;
// defines variables
long duration;
int distance;
int safetyDistance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
safetyDistance = distance;
if (safetyDistance <= 20) // You can change safe distance from here changing value Ex. 20 , 40 , 60 , 80 , 100, all in cm
{
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
}
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
Now execute your program and have a fun
No comments:
Post a Comment