GPIO (General Purpose Input/Output) pins allow the Raspberry Pi to interact with hardware — LEDs, sensors, relays, and more. This guide introduces GPIO control using Python.

Install RPi.GPIO Library

sudo apt install python3-rpi.gpio

Blink an LED

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

for _ in range(10):
    GPIO.output(18, GPIO.HIGH)
    time.sleep(0.5)
    GPIO.output(18, GPIO.LOW)
    time.sleep(0.5)

GPIO.cleanup()

Read a Button Input

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
state = GPIO.input(17)