Ultra Sonic Sensor HC-SR04

HC-SR04 Ultra Sonic Sensor

Ultra Sonic Sensor HC-SR04 is a sensor which is used for measuring distance, this Ultra Sonic Sensor HC-SR04 could be used in different types of projects for example it could be used for Obstacle avoiding robot measuring water level in water tank. Ultra Sonic Sensor HC-SR04 sensor has got 4 pins, those are VCC which need to be connected with 5 volts, Trig which is trigger which is needed to be defined as output, this pin is need to be defined as Input, and GND which is ground and needed to be connected with ground of Arduino. Ultra Sonic Sensor HC-SR04 working is as follows with Arduino, when trigger pin is set high ultra sonic sound wave is emitted from the sensor this wave then travels some distance hits the objects and returns back, when this signal returns back Echo pin gives high input to Arduino meanwhile Arduino keeps on measuring the duration from since the Trigger pin of Ultra Sonic Sensor HC-SR04 has turned high and then low till when Echo pin gives high output. This duration which Arduino has recorded is the time taken for Ultrasonic sound wave to travel towards the object hit it and then return back, so in order to measure distance between object and Ultra Sonic Sensor HC-SR04 we need to divide this duration by two and multiply it with speed of sound which is 340m/s, or in case of this video where we are interested in measuring distance in centimeters, speed will be 34000 cm/s * time in micro secs.

To see how Ultra Sonic Sensor HC-SR04 you can also watch the video.



So that is how Ultra Sonic Sensor HC-SR04 is working. code is given below which you could use for programming you Arduino board.

#include <LiquidCrystal_I2C.h>               // Working with I2C LCD

LiquidCrystal_I2C lcd(0x27, 16, 2);          // Defining LCD
#define echoPin 2                                       // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3                                         //attach pin D3 Arduino to pin Trig of HC-SR04
                                                                    // defines variables
long duration;                                              // variable for the duration of sound wave travel
int distance;                                                 // variable for the distance measurement
void setup() 
{
  pinMode(trigPin, OUTPUT);            // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT);              // Sets the echoPin as an INPUT
  lcd.init();                                            // Initializing LCD
  lcd.backlight();                                   // Enabling Back Light
}
void loop() 
{
  digitalWrite(trigPin, HIGH);                 // Sets the trigPin HIGH (ACTIVE) 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);    // Reads the echoPin, returns the sound wave travel time in microseconds
                                                                // Calculating the distance
  distance = (duration * 0.034) / 2;         // Speed of sound wave divided by 2 (go and back) in cm 340*100 = 34000 / 1000000 = 0.034
                                                                // Displays the distance on the Serial Monitor
  lcd.setCursor(0,0);                        
  lcd.print("Distance: ");         
  lcd.print(distance);         
  lcd.print(" cm");
  delay(200); 
  lcd.clear();          
}

NOTE :  I am using I2C LCD so I have added its library in my code if you are using another LCD use library for it accordingly.

Comments

Popular posts from this blog

DS3231 Real Time Clock Interfacing With Arduino

FM Transmitter Project

4 Way Traffic Signal Using Flip Flops