Bird
Raised Fist0
Raspberry Piprogramming~10 mins

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

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

Practice

(1/5)
1. What does GPIO.setup(pin, GPIO.IN) do in Raspberry Pi programming?
easy
A. Sets the pin as an input to read signals
B. Sets the pin as an output to send signals
C. Resets the pin to default mode
D. Disables the pin

Solution

  1. Step 1: Understand the function purpose

    GPIO.setup configures a pin's mode on the Raspberry Pi.
  2. Step 2: Identify the mode set by GPIO.IN

    GPIO.IN sets the pin to input mode, allowing it to read signals.
  3. Final Answer:

    Sets the pin as an input to read signals -> Option A
  4. Quick Check:

    GPIO.IN means input mode [OK]
Hint: IN means input mode to read signals [OK]
Common Mistakes:
  • Confusing IN with OUT mode
  • Thinking it resets or disables the pin
  • Assuming it sets output mode
2. Which of the following is the correct syntax to set GPIO pin 17 as an output pin?
easy
A. GPIO.setmode(17, GPIO.OUT)
B. GPIO.setup(17, GPIO.IN)
C. GPIO.output(17, GPIO.OUT)
D. GPIO.setup(17, GPIO.OUT)

Solution

  1. Step 1: Identify the correct function to set pin mode

    GPIO.setup(pin, mode) is used to set pin mode.
  2. Step 2: Choose the correct mode for output

    GPIO.OUT sets the pin as output.
  3. Final Answer:

    GPIO.setup(17, GPIO.OUT) -> Option D
  4. Quick Check:

    Use setup with OUT for output pins [OK]
Hint: Use GPIO.setup(pin, GPIO.OUT) for output pins [OK]
Common Mistakes:
  • Using GPIO.output instead of GPIO.setup to set mode
  • Confusing GPIO.setmode with GPIO.setup
  • Setting pin as input instead of output
3. What will be the output of this code snippet?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT)
GPIO.output(22, True)
print(GPIO.input(22))
medium
A. None
B. True
C. Error: Pin not set as input
D. False

Solution

  1. Step 1: Understand pin mode and output

    Pin 22 is set as output and then set to True (high voltage).
  2. Step 2: Reading pin state with GPIO.input

    Reading the pin returns the current output state, which is True.
  3. Final Answer:

    True -> Option B
  4. Quick Check:

    Output pin set True reads as True [OK]
Hint: Output pin set True reads True when input read [OK]
Common Mistakes:
  • Thinking input read only works on input pins
  • Expecting False because pin is output
  • Assuming error when reading output pin
4. Identify the error in this code snippet:
import RPi.GPIO as GPIO
GPIO.setup(5, GPIO.OUT)
GPIO.output(5, True)
medium
A. Missing GPIO.setmode() before setup
B. Wrong pin number used
C. GPIO.output() cannot set pin high
D. GPIO.setup() should use GPIO.IN

Solution

  1. Step 1: Check for GPIO mode setting

    GPIO.setmode() must be called before setup to define pin numbering scheme.
  2. Step 2: Verify setup and output usage

    Setup and output calls are correct but missing mode causes runtime error.
  3. Final Answer:

    Missing GPIO.setmode() before setup -> Option A
  4. Quick Check:

    Always call GPIO.setmode() first [OK]
Hint: Always call GPIO.setmode() before setup [OK]
Common Mistakes:
  • Skipping GPIO.setmode() call
  • Confusing setup and output functions
  • Using wrong pin numbering without setmode
5. You want to read a button press on pin 10 and turn on an LED on pin 11 when pressed. Which code correctly sets the pin modes?
hard
A. GPIO.setup(10, GPIO.OUT) GPIO.setup(11, GPIO.IN)
B. GPIO.setup(10, GPIO.IN) GPIO.setup(11, GPIO.IN)
C. GPIO.setup(10, GPIO.IN) GPIO.setup(11, GPIO.OUT)
D. GPIO.setup(10, GPIO.OUT) GPIO.setup(11, GPIO.OUT)

Solution

  1. Step 1: Determine pin roles

    Pin 10 reads button input, so it must be input mode.
  2. Step 2: Set LED pin mode

    Pin 11 controls LED output, so it must be output mode.
  3. Final Answer:

    GPIO.setup(10, GPIO.IN) GPIO.setup(11, GPIO.OUT) -> Option C
  4. Quick Check:

    Input pin for button, output pin for LED [OK]
Hint: Input for button pin, output for LED pin [OK]
Common Mistakes:
  • Reversing input and output pins
  • Setting both pins as input
  • Setting both pins as output
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