Bird
Raised Fist0
Raspberry Piprogramming~30 mins

Setting pin mode (IN, OUT) in Raspberry Pi - Mini Project: Build & Apply

Choose your learning style10 modes available
Setting pin mode (IN, OUT)
📖 Scenario: You have a Raspberry Pi and want to control an LED and read a button press. To do this, you need to set the pins to the right mode: input for the button and output for the LED.
🎯 Goal: Learn how to set the pin mode to IN or OUT using the Raspberry Pi GPIO library.
📋 What You'll Learn
Use the RPi.GPIO library
Set pin 18 as output for an LED
Set pin 23 as input for a button
💡 Why This Matters
🌍 Real World
Setting pin modes is essential when connecting sensors, buttons, and LEDs to a Raspberry Pi for home automation or robotics projects.
💼 Career
Understanding GPIO pin modes is important for hardware interfacing roles, embedded systems programming, and IoT development.
Progress0 / 4 steps
1
Import GPIO and set pin numbering mode
Write code to import the RPi.GPIO library as GPIO and set the pin numbering mode to GPIO.BCM.
Raspberry Pi
Hint

Use import RPi.GPIO as GPIO and then GPIO.setmode(GPIO.BCM) to set the pin numbering.

2
Set pin 18 as output and pin 23 as input
Write code to set pin 18 as an output pin and pin 23 as an input pin using GPIO.setup().
Raspberry Pi
Hint

Use GPIO.setup(18, GPIO.OUT) to set pin 18 as output and GPIO.setup(23, GPIO.IN) to set pin 23 as input.

3
Turn on the LED connected to pin 18
Write code to turn on the LED by setting pin 18 to GPIO.HIGH using GPIO.output().
Raspberry Pi
Hint

Use GPIO.output(18, GPIO.HIGH) to turn on the LED connected to pin 18.

4
Print the mode of pins 18 and 23
Write code to print the mode of pin 18 and pin 23 using GPIO.gpio_function(). The output should be exactly:
Pin 18 mode: OUT
Pin 23 mode: IN
Raspberry Pi
Hint

Use GPIO.gpio_function(pin) to get the mode, then print the mode names IN or OUT accordingly.

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