Raspberry Pi Pinout
The pins on a Raspberry Pi are not labelled, so you should refer to the pinout below. Note that the pinout for all models of Raspberry Pi is the same.
How to Use the GPIO Library
You can find documentation for the Raspberry Pi GPIO library here.
Within a Python script, import the library like this:
import RPi.GPIO as GPIO
Next, you’ll need to set what naming convention to use when referring to pins. There are two options: GPIO.BOARD and GPIO.BCM. GPIO.BOARD refers to pin numbers, while GPIO.BCM refers to channel numbers. I recommend using GPIO.BOARD to avoid any unnecessary complexity. Note that when using GPIO.BOARD, you’ll refer to GPIO channels as their pin numbers (e.g. GPIO 17 would be referenced as pin 11).
GPIO.setmode(GPIO.BOARD)
Every pin that you use will need to be designated as an input or output:
GPIO.setup(pin, GPIO.IN)
# or
GPIO.setup(pin, GPIO.OUT)
All GPIO pins on a Raspberry Pi are digital (external ADCs can be added for analog functionality), so the following commands can be used to read input or write output:
GPIO.input(pin)
# or
GPIO.output(pin, state) # state is either GPIO.LOW or GPIO.HIGH
At the end of your script, release all of the pins in use by including this command:
GPIO.cleanup()