0
0
Raspberry Piprogramming~10 mins

DistanceSensor (ultrasonic) in Raspberry Pi - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the DistanceSensor class from gpiozero.

Raspberry Pi
from gpiozero import [1]
Drag options to blanks, or click blank then click option'
ADistanceSensor
BLED
CButton
DMotor
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong class like LED or Button.
Forgetting to import the class.
2fill in blank
medium

Complete the code to create a DistanceSensor object with trigger pin 17 and echo pin 18.

Raspberry Pi
sensor = DistanceSensor(trigger=[1], echo=18)
Drag options to blanks, or click blank then click option'
A27
B22
C17
D23
Attempts:
3 left
💡 Hint
Common Mistakes
Using the echo pin number for trigger.
Using a wrong pin number.
3fill in blank
hard

Fix the error in the code to print the distance in centimeters.

Raspberry Pi
print(f"Distance: {sensor.[1] * 100:.1f} cm")
Drag options to blanks, or click blank then click option'
Adistance
Bvalue
Cdistance_cm
Ddistance_in
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent property like 'distance_cm'.
Not multiplying by 100 to convert meters to centimeters.
4fill in blank
hard

Fill both blanks to create a dictionary of distances for sensors on pins 5 and 6, only if distance is less than 0.5 meters.

Raspberry Pi
distances = {pin: sensor.distance for pin in [5, 6] if sensor.distance [1] [2]
Drag options to blanks, or click blank then click option'
A<
B0.5
C>
D1.0
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<'.
Using the wrong threshold value.
5fill in blank
hard

Fill all three blanks to create a dictionary with pin numbers as keys, distances in cm as values, only for distances greater than 0.2 meters.

Raspberry Pi
dist_cm = {pin: sensor.[1] * 100 for pin in [7, 8] if sensor.[2] > [3]
Drag options to blanks, or click blank then click option'
Adistance
C0.2
D0.5
Attempts:
3 left
💡 Hint
Common Mistakes
Using different property names.
Using wrong threshold values.