Exp. No.: 4 Date: Arduino Based Ultrasonic Distance Sensor AIM
Exp. No.: 4 Date: Arduino Based Ultrasonic Distance Sensor AIM
1. Arduino UnO
2. Ultrasonic Sensor module (HC-SR04)
3. Breadboard
4. 9V Power supply
5. USB cable
6. 16x2 LCD
7. Connecting wires
DESCRIPTION
This application is based upon the reflection of sound waves. Sound waves are defined as
longitudinal pressure waves in the medium in which they are travelling. Subjects whose
dimensions are larger than the wavelength of the impinging sound waves reflect them; the
reflected waves are called the echo. If the speed of sound in the medium is known and the time
taken for the sound waves to travel the distance from the source to the subject and back to the
source is measured, the distance from the source to the subject can be computed accurately. In
this distance measurement system we had ultrasonic sensor HC-SR04 interfaced with Arduino
UnoR3. Programming and hardware part of ultrasonic sensor interfacing with arduino UnoR3
and use a proper display unit for measurement of distance.
Ultrasonic sensor HC-SR04 is used here to measure distance in range of 2cm-400cm with
accuracy of 3mm. The sensor module consists of ultrasonic transmitter, receiver and the control
circuit. The working principle of ultrasonic sensor is as follows:
i. High level signal is sent for 10us using Trigger.
ii. The module sends eight 40 KHz signals automatically, and then detects whether pulse is
received or not.
Koneru Lakshmaiah Education Foundation (Deemed to be University), NAAC - “A++”, Guntur, AP | ECE 55
Embedded Systems and Applications Laboratory Manual 2019
iii. If the signal is received, then it is through high level. The time of high duration is the time
gap between sending and receiving the signal.
Thus the distance of the obstacle from the sensor is calculated by the formula given as:
Distance= (Time x Speed of Sound in Air (340 m/s))/2
Here we have divided the product of speed and time by 2 because the time is the total
time it took to reach the obstacle and return back. Thus the time to reach obstacle is just half the
total time taken.
Specifications:
The HC-SR04 Ultrasonic Sensor contains the following specifications
Working Voltage: DC 5 V
Working Current: 15mA
Working Frequency Min Range 2cm: 40Hz MeasuringAngle 15 degree
Max Range: 4m
Min Range: 2cm
Measuring Angle: 15 degree
Trigger Input Signal: 10uS TTL pulse
Echo Output Signal: Input TTL lever signal and the range in proportion
Dimension: 45*20*15mm
Koneru Lakshmaiah Education Foundation (Deemed to be University), NAAC - “A++”, Guntur, AP | ECE 56
Embedded Systems and Applications Laboratory Manual 2019
Pre-lab Session:
1. Study of Ultrasonic module and Its Description.
3. Design a system to detect the object moment and indicating in the LED.
4. Study how to vary the sensitivity of ultrasonic sensor, other sensors to replace these
sensors for object detection and distance measurement.
Koneru Lakshmaiah Education Foundation (Deemed to be University), NAAC - “A++”, Guntur, AP | ECE 57
Embedded Systems and Applications Laboratory Manual 2019
WIRING DIAGRAM
PROCEDURE
1. Connections are made as per the block diagram.
2. The trigger pin is connected to digital pin 11, and the echo pin is connected to digital pin 12
3. Initialize the serial port at a baud rate of 9600, and set the trigger pin as an output and the echo
pin as an input.
4. Connect the setup to the pc containing arduino software.
5. Open the arduino software tools bar-serial window and observe the results.
CODING/ PROGRAM
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6,
d7)
const int trigPin = 9;
const int echoPin = 10;
long duration;
long distanceCm, distanceInch;
void setup() {
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width
and height) of the display
Koneru Lakshmaiah Education Foundation (Deemed to be University), NAAC - “A++”, Guntur, AP | ECE 58
Embedded Systems and Applications Laboratory Manual 2019
Koneru Lakshmaiah Education Foundation (Deemed to be University), NAAC - “A++”, Guntur, AP | ECE 59
Embedded Systems and Applications Laboratory Manual 2019
EXPECTED OUTPUT
The ultrasonic sensor receives the signal from the obstacle. Based on the duration we
calculated the distance of the obstacle. Output is observed in serial monitor of arduino software
in pc. Sample o/p: 12.12 cm
INFERENCES/ANALYSIS
First of all we need to trigger the ultrasonic sensor module to transmit signal by using
arduino and then wait to receive ECHO. Arduino reads the time between triggering and Received
ECHO. PulsIn () function will wait for the echo pin to go HIGH caused by the bounced sound
wave and it will start timing, then it will wait for the pin to go LOW when the sound wave will
end which will stop the timing. At the end the function will return the length of the pulse in
microseconds. For getting the distance we will multiply the duration by 0.034 or 1/29.1 and
divide it by 2. At the end we will print the value of the distance on the Serial Monitor. We can
calculate distance by using given formula:
Distance= (travel time/2) * speed of sound
The speed of sound is: 343m/s = 0.0343 cm/uS = 1/29.1 cm/uS
Applications
Used to avoid and detect obstacles with robots like biped robot, obstacle avoider robot,
path finding robot etc.
Koneru Lakshmaiah Education Foundation (Deemed to be University), NAAC - “A++”, Guntur, AP | ECE 60
Embedded Systems and Applications Laboratory Manual 2019
CONCLUSION
The distance of an object from the ultrasonic sensor is measured using arduino board and
is displayed on serial monitor and also the lcd.
Koneru Lakshmaiah Education Foundation (Deemed to be University), NAAC - “A++”, Guntur, AP | ECE 61
Embedded Systems and Applications Laboratory Manual 2019
VIVAVOICE
1. How many pins for ultrasonic sensor? What are they?
Koneru Lakshmaiah Education Foundation (Deemed to be University), NAAC - “A++”, Guntur, AP | ECE 62