National Institute of Engineering, Mysuru
Department of Electronics and Communication Engineering
INTERNET OF THINGS LAB-4
Aim: To measure the distance of an object using Ultrasonic sensor HC-SR04.
Components required: Raspberry pi kit, Breadboard, HC-SR04 and connecting wires.
Explanation:
Ultrasonic sensor works on the principle of SONAR and RADAR system which is used to
determine the distance to an object.
1. An ultrasonic sensor generates the eight 40 KHz sound (ultrasound) waves.
2. When this ultrasound hits the object, it reflects as echo which is sensed by the
receiver as shown in figure 4.1.
3. Need to transmit trigger pulse of at least 10 us through HC-SR04 Trig Pin shown in
figure 4.2. Time taken by the pulse to reach the echo pin is calculated to determine the
distance.
4. HC - SR04 provides 2cm - 400cm non-contact measurement function, the accuracy
ranging can reach to 3mm.
Specifications: Detailed specification of the HC-SR04 Ultrasonic sensor is given in
table 4.1.
Table 4.1 HC-SR04 specifications
Sl. No Parameters Range
1 Operating Voltage, current 5V DC, 15 mA
2 Operating frequency 40 KHz
3 Range, angle 2 cm to 4m, 150
4 Trigger input signal 10 us TTL pulse
Department of Electronics and Communication Page 1
Figure 4.1 Ultrasonic sensor working
Figure 4.2 HC-SR04 Pulse signals.
Department of Electronics and Communication Page 2
Calculations:
The speed of sound is the distance travelled per unit time by a sound wave as it propagates
through an elastic medium. Speed of sound at different temperature is shown in table 4.2
Table 4.2 temperature v/s speed of sound
Temperature in Speed of sound
Sl. No
(0C) (m/s) in air
1 0 331.30
2 5 334.32
3 10 337.31
4 15 340.27
5 20 343.21
6 25 346.13
7 30 349.20
8 35 351.88
Distance = Speed * Time
346130 ∗Time of Echo pulse
Total Distance = at 25 oC
2
Total distance = 17300 * Time of Echo pulse
Program:
import RPi.GPIO as GPIO # Import Raspberry pi GPIO library
import time #Import time module
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
TRIG = 3 # Variable TRIG assigned to pin number 3
ECHO = 7 # Variable ECHO assigned to pin number 7
print "Distance measurement in progress" # print message
GPIO.setup(TRIG,GPIO.OUT) # Configuring TRIG Pin as Output pin to Pi
GPIO.setup(ECHO,GPIO.IN) # Configuring ECHO Pin as Input pin to Pi
while True: #Run Infinitely
GPIO.output(TRIG, False) # Set TRIG pin to Low
print "Waiting For Sensor to Settle" # Print message
time.sleep(2) #Wait for two seconds
GPIO.output(TRIG, True) #Set TRIG pin to High
time.sleep(0.00001) # transmit trigger pulse of 10 us duration
Department of Electronics and Communication Page 3
GPIO.output(TRIG, False) # Set TRIG pin to Low after 10 us duration
while GPIO.input(ECHO)==0: # Check if ECHO pin is Low
pulse_start = time.time() # Start the timer
while GPIO.input(ECHO)==1: # Check if ECHO pin is High
pulse_end = time.time() # End the timer
pulse_duration = pulse_end - pulse_start # Total time duration of pulse calculated
distance = pulse_duration * 17300 # Distance calculation
distance = round(distance, 2) # Round the number of decimal digits to 2 points
if distance > 2 and distance < 400: # Condition to print the distance if in range
print "Distance:",distance,"cm" #Print message
else: print"Out Of Range" # Condition to print the distance out of range
Department of Electronics and Communication Page 4