The ultrasonic sensor is an electronic sensor based on acoustic principles. It measures the distance between the sensor and an object by emitting ultrasonic pulses and receiving reflected signals, and is used to detect the presence of objects. It has widespread applications in modern technology and industry, becoming an indispensable tool due to its high-precision measurement capabilities and real-time performance.
The working principle of the ultrasonic sensor is based on the propagation characteristics of sound waves in air. It emits ultrasonic pulse waves, converting sound wave energy into mechanical wave energy that propagates towards the target object. When the sound waves encounter an object, some of the waves are reflected back and received by the sensor’s receiver. By measuring the time difference between emission and reception, the propagation time of the sound waves can be calculated, thus determining the distance between the object and the sensor.
The ultrasonic sensor has many advantages. Firstly, it has a relatively large measurement range, usually covering distances from a few centimeters to several meters. Secondly, it offers high precision and stability, providing accurate measurement results. Additionally, the response speed of the ultrasonic sensor is very fast, typically completing distance measurements within milliseconds. Furthermore, it does not have high requirements for the characteristics of target objects, making it suitable for various surface materials, colors, and transparencies.
Ultrasonic sensors are widely used in many fields. In industrial automation, they are commonly used in obstacle avoidance and navigation systems, capable of monitoring obstacles around robots or automated devices in real-time, thus preventing collisions or achieving precise positioning. In environmental monitoring, they can be used to detect the status of doors and windows, as well as personnel entry and exit, enabling a safe and intelligent management system. Moreover, in intelligent traffic systems, ultrasonic sensors are also widely applied in vehicle parking assistance and intelligent lighting control, enhancing traffic safety and efficiency.
In addition to industrial and transportation fields, ultrasonic sensors also have important applications in medical, home appliances, and consumer electronics. For example, in medical imaging diagnostics, ultrasonic sensors can be used to obtain images of internal tissue structures, assisting doctors in diagnosis and treatment. In the home appliance field, ultrasonic sensors can be used for water level control in washing machines and gesture recognition in smart speakers.
The HC-SR04 ultrasonic sensor module is a commonly used ultrasonic ranging module that combines ultrasonic technology with hardware and software design to measure the distance between an object and the sensor.
The HC-SR04 module mainly consists of a transmitter (ultrasonic transmitter), a receiver (ultrasonic receiver), and a control circuit. By sending ultrasonic pulses and receiving their reflected signals, the module can calculate the distance between the object and the sensor. During operation, the module first sends a short high-frequency ultrasonic pulse, then receives the reflected sound wave signal and calculates the time difference of the round trip of the sound wave. By using the time difference and the speed of sound in air, the distance between the object and the sensor can be obtained.
The HC-SR04 module is easy to use, typically requiring only a 5V power supply and sending a trigger signal through a control IO port to perform measurements. During use, the module’s trigger pin can be controlled through programming to realize the distance measurement function. The distance information output by the module can be processed and utilized by a microcontroller or other microcontrollers.
The HC-SR04 module has high measurement accuracy and stability, capable of reliable measurements within the range of 2 cm to 400 cm. It is not affected by the color or material of the target object and features fast response times, completing a ranging operation within tens of milliseconds.
Due to its simplicity, low cost, and high reliability, the HC-SR04 module is widely used in various fields such as robot navigation, obstacle avoidance control, smart vehicles, and home appliances. Whether for academic research or practical applications, this module provides users with a convenient and effective ultrasonic ranging solution.
-
VCC: Power supply pin for the ultrasonic module, connect to 5V power supply
-
Trig: Ultrasonic transmission pin, sends out 40KHZ ultrasonic waves when at high level
-
Echo: Ultrasonic reception detection pin, outputs high level when receiving returning ultrasonic waves
-
GND: Ground pin for the ultrasonic module
Distance Measurement Principle:
-
The Raspberry Pi sends a 10us pulse signal to the Trig pin.
-
The HC-SR04 receives the signal, starts sending ultrasonic waves, sets Echo to high level, and prepares to receive the returning ultrasonic waves.
-
The HC-SR04 receives the returning ultrasonic waves and sets Echo to low level.
-
The duration of the Echo high level is the time interval for the ultrasonic wave to travel to the object and back.
-
Calculate the distance:
Distance (in meters) = (start – end) * speed of sound / 2
Speed of sound is taken as 343m/s.
Then convert the measured distance to cm.
Distance (in cm) = (start – end) * speed of sound / 2 * 100
= (start – end) * 17150
Next, use the ultrasonic sensor HC-SR04 module on the Raspberry Pi for distance measurement, follow the steps below:
Connect Hardware: Connect the HC-SR04 module to the GPIO pins of the Raspberry Pi. Connect the VCC pin of the HC-SR04 to the 5V pin of the Raspberry Pi, the GND pin to the ground pin of the Raspberry Pi, the Trig pin to any GPIO output pin of the Raspberry Pi (e.g., GPIO17), and the Echo pin to any GPIO input pin of the Raspberry Pi (e.g., GPIO18).
Install Python Libraries: Open the terminal and ensure your Raspberry Pi is connected to the internet. Then, use the following command to install the RPi.GPIO library and pyserial library for Python:
sudo apt-get install python-rpi.gpio
Write Python Code: Create a new Python script, for example, distance_measurement.py, and enter the following code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO_TRIG = 17
GPIO_ECHO = 18
GPIO.setup(GPIO_TRIG, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
def distance(): # Send high level signal to Trig pin GPIO.output(GPIO_TRIG, True)
# Lasting 10 us time.sleep(0.00001)
GPIO.output(GPIO_TRIG, False)
# Duration of high level is the time for ultrasonic wave to travel to and return from the object while GPIO.input(GPIO_ECHO) == GPIO.LOW: pass
start_time = time.time()
while GPIO.input(GPIO_ECHO) == GPIO.HIGH: pass
stop_time = time.time()
# Calculate distance, speed of sound is 34000cm/s. distance = ((stop_time - start_time) * 34000) / 2
return distance
while True: dist = distance() print("Distance: {:.2f} cm".format(dist)) time.sleep(1)