0
0
Raspberry Piprogramming~30 mins

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

Choose your learning style9 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
Need a 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
Need a 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
Need a 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
Need a hint?

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