Bird
0
0

Examine this Flask route intended to toggle GPIO pin 22:

medium📝 Debug Q6 of 15
Raspberry Pi - Web Server and API
Examine this Flask route intended to toggle GPIO pin 22:
@app.route('/flip')
def flip_pin():
current_state = GPIO.input(22)
GPIO.output(22, not current_state)
return 'Pin toggled'
What is the main issue with this code?
AGPIO.output cannot accept boolean values
BThe route decorator syntax is incorrect
CGPIO.input(22) may not be set up as input, causing an error
DThe function lacks a return statement
Step-by-Step Solution
Solution:
  1. Step 1: Check GPIO setup

    GPIO pin 22 must be configured as input or output before reading or writing.
  2. Step 2: Identify missing setup

    If GPIO.setup(22, GPIO.OUT) is missing, GPIO.input(22) will cause an error.
  3. Step 3: Confirm other code parts

    Route decorator and return statement are correct; GPIO.output accepts boolean values.
  4. Final Answer:

    GPIO.input(22) may not be set up as input, causing an error -> Option C
  5. Quick Check:

    GPIO pins must be set up before input/output [OK]
Quick Trick: Always setup GPIO pins before reading or writing [OK]
Common Mistakes:
MISTAKES
  • Assuming GPIO.input works without setup
  • Thinking GPIO.output rejects boolean values
  • Ignoring the need for GPIO.setup call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes