0
0
Iot-protocolsHow-ToBeginner · 3 min read

How to Control GPIO on Raspberry Pi Using Python Easily

Use the RPi.GPIO library in Python to control Raspberry Pi GPIO pins. Import the library, set the pin numbering mode, configure pins as input or output, and use GPIO.output() or GPIO.input() to control or read pin states.
📐

Syntax

To control GPIO pins on Raspberry Pi using Python, you first import the RPi.GPIO library, set the pin numbering mode, configure pins as input or output, and then read or write pin states.

  • GPIO.setmode(GPIO.BCM): Use Broadcom pin numbering.
  • GPIO.setup(pin_number, GPIO.OUT): Set a pin as output.
  • GPIO.output(pin_number, GPIO.HIGH): Set pin to high voltage (on).
  • GPIO.input(pin_number): Read pin state.
  • GPIO.cleanup(): Reset pins when done.
python
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)  # Use BCM pin numbering
GPIO.setup(18, GPIO.OUT)  # Set pin 18 as output
GPIO.output(18, GPIO.HIGH)  # Turn pin 18 on
state = GPIO.input(18)  # Read pin 18 state
GPIO.cleanup()  # Reset GPIO pins
💻

Example

This example turns an LED connected to GPIO pin 18 on and off every second, showing how to control output pins with Python.

python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)  # Use BCM pin numbering
LED_PIN = 18
GPIO.setup(LED_PIN, GPIO.OUT)  # Set pin 18 as output

try:
    while True:
        GPIO.output(LED_PIN, GPIO.HIGH)  # LED on
        print("LED ON")
        time.sleep(1)  # Wait 1 second
        GPIO.output(LED_PIN, GPIO.LOW)  # LED off
        print("LED OFF")
        time.sleep(1)  # Wait 1 second
except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()  # Reset GPIO pins
Output
LED ON LED OFF LED ON LED OFF ... (repeats every second until stopped)
⚠️

Common Pitfalls

Common mistakes when controlling GPIO pins include:

  • Not calling GPIO.cleanup() after running code, which can leave pins in an undefined state.
  • Using the wrong pin numbering mode (GPIO.BOARD vs GPIO.BCM), causing pins to not work as expected.
  • Forgetting to set up pins as input or output before use.
  • Trying to control pins without running the script as root or with proper permissions.
python
import RPi.GPIO as GPIO

# Wrong: Not setting pin mode
# GPIO.setup(18, GPIO.OUT)  # This will fail if GPIO.setmode() not called first

# Right:
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
📊

Quick Reference

FunctionDescription
GPIO.setmode(GPIO.BCM)Set pin numbering to Broadcom SOC channel
GPIO.setup(pin, GPIO.OUT)Configure a pin as output
GPIO.setup(pin, GPIO.IN)Configure a pin as input
GPIO.output(pin, GPIO.HIGH)Set output pin to high voltage (on)
GPIO.output(pin, GPIO.LOW)Set output pin to low voltage (off)
GPIO.input(pin)Read the current state of a pin
GPIO.cleanup()Reset all GPIO pins to default state

Key Takeaways

Always import and use the RPi.GPIO library to control Raspberry Pi GPIO pins in Python.
Set the pin numbering mode with GPIO.setmode() before configuring pins.
Configure each pin as input or output before reading or writing to it.
Call GPIO.cleanup() at the end of your program to reset pins safely.
Run your Python script with appropriate permissions (usually as root) to access GPIO.