Hello,
In this tutorial, we are going to interface the Ultrasonic Sensor HC-SR04 with Raspberry Pi.

Hardware Required

Software Required

  • Raspberry Pi OS


  • One of the most commonly used sensors in robotics projects is the distance sensor. The Ultrasonic sensor HC-SR04 can easily help the robot navigate around in a controlled environment. It can also be used to calculate the distance to an object in front of it. It has many applications, some of which we will discuss below in this article.

  • This Ultrasonic sensor is a very inexpensive electronic component that can be used for a multitude of applications. It is used in almost all robotics projects as almost all of them are either required to measure the distance or for object intrusion. It is generally used with Arduino or other microcontrollers which provide 5V.

  • However, this Ultrasonic sensor can also be used with Raspberry Pi. As the Ultrasonic sensor requires 5 volts, we will need to connect a couple of resistors to interface it with the 3.3V GPIO pins. 

  • The Ultrasonic sensor can measure from about 2 cm to 400 cm. And it draws very little current so it can be used with battery-powered devices. 

  • This Ultrasonic sensor works on the principle of SONAR. 

  • SONAR (sound navigation and ranging) is very similar to the RADAR system. In both of the systems, the detection is based on the propagation of waves between the target and detector. There are two types of sonar systems, first is active sonar systems. Here the wave propagates from the transmitter to the target and back to the receiver, which is similar to pulse-echo radar. Then there are passive sonar systems. Here the target is the source of the energy which propagates to the receiver, which is similar to passive infrared detection. There is, however, a fundamental difference between sonar and radar systems. The energy transferred by acoustic waves is propagating in the water. 

  • This sensor uses ultrasonic pulses to detect the objects in front of it. It consists of two main components: 

    • Ultrasonic transmitter: The transmittal the sound pulses and it uses 40kHz frequency for it. 

    • Ultrasonic receiver: The receiver detects the reflected pulses. It outputs a signal which is used to measure the distance from the object to the sensor. 

  • The Ultrasonic sensor has 4 pins:

  • VCC pin: For Power supply.

  • TRIG pin: This pin is the input pin and transmits the waves.

  • ECHO pin: This pin is the output pin and it detects the reflected wave.

  • GND pin: This pin is the ground pin. 

Working of the Ultrasonic sensor

  • A 5V pulse is applied to the TRIG pin for at least 10 microseconds.

  • Now the transmitter will emit light pulses of the wave at 40kHz. The number of pulses is chosen to eight to keep the ultrasonic wave unique from other background noise at the time of detection. 

  • Now as the transmitted wave goes forward, the ECHO pin goes high and emits a pulse for 38 milliseconds. 

  • The ECHO pin goes low after 38ms if the wave does not get reflected. 

  • If the transmitted wave gets reflected, then the ECHO pin will immediately go low. And the width of the output pulse will be anywhere from 150 microseconds to 25 milliseconds. 

  • The width of the pulse from the ECHO pin is used to calculate the distance of the object from the sensor. 

Interfacing with Raspberry Pi

  • The circuit diagrams for this are very simple as we simply have to connect all 4 pins to the GPIO pins of the Raspberry Pi. 

Python code for Raspberry Pi

import RPi.GPIO as GPIO  
import time  GPIO.setmode(GPIO.BCM)  
GPIO_TRIG = 11  
GPIO_ECHO = 18 
GPIO.setup(GPIO_TRIG, GPIO.OUT)  
GPIO.setup(GPIO_ECHO, GPIO.IN)  
GPIO.output(GPIO_TRIG, GPIO.LOW)  
Time.sleep(2)  
GPIO.output(GPIO_TRIG, GPIO.HIGH)  
Time.sleep(0.00001)  
GPIO.output(GPIO_TRIG, GPIO.LOW)  
while GPIO.input(GPIO_ECHO)==0:  
start_time = time.time()  
while GPIO.input(GPIO_ECHO)==1:  
Bounce_back_time = time.time()  
pulse_duration = Bounce_back_time - start_time  distance = round(pulse_duration * 17150, 2)  
print ("Distance:",distance,"cm")  
GPIO.cleanup() 
  • Upload this code to the Raspberry Pi board via the python terminal and you will see the distance on the screen. 

  • I hope you learned something about the Ultrasonic sensor and its interfacing with Raspberry Pi. And I hope you enjoyed it, Thank you.