What is it?
Ever wanted to measure the distance to an object , person or wall? Sound consists of oscillating waves through a medium (such as air) with the pitch being determined by the closeness of those waves to each other, defined as the frequency. Ultrasonic sensors are designed to sense object proximity or range using ultrasound reflection, similar to radar, to calculate the time it takes to reflect ultrasound waves between the sensor and a solid object.
How it works
A basic ultrasonic sensor consists of one or more ultrasonic transmitters (basically speakers), a receiver, and a control circuit. The transmitters emit a high frequency ultrasonic sound, which bounce off any nearby solid objects. Some of that ultrasonic noise is reflected and detected by the receiver on the sensor. That return signal is then processed by the control circuit to calculate the time difference between the signal being transmitted and received. This time can subsequently be used, along with some clever math, to calculate the distance between the sensor and the reflecting object.
Fix it Up
Wiring up the Sensor is very simple, follow the diagram below and connect the pins as shown below. There are not additional libraries or software required by Raspberry Pi or Python to use the Sensor. Once the Pi boots up you are ready to start measuring the distance between the sensor and an object!
Since the distance sensor requires no extra libraries to be installed it is fairly easy to get up and running. After you have connected the wires then reboot the Pi and create a new Python file adding the following code. Type sudo idle in the LX Terminal to ensure that IDLE is opened with administrative privileges.
import RPi.GPIO as GPIO
TRIG = 23
ECHO = 24
print "Distance Measurement In Progress"
print "Waiting For Sensor To Settle"
pulse_start = time.time()
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)