Ultrasonic Interfacing






In this tutorial we are going to interface ultrasonic distance sensor HC-SR04 (proximity sensor) with Arduino and LCD. Ultrasonic sensor is used to measure the distance. It is also known as the proximity sensor or the sonar sensor. It sends an ultrasonic wave (sound wave) which comes back after hitting the object and tells us the time traveled by it. By using this time we calculate the distance traveled. The working of distance sensor is explained briefly in this tutorial.

Circuit Diagram and Explanation

Ultrasonic distance sensor has four pins
VCC: Connect that to 5V pin of Arduino.
Trig: Connect that to pin 2 of Arduino.
Echo: Connect that to pin 3 of Arduino.
GND: Connect that to ground of Arduino









Working of Ultrasonic distance sensor HC-SR04

The ultrasonic distance sensor HC-SR04 emits an ultrasonic wave from the trigger and it is received by the echo after deflecting an object. In order to generate a wave, we will have to set the trigger at high for 10 us which will send a 8 cycle sonic burst at 40 KHz which will hit the object and after hitting the object, the wave will be received by the echo. The echo will then tell us the time that the wave have traveled in us (micro seconds).
We will have to convert this time into cm to calculate the distance traveled. We will use the following equation to calculate the distance.

S = v * t

Ultrasonic wave is basically a sound wave which travels at a speed of 340 m/s. To calculate the distance we will have to convert speed into cm/us which is 0.034 cm/us. The ultrasonic sensor is measuring the time it takes to hit the object and then come back but we need only time that it takes to hit the object. So, we will divide it by 2. The equation will become

S = (t * 0.034)/2

To calculate the distance in inches, we will have to convert the cm in inches. One cm is equal to 2.54 inches. So, we will divide the 0.034 cm/us by 2.54 to convert it into inches/us. To calculate the distance in inches, the equation will become

S = (t * 0.0133)/2


Code

In the following code, we are going to interface the distance sensor with Arduino and display the distance on the serial monitor.

#include           //Including the LiquidCrystal Library
LiquidCrystal lcd(12,11,10,9,8,7); 
const int triggerpin = 2;     //Connect the trigger pin at pin 2
const int echopin = 3;        //Connect the echo pin at pin 3
long time;                    //Variable for storing the time traveled
int S;                        //Variable for storing the distance covered
int inch;                     //Variable for storing the distance in inches
void setup() {
lcd.begin(16,2);              //Initializing the interface of the LCD
pinMode(triggerpin, OUTPUT);  //Setting the triggerpin as output pin
pinMode(echopin, INPUT);      //Setting the echo pin as input pin
Serial.begin(9600);       
}
void loop() {
lcd.clear();
digitalWrite(triggerpin, LOW);  
delayMicroseconds(2);
digitalWrite(triggerpin, HIGH); //Setting the triggerpin high for 10us to generate a wave
delayMicroseconds(10);
digitalWrite(triggerpin, LOW);
time = pulseIn(echopin, HIGH); //Setting the echopin high to receive the wave
S= time*0.034/2;                //Calculating the distance traveled in cm
inch = time*0.0133/2;           //Calculating the disatnce traveled in inches
lcd.setCursor(0,0);             // Sets the location at start
lcd.print("Distance: "); 
lcd.print(S); 
lcd.print(" cm");
delay(10);
lcd.setCursor(0,1);
lcd.print("Distance: ");
lcd.print(inch);
lcd.print(" inch");
delay(1000);
}

0 Comments