Distance sensor with led and active buzzer

DISTANCE SENSOR WITH LED AND
ACTIVE BUZZER
INTRO:

Here we gonna learn about HC-SR04 with Arduino Tutorial.........................
Ultrasonic sensor that can measure distance.Range of the ultrasonic sound is 40KHz (40,000Hz) it emits an ultrasound which travels through the air and if there is any obstacle (object)it will bounce to the moudle .The most interesting things if we consider the travel time and speed of the sound we can calculate the distance.

Pin of  HC-SR04 is VCC(1),TRIG(2),ECHO(3),AND GND(4). The supply voltage of VCC is +5v and
we can attach TRIG and ECHO pin to any Digital I/O in your Arduino Board .

In this two picture we can see how HC-SR04 work and the pin of ultrasonic sensor.






THINGS REQUIRED:

  • Ardunio Uno 
  • Ultrasonic Sensor HC-SR04
  • male to male jumper
  • breadboard
  • led
  • active buzzer   

In order to generated the ultrasound we need to set the Trigger Pin  on a high state for 10 microsecond.
that will send out an 8 cycle sonic brust which will travel at the speed sound and it will be recived in the Echo Pin .The Echo pin will OUTPUT the time in microseconds the sound wave traveled.

FORMULA FOR DISTANCE CALCULATING:

Speed of sound:
v= 340m/s
v= 0.034cm/microseconds
Time= Distance/Speed
t=s/v=10/0.034
         
s=t*0.034/2


In the above example ,if the object is 10cm away from the sensor , and the speed of the sound 240m/s the sound wave will need to travel about 417 microseconds . But what we will get from the Echo Pin will  double that number because the sound travel forward and bounce backward .So in order to get the distance in cm we need to multiply the received travel time value from the echo pin by 0.024 and divided it by 2 .




code:


const int echo=11;
const int trig=10;
const int ledPin=13;
const int buzzerPin=12; 

int duration=0;
int distance=0;
void setup() {
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
pinMode(
buzzerPin,OUTPUT);
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(trig,OUTPUT);
delayMicroseconds(1000);
digitalWrite(trig,LOW);

duration=pulseIn(echo,HIGH);
distance=(duration/2)/28.5;
Serial.println(distance);

if(distance<=14){

tone(buzzerPin,100);
digitalWrite(ledPin,HIGH);
delay(500);
noTone(buzzerPin);
digitalWrite(ledPin,LOW);
delay(100);
Serial.println("--------ALARM ACTIVATED------");
}

else{
  noTone(buzzerPin);
  digitalWrite(ledPin,LOW);

  Serial.println("ALARM DEACTIVATED");
}

}

For the programming Code, first, we have to define the Echo Pin and Trigger Pin that connect with the Arduino board. In this project, the Echo pin is attached to D11  and Trig pin D10  Connect led with the pin  D13  buzzer pin with D12 Then defines the variables for the distance (int) and duration (long).
In the loop, we have to make sure that the pin is clear so we have to set the pin on a Low State for just 2 microseconds. Now for generating the Ultrasound wave we have to set the trig pin on HIGH State for 10 microseconds Using pulsIN() function you have to read the travel time and put that value into the variable "duration", .this function has 2 parameters, the first one is the name of echo pin and for the second one, you write either HIGH or LOW. In this case, HIGH means that the pulseln() function will wait for the pin to LOW when the sound wave will end which will stop the timing. At the end of the function will return the length of the pulse in the microseconds. For getting the distance we will multiply the duration by 0.024 and divided by 2 as we will discuss this in above. In the end, we will print the value of the distance on the serial monitor. when an object came in the path of the sensor's active buzzer gets ON and LED started blinking.


































Comments

Popular Posts