Home / Tutorials / Raspberry Pi Tutorial / Raspberry Pi Ultrasonic Sensor Tutorial

Raspberry Pi Ultrasonic Sensor Tutorial

An ultrasonic sensor is a common tool for measuring the distance of an object in front of it. While I recommend using an Arduino with ultrasonic sensor to achieve real-time results, it is still possible to use the sensor with a Raspberry Pi. This tutorial shows you how to build a Raspberry Pi ultrasonic sensor for distance measuring.

Ultrasonic Sensor Basics

The term “ultrasonic” means beyond the sound that humans can hear; the human ear can only detect sound up to 20000 Hz. The HC-SR04 ultrasonic sensor featured here emits 40000 Hz.

Physics taught us that sound is vibration traveling through air. When the ultrasound from the sensor bumps into an object, it reflects back to the sensor. The time between signal emission and reflection (echo) is useful for calculating the distance:

$D = V_{sound} * (t_{emit} - t_{ref})$

The velocity of sound in air is 343 metres per second. Thus,

$D = 343 * (t_{emit} - t_{ref})$

The formula above gives the distance (in meters) of the object in front of the sensor.

The HC-SR04 ultrasonic sensor contains four pins as shown:

Note that the blue-colored module works exactly the same as the one in the picture.

The HC-SR04 sensor draws 5V at 15 mA and can detect objects within 2 cm up to 4 m. Also, the viewing angle of this sensor is 15 degrees.

In order for the sensor to start sending ultrasound, a 10 μs pulse is sent to the trigger pin. The ultrasound signal consists of 8 cycles of 40000 Hz. After the signal emission, the echo pin will go high then will go low upon detection of the echo. Below is the timing diagram for the signals:

Using the Ultrasonic Sensor with Raspberry Pi

Hardware

The Raspberry Pi GPIO pins use 3.3V signal levels. The trigger pin of the ultrasonic sensor will still work with 3.3V. However, the echo pin, which gives 5 V, will damage the GPIO pin of the Raspberry Pi. Therefore, the 5V must be reduced to 3.3V through a voltage divider network.

Below is an example wiring diagram for connecting the ultrasonic sensor to a Raspberry Pi:

Note that while a Raspberry Pi Zero is in the diagram, the wiring connection still works for a Raspberry Pi 2 or 3.

Here, a 1k ohm resistor and a 2k ohm resistor creates a voltage divider network which reduces the voltage from the echo pin from 5 V to 3.3 V. The formula is simply:

$V = \frac{2k}{2k+1k} * 5 V= 3.3 V$

Software

We will be using Python 2 for our code. To generate a pulse of 10 μs on the trigger pin, we simply make the pin high, wait for 10 μs and then make the pin low:

Then we note when the echo pin goes low:

And also we take note of when the echo pin goes high:

The pulse width of the echo pin, which is the time between transmission and reception of the ultrasound, is now:

The pulse duration will be in seconds since the function time.time() returns values in seconds. The distance of the object to where the ultrasound was reflected can now be known:

Here, we used 34300 cm/s instead of 343 m/s because of the given range of the sensor (2 cm to 4 m only). Of course, we divide the time by 2 since we only want the time when the signal is emitted and when it hits the object, not when the signal is reflected back.

We can simplify this to:

Here is now the full python code which gives the distance of the object in centimeters:

Was this tutorial helpful? Kindly drop a comment below!

How the DHT22 Sensor Works

The DHT22 temperature and humidity sensor is a common sensor; in fact, there are tons …

This site uses Akismet to reduce spam. Learn how your comment data is processed.