Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q14 of 15
Raspberry Pi - GPIO Basics with Python
Identify the error in this code snippet:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.input(18)
GPIO.setup(18, GPIO.IN)
AGPIO.input called before GPIO.setup
BGPIO.setup uses wrong pin number
CGPIO.setmode is missing
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check order of GPIO calls

    GPIO pins must be set up before reading input. Here, GPIO.input(18) is called before GPIO.setup(18, GPIO.IN), which is incorrect.
  2. Step 2: Verify other parts

    Pin number 18 is consistent, and GPIO.setmode(GPIO.BCM) is present. So no error there.
  3. Final Answer:

    GPIO.input called before GPIO.setup -> Option A
  4. Quick Check:

    GPIO.input called before GPIO.setup = D [OK]
Quick Trick: Always setup pins before reading input [OK]
Common Mistakes:
  • Calling GPIO.input before GPIO.setup
  • Ignoring pin mode setup
  • Assuming code runs without setup

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes