This is a tutorial on Raspberry Pi With Ultrasonic Sensor Tutorial or how to use an ultrasonic sensor module with a Raspberry Pi. The ultrasonic sensor module is used to measure distance. The module can be used to measure the distance to an object. The module can also be used to detect ultrasonic sounds. How to Setup a Raspberry Pi for the First Time | Getting Started with Raspberry Pi
The HC-SR04 module
The HC-SR04 module is an Ultrasonic sensor that can be used with Raspberry Pi or Arduino. It is a cheap and easy way to get started with using Ultrasonic sensors. The sensor can be used to measure distance, and can also be used to detect objects.
The sensor has two transducers, one that emits a sound wave, and one that receives the sound wave. The sound wave is used to determine the distance to an object. The HC-SR04 module can be used to measure distances up to 400 cm. The module is easy to use and can be interfaced with Raspberry Pi using just a few lines of code.
The HC-SR04 pinout
The HC-SR04 pinout is as follows:
- Vcc is the power supply voltage, which is +5V for the HC-SR04.
- Trig is the trigger input,
- The echo is the echo output.
- GND is the ground.
How Does the Ultrasonic Sensor Work?
The HC-SR04 sensor works by sending out a ray of ultrasound at a frequency of 40kHz. When this ultrasound hits an object, it will bounce back and be received by the HC-SR04. The HC-SR04 will then output a pulse whose width is proportional to the time it took for the ultrasound to bounce back. The HC-SR04 can be used to measure distance in a variety of applications. For example, it could be used to measure the distance between a sensor and an object in order to determine if the object is close enough to trigger a reaction.
Distance = Time x Speed
- Time – Time that is taken by the sound wave to reach the receiver
- Speed – Speed of the sound wave.
Interesting Note –
Speed of sound changes due to the change in temperature and change in humidity.
The speed of sound, however, is 4.3 times as fast in water as in air
Connecting The Raspberry Pi With Ultrasonic Sensor
As discussed before, ultrasonic sensors have a transmitter (Trigger) that can be operated to transmit infrared sound waves and have a receiver that receives reflected sound waves(ECHO pin).
The trig pin must be connected to an output pin, and the echo pin must be connected to an input pin. Then, the microcontroller can be used to trigger the sensor and read the echo pulse. The HC-SR04 is a viral sensor and is relatively easy to use. It is a versatile sensor that can
Python Script for Raspberry Pi With Ultrasonic Sensor
Now for the script to really take some measures. In this example, I am using Python. Why Python? It’s a famous language on the Raspberry Pi.
You need to update the package version of Raspberry Pi using the below command.
sudo apt-get update
sudo apt-get install idle3
Save the program named distance.py and type python distance.py to run the program in a terminal window. Now, you will see that the sensor starts reading at one-second intervals. Here, you can see an image of a Distance reading.
# Measure distance using an ultrasonic module
# Import required Python libraries
import RPi.GPIO as GPIO
#GPIO Mode (BOARD / BCM)
#set GPIO Pins
GPIO_TRIGGER = 16
GPIO_ECHO = 20
#set GPIO direction (IN / OUT)
# set Trigger to HIGH
# set Trigger after 0.01ms to LOW
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
if __name__ == '__main__':
dist = distance()
print ("Measured Distance = %.1f cm" % dist)
# Reset by pressing CTRL + C
print("Measurement stopped by User")
RPi.GPIO– This module helps us to talk with the GPIO pins of Raspberry Pi.
Time– We are operating the Time Module to add a delay between transmitting infrared sound waves and receiving them.
<span class="hljs-attr">GPIO_ECHO</span> = 20
<span class="hljs-attr">GPIO_TRIG</span> = 16// These lines of code are used for initialize of PINs, to which we will connect the ultrasonic sensor.
- Open Terminal
- Launch IDLE IDE by typing
- After the IDLE launches, open a new window by FILE>OPEN or Ctrl+N
- Type the code below in the window
- Save the code by FILE>SAVE or Ctrl+S
- To run your code RUN>RUN or Ctrl+F5
In this article, we learned how to interface Raspberry Pi With Ultrasonic Sensor Tutorial. If you liked this article or have any suspicion then let me know in the comment area.
Read Similar Articles:
- Getting Started Raspberry Pi Pico – Pinout, Specs – Beginner Guide
- Interfacing PIR Motion Sensor with Raspberry Pi Pico
- Raspberry Pi Pico Home Automation System
- Interface Servo Motor With Raspberry Pi Pico
- Interface 0.96″ OLED Display with Raspberry Pi Pico
- Raspberry Pi Pico Weather Station Using Dht11 Sensor
- Interface 16*2 LCD Display With Raspberry Pi Pico