0% found this document useful (0 votes)
8 views

Lecture-22-ESD-Ultrasonic-Distance-using-Arduino-Fall-2023-Anzar-12122024-030141pm

Uploaded by

Muhammad Umair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture-22-ESD-Ultrasonic-Distance-using-Arduino-Fall-2023-Anzar-12122024-030141pm

Uploaded by

Muhammad Umair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Ultrasonic Sensor HC-SR04 and Arduino

Muhammad Anzar Alam (All rights reserved) 1


How the HC-SR04 Ultrasonic Distance Sensor Works?
It emits an ultrasound at 40KHz which travels through the air and if there is
an object or obstacle on its path It will bounce back to the module.
Considering the travel time and the speed of the sound you can calculate
the distance.

Muhammad Anzar Alam (All rights reserved) 2


How the HC-SR04 Ultrasonic Distance Sensor Works?
In order to generate the ultrasound we need to set the Trig
pin on a High State for 10 µs. That will send out an 8 cycle
ultrasonic burst which will travel at the speed of sound.
The Echo pins goes high right away after that 8 cycle
ultrasonic burst is sent, and it starts listening or waiting for
that wave to be reflected from an object.
If there is no object or reflected pulse, the Echo pin will time-
out after 38ms and get back to low state.

Muhammad Anzar Alam (All rights reserved) 3


If we receive a reflected pulse, the Echo pin will go down sooner than those 38ms.
According to the amount of time the Echo pin was HIGH, we can determine the distance the
sound wave traveled, thus the distance from the sensor to the object.
For that purpose we are using the following basic formula for calculating distance:
Distance = Speed x Time
We actually know both the speed and the time values. The time is the amount of time the
Echo pin was HIGH, and the speed is the speed of sound which is 340m/s. There’s one
additional step we need to do, and that’s divide the end result by 2, and that’s because we
are measuring the duration the sound wave needs to travel to the object and bounce back.
Let’s say the Echo pin was HIGH for 1.5ms. If we want to get the distance result in cm, we
can convert the speed of sound value from 340m/s to 34cm/ms.
Distance = (Speed x Time) / 2 = (34cm/ms x 1.5ms) / 2 = 25.5cm.
So, if the Echo pin was HIGH for 2ms (which we measure using the pulseIn() function), the
distance from the sensor to the object is 34cm.
Using the pulseIn() function we read the travel time. This function has 2 parameters,
the first one is the name of the Echo pin and for the second is the state of the pulse we
are reading, either High or Low. echoPin, returns the sound wave travel time in
microseconds duration = pulseIn(echoPin, HIGH);

Muhammad Anzar Alam (All rights reserved) 4


Distance measurement using ultrasonic sensor in Arduino

cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343


inches = (duration/2) / 74; // Divide by 74 or multiply by 0.0135

Muhammad Anzar Alam (All rights reserved) 5


Muhammad Anzar Alam (All rights reserved) 6
Muhammad Anzar Alam (All rights reserved) 7
Muhammad Anzar Alam (All rights reserved) 8
All codes in one slide

Muhammad Anzar Alam (All rights reserved) 9


Distance Measurement using Ultrasonic Waves Clip

Muhammad Anzar Alam (All rights reserved) 10

You might also like