Tutorial 5- Ultrasonic Sensor
In this tutorial, we will learn how to connect the Ultrasonc Sensor Module to the Raspberry Pi 5.
Introduction
The Ultrasonic Sensor Module is a non-contact distance measurement module that uses ultrasonic waves to detect the distance between the module and an object. It is commonly used in robotics, automation, and security systems for obstacle avoidance, distance measurement, and object detection.
The Ultrasonic Sensor Module works by emitting a high-frequency sound wave and measuring the time it takes for the sound wave to bounce back after hitting an object. The module contains a transducer that emits the sound wave and a receiver that detects the reflected wave.
Components Needed
Component | Quantity |
---|---|
Raspberry Pi 5 | 1 |
Breadboard | 1 |
Wires | Several |
HC SR04 Ultrasonic Module | 1 |
T-extension Board | 1 |
Fritzing Diagram
Connect the Keypad to your Raspberry Pi as shown in the following diagram.
Code
from gpiozero import DistanceSensor
from time import sleep
# Initialize the DistanceSensor with specified GPIO pins
sensor = DistanceSensor(echo=24, trigger=23)
# Main loop to continuously measure and report distance
while True:
distance_cm = sensor.distance * 100 # Convert distance from meters to centimeters
print('Distance: {:.2f} cm'.format(distance_cm)) # Print the distance with two decimal precision
sleep(0.3) # Wait for 0.3 seconds before the next measurement
Code Explanation
Imports and Initialization
from gpiozero import DistanceSensor
from time import sleep
# Initialize the DistanceSensor with specified GPIO pins
sensor = DistanceSensor(echo=24, trigger=23)
Imports: The DistanceSensor class from the gpiozero library is imported to handle the ultrasonic sensor, and sleep from the time module is imported to create delays.
Sensor Initialization: An instance of DistanceSensor is created, specifying the GPIO pins for the echo and trigger. The echo pin (24) receives the reflected signal, while the trigger pin (23) sends out the ultrasonic pulse.
Main Loop
# Main loop to continuously measure and report distance
while True:
distance_cm = sensor.distance * 100 # Convert distance from meters to centimeters
print('Distance: {:.2f} cm'.format(distance_cm)) # Print the distance with two decimal precision
sleep(0.3) # Wait for 0.3 seconds before the next measurement
Infinite Loop: The while True: statement creates an infinite loop that continuously executes the code inside it.
Distance Measurement: sensor.distance retrieves the distance measured by the sensor in meters. Multiplying by 100 converts this distance to centimeters.
Print Output: The distance is printed to the console with two decimal places of precision using formatted string output.
Delay: sleep(0.3) pauses the loop for 0.3 seconds before the next measurement, reducing the frequency of readings and allowing time for the sensor to reset.
Conclusion
This code initializes an ultrasonic distance sensor and continuously measures the distance to an object, printing the results to the console. The program runs indefinitely, providing real-time distance measurements until it is manually stopped. The structure is simple and straightforward, making it easy to understand and modify.