Experiment-2: Aim:Calculate The Distance To An Object With The Help of An Ultrasonic Sensor and Procedure
Experiment-2: Aim:Calculate The Distance To An Object With The Help of An Ultrasonic Sensor and Procedure
Aim:Calculate the distance to an object with the help of an ultrasonic sensor and
display it on an LCD
Procedure:
Inthisexperiment, wearegoingtointerfaceUltrasonicsensorHC-SR04withArduino and
LCD Display. The ultrasonic sensor is used to measure the distance. It acts as a Sonar.
It sends an ultrasonic wave of a certain frequency that comes backafterhitting the
object and calculates the time traveled by it. So let’s learn about Distance
Measurement Using Arduino & HC-SR04 Ultrasonic Sensor.
ComponentsRequired:
1. ArduinoUno Board
2. UltrasonicSensorHC-SR04
3. 16*2LCDDisplay
4. Breadboard
5. ConnectingWires
6. 5VPowerSupply
UltrasonicSensorHC-SR04:
Description:
The HC-SR04 ultrasonic sensor uses sonar to determine the distance to an object like
bats do. It offers excellent non-contact range detection with high accuracy and stable
readings in an easy-to-use package.
DistanceMeasurementUsingArduino&HC-SR04
From 2cm to 400 cm or 1” to 13 feet. Its operation is not affected by sunlight or black
materiallikesharprangefindersare(althoughacousticallysoftmaterialslikecloth can be
difficult to detect). It comes complete with the ultrasonic transmitter and a receiver
module.
ThespecificationsoftheultrasonicdistancesensorHC-SR04arebelow:
1. Minimummeasuringrange–2cm
2. Maximummeasuringrange:400cmor4meter
3. Accuracy:3mm
4. OperatingVoltage:+5V
5. OperatingCurrent:15mA
6. WorkingFrequency:40KHz
7. TriggerInputsignal:10uspulse
8. Measuringangle:15degrees
Pins:
1. VCC:+5VDC
2. Trig:Trigger(INPUT)
3. Echo:Echo(OUTPUT)
4. GND:GND
WorkingProcedure:
Wewillhavetoconvertthistimeintocmtocalculatethedistancetraveled.Wewill
usethefollowingequationtocalculatethedistance.
S=v*t
The ultrasonic wave is basically a sound wave that travels at a speed of 340 m/s
(0.034 cm/s). 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.
circuitDiagramandConnections
Trig and Echo pins of the ultrasonic sensor are connected to digital pin 3& 2 of
Arduino. VCCpin of the ultrasonic sensor is connected to the 5v pin of Arduino while
theGNDpinisconnectedtotheGNDofArduino.SDA&SCLpinoftheI2Cmodule is connected
to the A4& A5 pin of Arduino while V CCand GND pins are connected to the 5V & GND
pin of Arduino.
CopythiscodethencompileanduploadittoyourArduinoboard. Arduino
Code:
#include<Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2Clcd(0x27,16,2); const
int trigPin = 3;
constintechoPin=2; long
duration;
int distance;
voidsetup(){
}
void loop() {
lcd.clear();
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);duration
= pulseIn(echoPin, HIGH); distance
= duration * 0.0340 /
2;Serial.println("Distance");
Serial.println(distance);
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print("cm");
delay(1000);
}
Output: