Distance Calculation with Ultrasonic Sensor

Arbaz Hussain
4 min readSep 9, 2018

Project at Hackster.io : https://www.hackster.io/arbazhussain/distance-calculation-with-ultrasonic-sensor-26d63e

Ultrasonic distance sensors are designed to measure the distance between the source and target using ultrasonic waves.

Things used in this project

Raspberry Pi 3 Model B×1

Resistor 330 ohm×1

Resistor 475 ohm×1

Breadboard (generic)×1

Jumper wires (generic)×1

Ultrasonic Sensor — HC-SR04 (Generic)×1

HC-SR04 is a commonly used module for non-contact distance measurement for distances from 2cm to 400cm. It uses sonar (like bats and dolphins) to measure distance with high accuracy and stable readings. It consists of an ultrasonic transmitter, receiver and control circuit. The transmitter transmits short bursts which get reflected by the target and are picked up by the receiver. The time difference between the transmission and reception of ultrasonic signals is calculated. Using the speed of sound and ‘Speed = Distance/Time’ equation, the distance between the source and target can be easily calculated.

HC-SR04 ultrasonic distance sensor module has four pins:

  • VCC — 5V, input power
  • TRIG — Trigger Input
  • ECHO — Echo Output
  • GND — Ground
  • VCC -> 5 volt pin on Pi
  • Trig -> Trigger activates the sensor and connect to GIO output pin
  • Echo -> Receives the signal, read by GPIO input pin
  • GND -> GROUND PIN

Distance Calculation

Time taken by the pulse is actually for to and from travel of ultrasonic signals, while we need only half of this. Therefore time is taken as time/2.

Distance = Speed * Time/2

Speed of sound at sea level = 343 m/s or 34300 cm/s

Thus, Distance = 17150 * Time (unit cm)

Calibration

For accurate distance readings, the output can be calibrated using a ruler. In the below program a calibration of 0.5 cm is added.

Setup Shown in Circuit Diagram:

1 / 4 • PINS

Connection Diagram

Code

import RPi.GPIO as GPIO                    #Import GPIO library
import time #Import time library
GPIO.setmode(GPIO.BCM) #Set GPIO pin numbering
TRIG = 23 #Associate pin 23 to TRIG
ECHO = 24 #Associate pin 24 to ECHO
print "Distance measurement in progress"GPIO.setup(TRIG,GPIO.OUT) #Set pin as GPIO out
GPIO.setup(ECHO,GPIO.IN) #Set pin as GPIO in
while True: GPIO.output(TRIG, False) #Set TRIG as LOW
print "Waitng For Sensor To Settle"
time.sleep(2) #Delay of 2 seconds
GPIO.output(TRIG, True) #Set TRIG as HIGH
time.sleep(0.00001) #Delay of 0.00001 seconds
GPIO.output(TRIG, False) #Set TRIG as LOW
while GPIO.input(ECHO)==0: #Check whether the ECHO is LOW
pulse_start = time.time() #Saves the last known time of LOW pulse
while GPIO.input(ECHO)==1: #Check whether the ECHO is HIGH
pulse_end = time.time() #Saves the last known time of HIGH pulse
pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable distance = pulse_duration * 17150 #Multiply pulse duration by 17150 to get distance
distance = round(distance, 2) #Round to two decimal points
if distance > 2 and distance < 400: #Check whether the distance is within range
print "Distance:",distance - 0.5,"cm" #Print distance with 0.5 cm calibration
else:
print "Out Of Range" #display out of range

--

--