Interfacing Ultrasonic Sensor with Raspberry Pi 4 GPIO

In this guide, we will be looking at interfacing ultrasonic sensor module HC-SR04 with the latest version of Raspberry Pi 4. So let's get started!

Before starting with the project let's understand how ultrasonic sensor works:

How Ultrasonic Sensors Work?

How ultrasonic sensors work

An ultrasonic sensor is a sensor that transmits ultrasonic sound waves and receives them back to calculate the distance from the object. It is used in lots of applications like obstacle avoidance robots, measuring liquid in bottles, etc. It's similar to SONAR technology used in ships to calculate the depth of the ocean.

Now let us look at the HC-SR04 ultrasonic sensor particularly. HC-SR04 ultrasonic sensor has 4 important pins. Let's understand them properly.

VCC and GND are power supply pins.

Trig pin:

Sends the ultrasonic wave which hits the target object and gets reflected or echoed back.

Echo pin:

This reflected/echoed wave is received by the receiver which makes this echo pin go from the initial state of HIGH(goes high 5v when trig pin transmits wave) to LOW(goes low when the wave is received).

 

Note: Sending 5V output signal from echo pin to GPIO of Rpi ( rated at 3.3v) would damage the pin. To solve this problem we will be using a level shifter for this pin.

How to calculate the distance between the ultrasonic sensor and the object?

formula for calculate the distance between the ultrasonic sensor and the object

Note:

Speed of sound is 340 m/s in air medium. To calculate the distance in cm, the speed of sound is 34000 cm/s. We will be using this formula later in our code. Now we have understood what is an ultrasonic sensor and how it works let's start making the project now.

Interfacing HC-SR04 with Raspberry Pi

In this project we will be using the following components:

  1. Raspberry Pi 4 B
  2. HC-SR04 ultrasonic sensor module
  3. 1K ,2K Resistors
  4. Jumper cables
  5. Breadboard
  6. Rasberry Pi official power supply adapter

Raspberry pi 4 B

Raspberry pi 4 B is the latest version of RPI which is very fast compared to its predecessors and also easy to use. We will be using this board in our project.

Pinout Diagram of Raspberry Pi 4 B
Pinout Diagram of Raspberry Pi 4 B

Above is the pin diagram for RPi 4 B. It can come in handy in not only this project but any Rpi project.

Pinout diagram of Ultrasonic sensor HC-SR04

Pinout diagram of Ultrasonic sensor HC-SR04

Vcc and ground are power supply pins. Trig and echo are used to transmit and receive the ultrasonic pulse.

Connecting Ultrasonic Sensor to Raspberry Pi

Do the connections as shown in the below diagram:

The trigger pin of the ultrasonic sensor goes to the GPIO 11 (physical PIN is 23) of RPI via the level shifter. The echo pin of the ultrasonic sensor goes to GPIO 12 ( physical PIN 32) of RPI. The 5V and GND pin of ultrasonic sensor is connected to 5V and GND of Raspberry Pi respectively.

Connecting Ultrasonic Sensor to Raspberry Pi

Voltage divider circuit 

Voltage divider circuit

Above is the level shifter circuit made using resistors to bring echo pin level from 5V to 3.3V. Here echo pin of the ultrasonic sensor works on 5V whereas the RPI GPIO pin work on 3.3 V. To make compatible between two different voltages this circuit is used.

Now the connections are done we can start coding!

Python code for interacting Raspberry Pi 4 with ultrasonic sensor HC-SR04

Following is the code for interfacing the ultrasonic sensor with Raspberry pi 4.

Note: Text following ‘#’ character in the below code are all comments added which will help you to understand the code.

 #Raspberry Pi 4.0 code for interfacing with ultrasonic sensor

 

import RPi.GPIO as GPIO         #imports modules required in program
import time                     #time module is used to add delays
GPIO.setmode (GPIO.BCM)   

#Activates broadcom chip specific pin numbers.
#GPIO.setmode (GPIO.BOARD) -activates board pin numbers.                                                    

TRIG_PIN=11                     #assign TRIG_PIN variable to GPIO pin 11
ECHO_PIN=12                     #assign ECHO_PIN variable to GPIO pin 12

GPIO.setup(TRIG_PIN,GPIO.OUT)   #trig pin is output
GPIO.setup(ECHO_PIN,GPIO.IN)    #echo pin is input
GPIO.OUTPUT(TRIG_PIN,GPIO.LOW)  #drives trig pin to 0V

time.sleep(2)                   #delay of 2 seconds

GPIO.output(TRIG_PIN,GPIO.HIGH) #set trig pin high


time.sleep(0.00001

#keeps trig pin high for 10 microseconds 

#this is used to trigger/start the ultrasonic module

#sends 8 ultrasonic bursts at 40KHz.


GPIO.output(TRIG_PIN,GPIO.LOW) #Set trig pin low

while GPIO.input(ECHO_PIN)==0: #check when the echo pin goes low and
    pulse_send=time.time()     #note down this time stamp in pulse_send

while GPIO.input(ECHO_PIN)==1: #check when the echo pin goes high and
    pulse_received=time.time() #note down this time stamp

pulse_duration=pulse_received-pulse_send 

#Pulse duration is the time difference between when the pulse was received #and sent.


pulse_duration=round(pulse_duration/2,2)

#The round function rounds off the value upto 2 decimal places.


distance= 34000*pulse_duration 

#calculate and display the distance


print "Object is at ",distance,"cm from the ultrasonic sensor"

GPIO.cleanup()

#cleans/resets all the ports/pins used in the program.

 

#speed = distance/time

#distance=speed * time

#speed of sound is 340 m/s in air medium

#To calculate the distance in cm, the speed of sound is 34000 cm/s

#note that we have to calculate the distance from the ultrasonic sensor to the #object

# but here pulse duration we have considered is from the time it is sent #till it hits the target and comes back.

# But as we noted earlier we just need distance #from ultrasonic #sensor #to object

#so our pulse duration will be half

 

Conclusion

In this article, we have seen what is an ultrasonic sensor, how it works and also we interfaced it with the latest version of Raspberry Pi

If you have any doubts feel free to comment.

Components and Supplies

You May Also Like To Read:

Leave a comment

Please note, comments must be approved before they are published