Bird
Raised Fist0
Raspberry Piprogramming~10 mins

RPi.GPIO library setup in Raspberry Pi - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - RPi.GPIO library setup
Import RPi.GPIO as GPIO
↓
Set GPIO Mode (BOARD or BCM)
↓
Setup Pin as Input or Output
↓
Use Pin for Reading or Writing
↓
Cleanup GPIO Pins
This flow shows how to prepare and use GPIO pins on Raspberry Pi step-by-step.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
GPIO.cleanup()
This code sets pin 18 as output, turns it on, then cleans up GPIO settings.
Execution Table
StepActionGPIO StatePin 18 ModePin 18 OutputNotes
1Import RPi.GPIO as GPIONot setNot setNot setLibrary ready to use
2Set mode to BCMBCM modeNot setNot setPin numbering uses BCM scheme
3Setup pin 18 as outputBCM modeOutputNot setPin 18 ready for output
4Set pin 18 output HIGHBCM modeOutputHIGHPin 18 turned ON
5Cleanup GPIOResetNot setNot setGPIO pins reset to default
💡 GPIO pins cleaned up, program ends
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
GPIO ModeNoneBCMBCMBCMNone
Pin 18 ModeNoneNoneOutputOutputNone
Pin 18 OutputNoneNoneNoneHIGHNone
Key Moments - 3 Insights
Why do we call GPIO.setmode() before setting up pins?
GPIO.setmode() defines how pins are numbered (BCM or BOARD). Without it, pin setup may use wrong numbering, causing errors. See execution_table step 2.
What happens if we forget GPIO.cleanup() at the end?
Pins stay configured, which can cause issues in later runs or hardware damage. Cleanup resets pins safely. See execution_table step 5.
Why do we import RPi.GPIO as GPIO instead of just RPi.GPIO?
Importing as GPIO lets us use shorter names like GPIO.setup(), making code easier to read and write. See execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the GPIO mode after step 3?
ABOARD
BBCM
CNot set
DReset
💡 Hint
Check the 'GPIO State' column at step 3 in execution_table
At which step is pin 18 set as an output?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at 'Pin 18 Mode' column in execution_table
If we skip GPIO.cleanup(), what remains true after the program ends?
APin 18 mode is None
BGPIO mode is reset
CPin 18 output is HIGH
DLibrary is not imported
💡 Hint
See variable_tracker for Pin 18 Output after step 4 and step 5
Concept Snapshot
RPi.GPIO setup steps:
1. Import library as GPIO
2. Set pin numbering mode (GPIO.setmode)
3. Setup pins as input/output (GPIO.setup)
4. Use pins (GPIO.output/input)
5. Cleanup pins (GPIO.cleanup) to reset
Always set mode before setup and cleanup at end.
Full Transcript
This visual execution shows how to prepare Raspberry Pi GPIO pins using the RPi.GPIO library. First, we import the library as GPIO. Then, we set the pin numbering mode to BCM to use Broadcom chip pin numbers. Next, we configure pin 18 as an output pin. After that, we set pin 18 output to HIGH, turning it on. Finally, we call GPIO.cleanup() to reset all pins to their default state. The variable tracker shows how GPIO mode and pin 18's mode and output change step-by-step. Key moments explain why setting mode first and cleaning up last are important. The quiz tests understanding of pin modes and cleanup effects.

Practice

(1/5)
1. What is the purpose of the GPIO.setmode() function in the RPi.GPIO library?
easy
A. It sets the pin numbering system to use (BOARD or BCM).
B. It cleans up all GPIO pins after use.
C. It sets the direction of a specific pin (input or output).
D. It reads the current value of a GPIO pin.

Solution

  1. Step 1: Understand the role of GPIO.setmode()

    This function tells the Raspberry Pi how to number the pins, either by physical board numbers (BOARD) or by Broadcom chip numbers (BCM).
  2. Step 2: Differentiate from other functions

    Cleaning pins is done by GPIO.cleanup(), setting pin direction is GPIO.setup(), and reading pin value is done by GPIO.input().
  3. Final Answer:

    It sets the pin numbering system to use (BOARD or BCM). -> Option A
  4. Quick Check:

    GPIO.setmode() = sets pin numbering [OK]
Hint: Remember: setmode chooses pin numbering style [OK]
Common Mistakes:
  • Confusing setmode() with cleanup()
  • Thinking setmode() sets pin direction
  • Mixing up setmode() with reading pin values
2. Which of the following is the correct way to import the RPi.GPIO library and set the pin numbering mode to BCM?
easy
A. import RPi.GPIO as GPIO\nGPIO.setmode(GPIO.BCM)
B. import RPi.GPIO as GPIO\nGPIO.setmode(GPIO.BOARD)
C. import GPIO\nGPIO.setmode(GPIO.BCM)
D. import RPi.GPIO\nGPIO.setmode(GPIO.BCM)

