0
0
Raspberry Piprogramming~20 mins

DistanceSensor (ultrasonic) in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ultrasonic Sensor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this ultrasonic sensor reading code?

Consider this Python code snippet using the DistanceSensor from gpiozero on a Raspberry Pi. What will it print?

Raspberry Pi
from gpiozero import DistanceSensor
sensor = DistanceSensor(echo=17, trigger=4)
distance = sensor.distance
print(round(distance, 2))
AA float number representing distance in meters, rounded to 2 decimals
BAn integer number representing distance in centimeters
CA string describing the distance like 'Distance: 0.5m'
DA runtime error because sensor.distance is not a valid attribute
Attempts:
2 left
💡 Hint

Check the distance attribute of the DistanceSensor class in gpiozero.

🧠 Conceptual
intermediate
1:30remaining
Which pin is used as the trigger in this DistanceSensor setup?

Given this setup: DistanceSensor(echo=18, trigger=23), which GPIO pin sends the ultrasonic pulse?

AGPIO 18
BNeither, pins must be specified differently
CBoth GPIO 18 and 23
DGPIO 23
Attempts:
2 left
💡 Hint

Remember: trigger pin sends the pulse, echo pin listens.

🔧 Debug
advanced
2:30remaining
Why does this DistanceSensor code raise an AttributeError?

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'?

ABecause the pins 27 and 22 are invalid GPIO pins
BBecause the sensor must be initialized with <code>DistanceSensor(trigger=22, echo=27)</code> instead
CBecause the DistanceSensor class uses the attribute <code>distance</code>, not a method <code>get_distance()</code>
DBecause the gpiozero library is not imported correctly
Attempts:
2 left
💡 Hint

Check the official gpiozero DistanceSensor API for available methods and attributes.

📝 Syntax
advanced
1:30remaining
Which option correctly initializes a DistanceSensor with trigger on GPIO 5 and echo on GPIO 6?

Choose the correct Python code snippet that creates a DistanceSensor object with trigger pin 5 and echo pin 6.

Asensor = DistanceSensor(echo=5, trigger=6)
Bsensor = DistanceSensor(trigger=5, echo=6)
Csensor = DistanceSensor(5, 6)
Dsensor = DistanceSensor(trigger_pin=5, echo_pin=6)
Attempts:
2 left
💡 Hint

Check the parameter names in the DistanceSensor constructor.

🚀 Application
expert
3:00remaining
How many distance readings will this code collect in 5 seconds?

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?

AAbout 50 readings (due to sensor measurement delay)
BAbout 100 readings (limited by sensor response time)
CAbout 5000 readings (one per millisecond)
DExactly 5 readings (one per second)
Attempts:
2 left
💡 Hint

Think about how fast the ultrasonic sensor can measure distance and how fast the loop can run.