0
0
Raspberry Piprogramming~10 mins

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

Choose your learning style9 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.