0
0
Raspberry Piprogramming~10 mins

Setting pin mode (IN, OUT) in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Setting pin mode (IN, OUT)
Start
Choose Pin Number
Choose Mode: IN or OUT
Call set_pin_mode(pin, mode)
Pin mode set in hardware
Ready to read or write
End
This flow shows how you pick a pin, choose its mode (input or output), set it, and then use it.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
This code sets pin 18 as an output and turns it on (high).
Execution Table
StepActionPinModeResult
1Set GPIO mode-BCMGPIO numbering set to BCM
2Setup pin mode18OUTPin 18 set as output
3Write output18HIGHPin 18 output set to HIGH
4End--Pin 18 ready as output and ON
💡 All steps completed, pin 18 configured as output and set HIGH
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
GPIO modeNoneBCMBCMBCMBCM
Pin 18 modeUnknownUnknownOUTOUTOUT
Pin 18 outputUnknownUnknownUnknownHIGHHIGH
Key Moments - 2 Insights
Why do we call GPIO.setmode(GPIO.BCM) before setting pin modes?
GPIO.setmode(GPIO.BCM) sets the numbering system for pins. Without it, the program won't know which pin numbers you mean. See execution_table step 1.
What happens if you set a pin mode to OUT but never write HIGH or LOW?
The pin is ready to output but stays in its default state (usually LOW). You must write HIGH or LOW to control it. See execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what mode is pin 18 set to after step 2?
AIN
BOUT
CHIGH
DBCM
💡 Hint
Check the 'Mode' column in execution_table row for step 2.
At which step is the pin output value set to HIGH?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table.
If you skip GPIO.setmode(GPIO.BCM), what will happen?
APin modes will default to OUT
BPin output will be HIGH by default
CPin numbers may be misinterpreted
DProgram will run without errors
💡 Hint
Refer to key_moments about why setmode is important.
Concept Snapshot
Use GPIO.setmode(GPIO.BCM) to choose pin numbering.
Use GPIO.setup(pin, GPIO.IN or GPIO.OUT) to set pin mode.
For output pins, use GPIO.output(pin, GPIO.HIGH or GPIO.LOW) to control voltage.
Input pins read signals; output pins send signals.
Always set mode before using pins.
Full Transcript
This lesson shows how to set a Raspberry Pi pin as input or output. First, you choose the pin numbering system with GPIO.setmode(GPIO.BCM). Then you pick a pin number and set its mode with GPIO.setup. For output pins, you can turn them on or off with GPIO.output. The execution table traces each step: setting mode, configuring pin 18 as output, and turning it on. Variables track the mode and output state changes. Key moments explain why setting the mode first is important and what happens if you don't write output values. The quiz tests your understanding of pin modes and step order. Remember, setting pin mode correctly is the first step to controlling hardware safely and effectively.