0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Use RPi.GPIO Library in Python on Raspberry Pi

To use the RPi.GPIO library in Python on a Raspberry Pi, first import it with import RPi.GPIO as GPIO. Then set the pin numbering mode with GPIO.setmode(), configure pins as input or output using GPIO.setup(), and control pins with GPIO.output() or read input with GPIO.input().
📐

Syntax

The RPi.GPIO library lets you control Raspberry Pi pins easily. Here are the main parts:

  • import RPi.GPIO as GPIO: Loads the library and gives it a short name.
  • GPIO.setmode(GPIO.BCM) or GPIO.setmode(GPIO.BOARD): Choose pin numbering style (BCM uses chip pin numbers, BOARD uses physical pin numbers).
  • GPIO.setup(pin_number, GPIO.OUT): Set a pin as output to send signals.
  • GPIO.setup(pin_number, GPIO.IN): Set a pin as input to read signals.
  • GPIO.output(pin_number, GPIO.HIGH): Turn an output pin on.
  • GPIO.output(pin_number, GPIO.LOW): Turn an output pin off.
  • GPIO.input(pin_number): Read the current value of an input pin.
  • GPIO.cleanup(): Reset pins to safe state 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
GPIO.output(18, GPIO.LOW)   # Turn pin 18 off
GPIO.cleanup()  # Reset pins
💻

Example

This example blinks an LED connected to GPIO pin 18 on and off every second. It shows how to set up the pin, turn it on and off, and clean up safely.

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  # Stop on Ctrl+C
finally:
    GPIO.cleanup()  # Reset pins
Output
LED ON LED OFF LED ON LED OFF ... (repeats every second until stopped)
⚠️

Common Pitfalls

Some common mistakes when using RPi.GPIO include:

  • Not calling GPIO.cleanup() at the end, which can leave pins in an unsafe state.
  • Mixing GPIO.BCM and GPIO.BOARD numbering modes, causing wrong pins to be controlled.
  • Forgetting to set up pins with GPIO.setup() before using them.
  • Running the script without root permissions, which is required to access GPIO pins.

Example of wrong and right usage:

python
# Wrong: No setup and no cleanup
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.output(18, GPIO.HIGH)  # Error: pin not set up

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

Quick Reference

FunctionPurposeExample
import RPi.GPIO as GPIOLoad the GPIO libraryimport RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)Set pin numbering mode to BCMGPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)Set pin as outputGPIO.setup(18, GPIO.OUT)
GPIO.setup(pin, GPIO.IN)Set pin as inputGPIO.setup(23, GPIO.IN)
GPIO.output(pin, GPIO.HIGH)Turn output pin onGPIO.output(18, GPIO.HIGH)
GPIO.output(pin, GPIO.LOW)Turn output pin offGPIO.output(18, GPIO.LOW)
GPIO.input(pin)Read input pin valuevalue = GPIO.input(23)
GPIO.cleanup()Reset all pins safelyGPIO.cleanup()

Key Takeaways

Always import RPi.GPIO and set the pin numbering mode before using pins.
Use GPIO.setup() to configure pins as input or output before reading or writing.
Call GPIO.cleanup() at the end to reset pins and avoid issues.
Run your Python script with root permissions (e.g., sudo) to access GPIO.
Be consistent with pin numbering mode: BCM or BOARD, never mix them.