← How-Tos
raspberry-pi Jun 18, 2026 ◑ 2 views ◯ 1 min read

Raspberry Pi GPIO: Complete Beginner Guide with Python Examples

raspberry pigpiopythonledsensori2cspitutorialbeginnerelectronics

The Raspberry Pi's 40-pin GPIO header lets you control LEDs, read buttons, drive motors, and talk to sensors. This guide covers the essentials with working Python examples.

BCM vs Physical Pin Numbering

Two systems exist and mixing them is the #1 source of confusion:

Choose with GPIO.setmode(GPIO.BCM) or GPIO.setmode(GPIO.BOARD). See the full 40-pin pinout reference on this site.

Important: The Pi's GPIO is 3.3V, NOT 5V tolerant. Connecting 5V to any GPIO pin damages the Pi.

Blink an LED

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
try:
    while True:
        GPIO.output(17, GPIO.HIGH); time.sleep(0.5)
        GPIO.output(17, GPIO.LOW);  time.sleep(0.5)
except KeyboardInterrupt:
    GPIO.cleanup()

Connect a 330Ω resistor in series with LED between GPIO 17 and GND.

Read a Button

GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try:
    while True:
        if GPIO.input(27) == GPIO.LOW:  # active low
            print('Button pressed!')
except KeyboardInterrupt:
    GPIO.cleanup()

I2C Sensor

Enable I2C: sudo raspi-config → Interface Options → I2C → Enable.

import smbus2
bus = smbus2.SMBus(1)  # GPIO 2 (SDA), GPIO 3 (SCL)
value = bus.read_byte_data(0x48, 0x00)

Find connected devices: sudo i2cdetect -y 1

Common Mistakes