Paul Baumgarten

Computer Scientist, teacher, freelance programmer and self confessed geek

Raspberry Pi: GPIO

GPIO pin out

For Raspberry Pi model 2 and 3

It is unfortunate that each Raspberry Pi as more than one numbering scheme, and the numbers change/move around based on the version of the Pi. You specify in your Python code which numbering scheme you are using with the GPIO.setmode() command (see example code).

GPIO.setmode() has two acceptable values as follows:

  • GPIO.BOARD specifies that you are referring to the pins by the number of the pin on the board. These are the numbers in grey, which are by themselves on the above diagram.
  • GPIO.BCM means that you are referring to the pins by the "Broadcom SOC channel" number, these are the numbers that appear after the word "GPIO" in the orange rectangles on the above diagram.

Sample Python code and wiring

import RPi.GPIO as GPIO

# Set board pin numbering system

# Setup each pin you use: whether it is input/output and if the default is on/off
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(25, GPIO.OUT, initial=GPIO.LOW)

GPIO.add_event_detect(4, GPIO.BOTH)

def my_callback():
    GPIO.output(25, GPIO.input(4))

GPIO.add_event_callback(4, my_callback)

Function references

GPIO Input

Syntax: val = GPIO( channel )

This will return either 0 / GPIO.LOW / False or 1 / GPIO.HIGH / True.

You can take a snapshot of an input at a moment in time:

if GPIO.input(channel):
    print('Input was HIGH')
    print('Input was LOW')

To wait for a button press by polling in a loop:

while GPIO.input(channel) == GPIO.LOW:
    time.sleep(0.01)  # wait 10 ms to give CPU chance to do other things

GPIO Outout

Syntax: GPIO.output( channel, state )

State can be 0 / GPIO.LOW / False or 1 / GPIO.HIGH / True.

To set an output high:

GPIO.output(12, GPIO.HIGH)
 # or
GPIO.output(12, 1)
 # or
GPIO.output(12, True)

To set an output low:

GPIO.output(12, GPIO.LOW)
 # or
GPIO.output(12, 0)
 # or
GPIO.output(12, False)

GPIO Event detection

Syntax: GPIO.add_event_detect(channel, event_to_look_for)

event_to_look_for can be:

  • GPIO.RISING - The value has moved to 1 / GPIO.HIGH / True
  • GPIO.FALLING - The value has moved to 0 / GPIO.LOW / False
  • GPIO.BOTH - The value has changed. Get the GPIO.input() function to determine which way.
GPIO.add_event_detect(channel, GPIO.RISING)  # add rising edge detection on a channel
if GPIO.event_detected(channel):
    print('Button pressed')

GPIO Event callback

Syntax: GPIO.add_event_callback(channel, my_callback)

Setup a callback function to be executed whenever a value change is detected on an input pin.

def my_callback(channel):

GPIO.add_event_detect(channel, GPIO.RISING)
GPIO.add_event_callback(channel, my_callback)

GPIO Clean up

  • Syntax: GPIO.cleanup()

By returning all channels you have used back to inputs with no pull up/down, you can avoid accidental damage to your RPi by shorting out the pins. Note that this will only clean up GPIO channels that your script has used. Note that GPIO.cleanup() also clears the pin numbering system in use.

Obtain information about your board

To discover information about your RPi:


To discover the Raspberry Pi board revision:


To discover the version of RPi.GPIO:


Further information