DistanceSensor (ultrasonic) in Raspberry Pi - Time & Space Complexity
When using an ultrasonic DistanceSensor on a Raspberry Pi, it's important to understand how the time to get distance readings grows as we take more measurements.
We want to know how the program's running time changes when we increase the number of distance checks.
Analyze the time complexity of the following code snippet.
from gpiozero import DistanceSensor
sensor = DistanceSensor(echo=17, trigger=4)
for i in range(n):
distance = sensor.distance
print(f"Distance {i+1}: {distance:.2f} meters")
This code takes n distance measurements from the ultrasonic sensor and prints each result.
- Primary operation: Reading the distance from the sensor inside the loop.
- How many times: Exactly
ntimes, once per loop iteration.
Each additional measurement adds one more sensor reading and print operation.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 sensor reads + 10 prints |
| 100 | 100 sensor reads + 100 prints |
| 1000 | 1000 sensor reads + 1000 prints |
Pattern observation: The total work grows directly in proportion to the number of measurements.
Time Complexity: O(n)
This means the time to complete the program grows linearly as you increase the number of distance readings.
[X] Wrong: "Reading the sensor once and printing multiple times is the same as reading it multiple times."
[OK] Correct: Each sensor reading takes time and reflects a new measurement; printing multiple times without new readings does not give updated distances.
Understanding how loops affect program time helps you explain how sensor data collection scales, a useful skill when working with hardware and real-time data.
"What if we added a delay inside the loop between readings? How would the time complexity change?"
Practice
DistanceSensor measure using ultrasonic waves?Solution
Step 1: Understand the sensor's purpose
The DistanceSensor uses sound waves to detect how far an object is without physical contact.Step 2: Identify the correct measurement type
It measures distance, not temperature, speed, or brightness.Final Answer:
The distance to an object without touching it -> Option CQuick Check:
DistanceSensor measures distance = C [OK]
- Confusing distance with temperature
- Thinking it measures speed or light
- Assuming physical contact is needed
Solution
Step 1: Recall parameter names for DistanceSensor
The DistanceSensor constructor expects named parameters: echo and trigger pins.Step 2: Match correct pin assignments
sensor = DistanceSensor(echo=17, trigger=4) correctly assigns echo=17 and trigger=4 as named arguments.Final Answer:
sensor = DistanceSensor(echo=17, trigger=4) -> Option BQuick Check:
Use named pins echo=17, trigger=4 = A [OK]
- Swapping echo and trigger pins
- Passing pins without names
- Using wrong pin numbers
from gpiozero import DistanceSensor sensor = DistanceSensor(echo=17, trigger=4) distance_cm = sensor.distance * 100 print(round(distance_cm))
Solution
Step 1: Understand sensor.distance value
The sensor.distance returns distance in meters as a float, here 0.25 meters.Step 2: Convert meters to centimeters and round
Multiplying 0.25 by 100 gives 25 centimeters, then rounding keeps it 25.Final Answer:
25 -> Option AQuick Check:
0.25 m * 100 = 25 cm [OK]
- Printing meters instead of centimeters
- Multiplying by 1000 instead of 100
- Not rounding the result
from gpiozero import DistanceSensor sensor = DistanceSensor(echo=4, trigger=17) print(sensor.distance * 100)
Solution
Step 1: Check pin assignments
The echo pin should be connected to the sensor's echo pin, trigger to trigger pin. Usually, echo=17 and trigger=4 is standard, so swapping is likely wrong.Step 2: Verify other code parts
Import is correct, sensor.distance returns meters (not centimeters), and print syntax is valid.Final Answer:
Echo and trigger pins are swapped -> Option AQuick Check:
Pin swap error = D [OK]
- Confusing echo and trigger pins
- Assuming distance returns centimeters
- Forgetting to import gpiozero
Solution
Step 1: Check pin assignments and imports
from gpiozero import DistanceSensor import time sensor = DistanceSensor(echo=17, trigger=4) while True: print(int(sensor.distance * 100)) time.sleep(1) correctly imports time, sets echo=17 and trigger=4, which is standard.Step 2: Verify distance calculation and printing
It multiplies sensor.distance (meters) by 100 to get centimeters, converts to int for integer output, and prints every second with time.sleep(1).Step 3: Compare with other options
from gpiozero import DistanceSensor sensor = DistanceSensor(echo=17, trigger=4) print(round(sensor.distance)) prints once and rounds meters, not centimeters. from gpiozero import DistanceSensor import time sensor = DistanceSensor(echo=4, trigger=17) while True: print(sensor.distance * 100) time.sleep(1) swaps pins and prints float centimeters. from gpiozero import DistanceSensor import time sensor = DistanceSensor(echo=17, trigger=4) while True: print(sensor.distance) time.sleep(1) prints meters, not centimeters.Final Answer:
from gpiozero import DistanceSensor import time sensor = DistanceSensor(echo=17, trigger=4) while True: print(int(sensor.distance * 100)) time.sleep(1) -> Option DQuick Check:
Correct pins, int centimeters, 1-second delay = A [OK]
- Swapping echo and trigger pins
- Not converting meters to centimeters
- Printing only once instead of every second
Ready to prove it?
Pass the test and this pattern moves unknown
