It is a simple tutorial on how to create little a security device using an Arduino.
Here we are going to make door alarm security system using HC-Ultrasonic sensor. The used ultrasonic sensor is used as distance sensor, it will tell us the distance at which the object is placed. Using this distance value, we will turn the buzzer on or off.
Components Required
- Arduino Uno
- Bread Board
- Ultrasonic Sensor- HC-SR04 (Generic)
- Buzzer
- Connecting Wire
Connections
The connections for the ultrasonic sensor with Arduino are as follows.
Ultrasonic Sensor | Arduino |
VCC | 5 v pin |
Trig Pin | Pin2 |
Echo pin | Pin 3 |
GND | GND |
Connect positive pinon buzzer with pin 10 on Arduino and Buzzer negative pin with GND pin on Arduino.
Working Mechanism
The ultrasonic sensor emits an ultrasonic wave from the trigger which comes back after hitting the object and it is received by the travelled in microsecond. To send an ultrasonic wave from the trigger, we will have to set trigger for 10 micro-second. It will send an 8 cycles sonic burst at 40 KHZ which will hit object and is then received by the echo.
Distance = v * t
Here we have value of t and we know that the speed of sound wave is 340 m/s. We have to convert speed of sound into cm/ micro-second to calculate distance. The speed of sound in cm/ micro-second is 0.034 cm/ micro-second. Then,
S= 0.034 * t
We require only the distance it take to hit the object so,
S=(0.034*t)/2
We will get the distance value using equation above and after that we will set a value which help us to make the buzzer high or low.
Code:
int trigger_pin = 2;
int echo_pin = 3;
int buzzer_pin = 10;
int time;
int distance;
void setup ( ) {
Serial.begin (9600);
pinMode (trigger_pin, OUTPUT);
pinMode (echo_pin, INPUT);
pinMode (buzzer_pin, OUTPUT);
}
void loop ( ) {
digitalWrite (trigger_pin, HIGH);
delayMicroseconds (10);
digitalWrite (trigger_pin, LOW);
time = pulseIn (echo_pin, HIGH);
distance = (time * 0.034) / 2;
if (distance <= 10)
{
Serial.println (" Door Open ");
Serial.print (" Distance= ");
Serial.println (distance);
digitalWrite (buzzer_pin, HIGH);
delay (500);
}
else {
Serial.println (" Door closed ");
Serial.print (" Distance= ");
Serial.println (distance);
digitalWrite (buzzer_pin, LOW);
delay (500);
}
No comments:
Post a Comment