An ultrasonic sensor is a sensor that measures distance. The idea is to utilize the property of sound waves of reflection to receive the reflected signal upon striking obstructions, and then use the speed at which the waves travel through the air to calculate distance.
There are four pins on the sensor they are for 5V Supply, Trigger Pulse Input, Echo Pulse Output, and 0V Ground respectively.

The sensor works by emitting bursts of ultrasonic sound and measuring how long they take to reflect to the sensor, this is the same principle used by animals like bats for echolocation. I will first send a trigger pulse to the sensor, which makes it emit the ultrasonic sound, the sensor then emits a return pulse back to the Raspberry Pi and the length of the pulse is proportional to how long it takes for the sound to reflect the sensor. Using the speed of sound in the air, the sensor can then calculate how far away the object is.

The gpiozero library has already integrated the ultrasonic module called DistanceSensor, I can use it directly through Python programming. How to use it is as follows:
gpiozero.DistanceSensor(echo,trigger,max_distance=1)
# echo tells the computer which pin it is connected to when receiving the reflected ultrasonic wave
# trigger tells the computer which pin it is connected to when sending the trigger signal
# max_distance is the maximum distance it is able to detect, and the default value is one meter
DistanceSensor.distance
# it returns the distance value in meters and the data type is float
Flowchart

Code
#import necessary libraries
from gpiozero import DistanceSensor
from luma.core.render import canvas
from luma.oled.device import ssd1306
from time import sleep
#initialize the OLED and the ultrasonic sensor
device = ssd1306(port=1, address=0x3c)
sensor = DistanceSensor(echo=15, trigger=14)
#detect the distance and display it on the OLED every 0.5 second
while True:
distance = sensor.distance *100
with canvas(device) as draw:
draw.text((0, 0), '01Studio', fill="white")
draw.text((0, 15), 'Distance test:', fill="white")
draw.text((0, 35), str("%.2f"%distance)+' CM' , fill="white")
print('Distance to nearest object is %.2f'%distance, 'CM')
sleep(0.5)