Consider this Python code snippet using the DistanceSensor from gpiozero on a Raspberry Pi. What will it print?
from gpiozero import DistanceSensor sensor = DistanceSensor(echo=17, trigger=4) distance = sensor.distance print(round(distance, 2))
Check the distance attribute of the DistanceSensor class in gpiozero.
The distance attribute returns a float representing the distance in meters. The code rounds it to 2 decimals and prints it as a number.
Given this setup: DistanceSensor(echo=18, trigger=23), which GPIO pin sends the ultrasonic pulse?
Remember: trigger pin sends the pulse, echo pin listens.
The trigger pin is the one that sends the ultrasonic pulse. Here it is GPIO 23.
Look at this code snippet:
from gpiozero import DistanceSensor sensor = DistanceSensor(echo=27, trigger=22) print(sensor.get_distance())
Why does it raise AttributeError: 'DistanceSensor' object has no attribute 'get_distance'?
Check the official gpiozero DistanceSensor API for available methods and attributes.
The DistanceSensor class provides a distance attribute, not a get_distance() method. Calling the latter causes AttributeError.
Choose the correct Python code snippet that creates a DistanceSensor object with trigger pin 5 and echo pin 6.
Check the parameter names in the DistanceSensor constructor.
The correct parameter names are trigger and echo. Option B uses them correctly.
Consider this code snippet:
from gpiozero import DistanceSensor
import time
sensor = DistanceSensor(echo=24, trigger=25)
start = time.time()
readings = []
while time.time() - start < 5:
readings.append(sensor.distance)
print(len(readings))Assuming the loop runs as fast as possible, how many readings will be collected approximately?
Think about how fast the ultrasonic sensor can measure distance and how fast the loop can run.
The DistanceSensor updates roughly every 0.1 seconds due to sensor timing, so about 50 readings in 5 seconds.