Solution

  1. Step 1: Check the correct import syntax

    The standard way is to import RPi.GPIO as GPIO to use GPIO functions easily.
  2. Step 2: Verify the setmode argument

    To set pin numbering to BCM, use GPIO.setmode(GPIO.BCM). import RPi.GPIO as GPIO\nGPIO.setmode(GPIO.BCM) matches both correct import and setmode usage.
  3. Final Answer:

    import RPi.GPIO as GPIO\nGPIO.setmode(GPIO.BCM) -> Option A
  4. Quick Check:

    Import as GPIO + setmode BCM = import RPi.GPIO as GPIO\nGPIO.setmode(GPIO.BCM) [OK]
Hint: Import as GPIO and use GPIO.BCM for BCM mode [OK]
Common Mistakes:
  • Forgetting 'as GPIO' in import
  • Using BOARD instead of BCM when BCM is asked
  • Importing just GPIO without RPi
3. What will be the output of this code snippet?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
print(GPIO.gpio_function(7))

Assuming pin 7 is set as output.
medium
A. 0
B. 3
C. 2
D. 1

Solution

  1. Step 1: Understand GPIO.gpio_function()

    This function returns a number representing the pin's current function. For output pins, it returns 1.
  2. Step 2: Check pin setup

    Pin 7 is set as GPIO.OUT, so gpio_function(7) returns 1.
  3. Final Answer:

    1 -> Option D
  4. Quick Check:

    Output pin function code = 1 [OK]
Hint: Output pins return 1 from gpio_function() [OK]
Common Mistakes:
  • Assuming gpio_function returns pin number
  • Confusing output code with input code
  • Not setting pin mode before calling gpio_function
4. Identify the error in this code snippet:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, "OUT")
GPIO.output(11, True)
GPIO.cleanup()
medium
A. Calling GPIO.cleanup() before output.
B. Using string "OUT" instead of GPIO.OUT in setup.
C. Missing GPIO.setwarnings(False) before setup.
D. Not importing time module for delays.

Solution

  1. Step 1: Check GPIO.setup() syntax

    The direction argument must be GPIO.OUT or GPIO.IN, not a string like "OUT".
  2. Step 2: Verify other lines

    GPIO.setwarnings() is optional, cleanup is correctly called last, and time module is not needed here.
  3. Final Answer:

    Using string "OUT" instead of GPIO.OUT in setup. -> Option B
  4. Quick Check:

    Pin direction must be GPIO.OUT, not "OUT" [OK]
Hint: Use GPIO.OUT constant, not string "OUT" [OK]
Common Mistakes:
  • Passing pin direction as string
  • Calling cleanup too early
  • Forgetting to import GPIO properly
5. You want to control an LED connected to pin 12 using BCM numbering. Which sequence of commands correctly sets up the pin and turns the LED on, then cleans up properly?
hard
A. GPIO.setmode(GPIO.BCM)\nGPIO.setup(18, GPIO.OUT)\nGPIO.output(18, True)\nGPIO.cleanup()
B. GPIO.setmode(GPIO.BOARD)\nGPIO.setup(12, GPIO.OUT)\nGPIO.output(12, True)\nGPIO.cleanup()
C. GPIO.setmode(GPIO.BCM)\nGPIO.setup(12, GPIO.OUT)\nGPIO.output(12, True)\nGPIO.cleanup()
D. GPIO.setmode(GPIO.BOARD)\nGPIO.setup(18, GPIO.OUT)\nGPIO.output(18, True)\nGPIO.cleanup()

Solution

  1. Step 1: Match numbering mode with pin number

    Pin 12 in BCM mode corresponds to GPIO12, so setmode(GPIO.BCM) and setup(12) is correct.
  2. Step 2: Confirm output setup and cleanup

    Pin is set as output, turned on with True, and cleanup is called last to reset pins.
  3. Final Answer:

    GPIO.setmode(GPIO.BCM)\nGPIO.setup(12, GPIO.OUT)\nGPIO.output(12, True)\nGPIO.cleanup() -> Option C
  4. Quick Check:

    BCM mode + pin 12 + output + cleanup = GPIO.setmode(GPIO.BCM)\nGPIO.setup(12, GPIO.OUT)\nGPIO.output(12, True)\nGPIO.cleanup() [OK]
Hint: Match setmode with pin number system [OK]
Common Mistakes:
  • Mixing BOARD and BCM numbering
  • Using wrong pin number for chosen mode
  • Forgetting to call cleanup()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard