0
0
Raspberry Piprogramming~10 mins

DistanceSensor (ultrasonic) in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - DistanceSensor (ultrasonic)
Start
Send Trigger Pulse
Wait for Echo Start
Wait for Echo End
Calculate Duration
Calculate Distance
Output Distance
End
The sensor sends a pulse, waits for the echo, measures the time, then calculates and outputs the distance.
Execution Sample
Raspberry Pi
from gpiozero import DistanceSensor
sensor = DistanceSensor(echo=17, trigger=4)
print(sensor.distance * 100)
This code measures distance using an ultrasonic sensor and prints it in centimeters.
Execution Table
StepActionSignal StateTime Measured (s)Distance Calculated (cm)Output
1Send trigger pulse (10µs HIGH)Trigger HIGH
2Wait for echo to go HIGHEcho LOW to HIGH
3Start timing echo pulseEcho HIGH
4Wait for echo to go LOWEcho HIGH to LOW0.015
5Calculate duration0.015
6Calculate distance (duration * 34300 / 2)257.25
7Output distance257.25257.25 cm
8End measurement cycleMeasurement complete
💡 Measurement ends after echo pulse duration is measured and distance is calculated.
Variable Tracker
VariableStartAfter Step 4After Step 6Final
triggerLOWHIGH (10µs pulse)LOWLOW
echoLOWHIGH (echo start)LOW (echo end)LOW
duration00.0150.0150.015
distance00257.25257.25
Key Moments - 3 Insights
Why do we wait for the echo signal to go HIGH and then LOW?
Because the time between echo HIGH and LOW is the pulse duration that tells us how long the sound took to return, as shown in steps 3 and 4 in the execution_table.
Why do we multiply duration by 34300 and then divide by 2 to get distance?
34300 cm/s is the speed of sound. We multiply by duration to get total travel distance and divide by 2 because the sound travels to the object and back, explained in step 6.
What happens if the echo signal never goes LOW?
The measurement would hang waiting for echo end, so the program must handle timeouts to avoid infinite wait, which is not shown here but important in real use.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'duration' after step 4?
A0 seconds
B0.015 seconds
C10 microseconds
D257.25 centimeters
💡 Hint
Check the 'Time Measured (s)' column at step 4 in the execution_table.
At which step is the distance actually calculated?
AStep 6
BStep 5
CStep 3
DStep 7
💡 Hint
Look at the 'Distance Calculated (cm)' column in the execution_table.
If the echo pulse duration doubles, how does the distance change?
ADistance stays the same
BDistance halves
CDistance doubles
DDistance quadruples
💡 Hint
Refer to the formula in step 6: distance is proportional to duration.
Concept Snapshot
DistanceSensor (ultrasonic) measures distance by:
- Sending a short trigger pulse
- Measuring echo pulse duration
- Calculating distance = (duration * speed_of_sound) / 2
- Outputting distance in cm
Use gpiozero library on Raspberry Pi for easy access.
Full Transcript
This visual trace shows how an ultrasonic DistanceSensor works on Raspberry Pi. First, the sensor sends a short trigger pulse. Then it waits for the echo signal to go HIGH, marking the start of the echo pulse. It times how long the echo stays HIGH until it goes LOW again. This duration is the time sound took to travel to the object and back. Using the speed of sound (34300 cm/s), the program calculates distance by multiplying duration by speed and dividing by two. Finally, it outputs the distance in centimeters. Variables like trigger and echo signals change state during the process. Key moments include understanding why we wait for echo HIGH and LOW, the distance formula, and handling echo timeouts. The quizzes test understanding of timing, calculation steps, and proportionality of distance to duration.