Bird
0
0

Identify the error in this code snippet using DistanceSensor:

medium📝 Debug Q14 of 15
Raspberry Pi - gpiozero Library
Identify the error in this code snippet using DistanceSensor:
from gpiozero import DistanceSensor
sensor = DistanceSensor(echo=4, trigger=17)
print(sensor.distance * 100)
AEcho and trigger pins are swapped
BMissing import statement
Csensor.distance returns centimeters directly
DPrint statement syntax error
Step-by-Step Solution
Solution:
  1. 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.
  2. Step 2: Verify other code parts

    Import is correct, sensor.distance returns meters (not centimeters), and print syntax is valid.
  3. Final Answer:

    Echo and trigger pins are swapped -> Option A
  4. Quick Check:

    Pin swap error = D [OK]
Quick Trick: Echo pin usually higher number than trigger [OK]
Common Mistakes:
  • Confusing echo and trigger pins
  • Assuming distance returns centimeters
  • Forgetting to import gpiozero

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes