Tuesday, June 18, 2024

Ultrasonic Distance Meter Using Raspberry Pi 2

Ultrasonic ranging module HC-SR04 provides 2cm to 400cm non-contact distance measurement range. Its accuracy can reach ±3mm with a cycle period of 50ms, which is the minimum time delay recommended before measurements are taken. The module includes ultrasonic transmitter, receiver and control circuit. This project uses an ultrasonic ranging module with a raspberry pi to build an ultrasonic distance meter.

Raspberry Pi 2 is a second-generation Raspberry Pi board, which was released in February 2015. It is a Linux based computer with a powerful Broadcom BCM2836 ARMv7 900MHz quad-core processor and 1GB SDRAM. It runs on 5V DC at 200mA and has 40 GPIO pins, along with HDMI, micro-SD card slot, Ethernet, USB 2.0 and 3.5 audio (out) ports.

Ultrasonic Distance Meter – Circuit and Working

Components required for this project are Raspberry Pi (or Raspi) 2, HC-SR04 module, 330-ohm and 470-ohm resistors, and some jumper wires.

Working of HC-SR04 ultrasonic sensor is fairly simple. Circuit diagram of Raspi with HC-SR04 is shown in Fig. 3. The HC-SR04 module and its working are shown in Figs 1 and 2, respectively.

HC-SR04 ultrasonic sensor module
Fig. 1: HC-SR04 ultrasonic sensor module
Working of HC-SR04
Fig. 2: Working of HC-SR04
Connection of HC-SR04 module with Raspi for Ultrasonic distance meter
Fig. 3: Connection of HC-SR04 module with Raspi

When the processor gives a high signal to trig pin of the sensor, the sensor emits out eight-cycle sonic bursts at 40kHz. After sending the sonic burst, the sensor sets echo to high till the sonic burst returns to the sensor—after reflection from the object. Hence, length of this pulse is proportional to how far the object is.

- Advertisement -

Now, distance covered by the pulse is twice the distance that has to be measured, and speed of sound is roughly 340 metres per second. Calculations can be carried out using the following relationship:

Speed=Distance/Time, or

340=(2×Distance)/Time, or

- Advertisement -


Hence, distance between the object and the sensor in metres can be determined. To calculate in centimetres, multiply it by 100.

Connect HC-SR04 to Raspi as indicated below (also refer Fig. 3)

HC-SR04 Raspi pins
VCC (+5V) Pin 2 (VCC)
TRIG Pin 16 (GPIO 23)
ECHO Pin 18 (GPIO 24)
GND Pin 34 (GND)

Resistors R1 and R2 are used to balance out the output as the sensor’s high signal is at +5V, whereas Raspi works on +3.3V level.

Hence, the voltage divider helps in reducing the output voltage level.


There are many operating systems designed for Raspi 2, but the most widely used are Raspian (Linux based) and Microsoft Windows 10 IoT Core. If you are a newbie to Raspi 2, check out the link: www.raspberrypi.org/help for information on Raspi.

In this project, the compiler used is Python and operating system for Raspi is Raspbian. The objective is to measure the distance between the sensor and an object that is kept at an unknown distance from the sensor.

There are some packages that are required for interfacing of HC-SR04. In order to download these packages, start Terminal on Raspi and execute the commands given below. Make sure that your Raspi is hooked to the Internet, otherwise the commands will not work.

After logging on, update Raspi using the commands given below. This ensures that you have the latest software and drivers installed.

pi@raspberrrypi ~$ sudo apt-get update
pi@raspberrrypi ~$ sudo apt-get upgrade

Following commands get you some common Python tools:
pi@raspberrrypi ~$ sudo apt-get install python-dev
// This should install python development tools
pi@raspberrrypi~$sudo wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.6.1.tar.gz
// Downloads the GPIO package
pi@raspberrrypi ~$ sudo tar zxf Rpi.
// Extracts the package
pi@raspberrrypi ~$ cd Rpi.GPIO-0.6.1
//This should change the directory to Rpi GPIO folder
pi@raspberrrypi ~$ sudo python setup.py install
// This will install GPIO package on Raspi

To make sure things are in order, reboot your Raspi by typing the following command:
pi@raspberrrypi ~$ sudo reboot

Now all packages required for interfacing of HC-SR04 with Raspi have been installed.

Python code

Start Raspi and create UltraTest folder. For this tutorial, libraries used are RPi.GPIO and time. Create a new file (Ultra_Test.py) in nano editor using the commands given below:
[stextbox id=”grey”]pi@raspberrrypi ~ $ cd ~/Desktop/UltraTest
// Change Directory to UltraTest
pi@raspberrrypi ~/Desktop/UltraTest
$ sudo nano Ultra_Test.py
// Write python script as Ultra_Test.py[/stextbox]
Press ctrl+o to save your Python script followed by pressing ctrl+x to exit.

Construction and Testing

After saving the code and connecting HC-SR04 to Raspi, boot Raspi. From the terminal, type the following commands to execute your program. Destination folder is UltraTest and file name is Ultra_Test.py as shown in Fig. 4.

Output screen of Raspi terminal window
Fig. 4: Output screen of Raspi terminal window

[stextbox id=”grey”]pi@raspberrrypi ~/Desktop/UltraTest
$ sudo python Ultra_Test.py[/stextbox]

This Python script should measure the distance between the desired object and the sensor in centimetres. Refer Fig. 4 for the output screenshot of Raspi.

Download source code

Arnav Bansal is an electronics hobbyist and loves to tinker with circuit designs


  1. I have used this sensor with Arduino chip for measuring the water level in overhead tanks. But, the sensor does not last for more than a week. Does anyone know a very robust sensor that can go on and on? Thanks


Unique DIY Projects

Electronics News

Truly Innovative Tech

MOst Popular Videos

Electronics Components