Control Raspberry Pi GPIO Pins Using Python

Raspberry Pi is a popular single-board computer that is widely used for various projects, ranging from home automation to robotics. One of the key features of Raspberry Pi is its ability to interface with the physical world through its GPIO (General Purpose Input/Output) pins. These pins allow you to connect sensors, actuators, and other electronic components to the Raspberry Pi and control them with software.

Python is a versatile programming language that is widely used for developing applications on Raspberry Pi. In fact, the Raspberry Pi OS comes with Python pre-installed, making it a natural choice for programming GPIO pins.

In this tutorial, we will explore how to use Python to control Raspberry Pi GPIO pins. We will cover the basics of GPIO programming and demonstrate how to turn on and off an LED using Python.

Installing the RPi.GPIO Library

Before we can control GPIO pins, we need to install the RPi.GPIO library. This library provides the interface between Python and the GPIO hardware. Install it using pip ?

pip install RPi.GPIO

Once installed, we can import the library and start working with GPIO pins.

Basic GPIO Setup

First, import the RPi.GPIO library in your Python script ?

import RPi.GPIO as GPIO
import time

Setting GPIO Mode

Configure the GPIO pin numbering mode using GPIO.setmode(). There are two modes available ?

  • GPIO.BCM Uses Broadcom SOC channel numbers
  • GPIO.BOARD Uses physical pin numbers on the board
# Use BCM mode (recommended)
GPIO.setmode(GPIO.BCM)

# Or use BOARD mode
# GPIO.setmode(GPIO.BOARD)

Configuring Pin Direction

Use GPIO.setup() to configure pins as input or output ?

# Configure pin 18 as output (for LED)
GPIO.setup(18, GPIO.OUT)

# Configure pin 17 as input (for button)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

Controlling GPIO Pins

Output Control

Use GPIO.output() to control output pins ?

# Turn LED on (HIGH)
GPIO.output(18, GPIO.HIGH)

# Turn LED off (LOW)
GPIO.output(18, GPIO.LOW)

Reading Input

Use GPIO.input() to read input pin states ?

# Read button state
button_state = GPIO.input(17)

if button_state == GPIO.LOW:
    print("Button pressed!")

Complete Example: LED Control with Button

Here's a complete example that controls an LED with a button press ?

import RPi.GPIO as GPIO
import time

# Set up GPIO mode
GPIO.setmode(GPIO.BCM)

# Define pin numbers
led_pin = 18
button_pin = 17

# Configure pins
GPIO.setup(led_pin, GPIO.OUT)
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    print("Press the button to control LED. Press Ctrl+C to exit.")
    
    while True:
        # Check button state
        if GPIO.input(button_pin) == GPIO.LOW:
            # Button pressed - turn LED on
            GPIO.output(led_pin, GPIO.HIGH)
            print("LED ON")
            
            # Wait for button release
            while GPIO.input(button_pin) == GPIO.LOW:
                time.sleep(0.1)
        else:
            # Button not pressed - turn LED off
            GPIO.output(led_pin, GPIO.LOW)
        
        time.sleep(0.1)

except KeyboardInterrupt:
    print("\nProgram interrupted")

finally:
    # Clean up GPIO pins
    GPIO.cleanup()
    print("GPIO cleanup completed")

GPIO Pin Functions Summary

Function Purpose Example
GPIO.setmode() Set pin numbering mode GPIO.setmode(GPIO.BCM)
GPIO.setup() Configure pin direction GPIO.setup(18, GPIO.OUT)
GPIO.output() Control output pins GPIO.output(18, GPIO.HIGH)
GPIO.input() Read input pins state = GPIO.input(17)
GPIO.cleanup() Reset GPIO settings GPIO.cleanup()

Best Practices

  • Always use GPIO.cleanup() Reset pins when your program ends
  • Use try-except blocks Handle interruptions gracefully
  • Choose consistent numbering Stick with either BCM or BOARD mode
  • Add pull-up/pull-down resistors Prevent floating inputs

Conclusion

Using Python to control Raspberry Pi GPIO pins opens up endless possibilities for physical computing projects. The RPi.GPIO library provides a simple interface for controlling LEDs, reading sensors, and interfacing with various electronic components. Always remember to clean up GPIO resources and handle exceptions properly in your projects.

Updated on: 2026-03-27T14:11:37+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements