Bird
0
0

What is wrong with this code snippet for servo control?

medium📝 Debug Q7 of 15
Raspberry Pi - PWM Output
What is wrong with this code snippet for servo control? import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.OUT) pwm = GPIO.PWM(23, 50) pwm.start(5) pwm.ChangeDutyCycle(20) pwm.stop() GPIO.cleanup()
AGPIO.setup missing output mode
BDuty cycle 20% is out of typical servo range
CPWM frequency should be 100 Hz
Dpwm.stop() should be called before ChangeDutyCycle
Step-by-Step Solution
Solution:
  1. Step 1: Check duty cycle range for servo

    Typical servo duty cycle ranges from about 2.5% to 12.5%, 20% is too high.
  2. Step 2: Verify other code correctness

    GPIO.setup has output mode, frequency 50 Hz is standard, stop called after changes.
  3. Final Answer:

    Duty cycle 20% is out of typical servo range -> Option B
  4. Quick Check:

    Duty cycle must be within servo range [OK]
Quick Trick: Keep duty cycle between 2.5% and 12.5% for servo [OK]
Common Mistakes:
  • Using too high duty cycle
  • Missing GPIO output mode
  • Wrong PWM frequency

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes