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 projects 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.
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.
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.
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:
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)
|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 upgrade
Following commands get you some common Python tools:
// 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:
Now all packages required for interfacing of HC-SR04 with Raspi have been installed.
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:
// Change Directory to UltraTest
$ sudo nano Ultra_Test.py
// Write python script as Ultra_Test.py
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.
$ sudo python Ultra_Test.py
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