Sep 012014

A few weeks ago I blogged about RPi.GPIO updates for the model B and updated my RPi.GPIO documentation and quick reference sheets. But there was one feature I held back on because I needed a bit more time to mess about with it. (And then got busy with other things.)

So What Was It? gpio_function()

Another RPi.GPIO feature that sneaked in while I wasn’t looking is

This is a feature that’s been inserted in RPi.GPIO to enable you to query the setup status of a port to see how it’s configured.

import RPi.GPIO as GPIO
port = 25
usage = GPIO.gpio_function(port)
print usage

The above code gives you the setup status of port 25 (or whatever port/pin you choose). The result will be a numerical return code, which will have one of the following values…
42 = GPIO.I2C

Why Is That Useful?

So you’ll then be able to determine the setup status of a port from within a script. I’ve written a little Python script (for B+, but could be modified for other Pis) to query the status of all the ports, then translate the numerical return codes into something a little more ‘human readable’. It uses a simple dictionary, which is a really useful feature of Python (Line 20).

#!/usr/bin/env python2.7
# demo of "gpio_function()" port test
# script by Alex Eames

import RPi.GPIO as GPIO

# Offer the user a choice of Pin or Port numbers and set numbering scheme accordingly
choice = raw_input("Type 1 for Pin numbers, anything else for BCM port numbers:\n")
if choice == "1":
    ports = [3,5,7,8,10,11,12,13,15,16,18,19,21,22,23,24,26,29,31,32,33,35,36,37,38,40]
    pin_type = "Pin"
    ports = [2,3,4,17,27,22,10,9,11,5,6,13,19,26,14,15,18,23,24,25,8,7,12,16,20,21]
    pin_type = "Port"

print "%s mode selected..." % pin_type

# Using a dictionary as a lookup table to give a name to gpio_function() return code
port_use = {0:"GPIO.OUT", 1:"GPIO.IN",40:"GPIO.SERIAL",41:"GPIO.SPI",42:"GPIO.I2C",
           43:"GPIO.HARD_PWM", -1:"GPIO.UNKNOWN"}

# loop through the list of ports/pins querying and displaying the status of each
for port in ports:
    usage = GPIO.gpio_function(port)
    print "%s %d status: %s" % (pin_type, port, port_use[usage])

What Does The Output Look Like?

GPIO.gpio_function(port) output in BCM mode

GPIO.gpio_function(port) output in BCM mode

GPIO.gpio_function(pin) output in BOARD mode

GPIO.gpio_function(pin) output in BOARD mode

The script shows what each of the ports/pins is set up to do. The screen-shots show what the output looks like in each mode.

I have spi and i2c enabled, Serial is enabled by default and all other ports are set up as inputs by default.

It’s probably something you won’t use every day, but if you don’t know it exists, you can’t use it can you? Have fun.

Click here to see the full index of my RPi.GPIO tutorials.


For an ‘extra credit’ challenge, you could modify the above script to check what revision of Pi it’s running on and choose which ports/pins to test accordingly.

RasPiO® GPIO Reference Aids

Our sister site RasPiO has three really useful reference products for Raspberry Pi GPIO work...

  1. Portsplus port ID board
  2. GPIO Ruler with RPi.GPIO code
  3. GPIO Zero Ruler with GPIO Zero code

  10 Responses to “RPi.GPIO – port function checker”

  1. Any reason for the seemingly-random ordering of your BCM ports list? ;)

    • Ooh. I’m surprised you haven’t worked that one out Andrew. :eek: (Or maybe you did but were hinting that an explanation might be a good idea?)

      Unless I’ve made a mistake, they should be in the order of “left column top to bottom, then right column top to bottom” i.e. according to the physical layout on the B+.

      • Ah, so they are! ;-)
        I’d just assumed that you’d put them in the same order as BOARD numbers, so was surprised when you didn’t. (GPIO.I2C being the first two values in both output images is what caught me out)

  2. Alex
    How did you know about this function, is there an official list somewhere that tells you of any new functions etc. when there is an update.


    • I found it in the RPi.GPIO documentation over at sourceforge. But I had to check some stuff with Ben Croston before I understood it properly.

      I noticed it in the release notes.

  3. Hi.. It will be interesting to modify your code to show PIN Status with HIGH, LOW, Error? etc… Can you pls include that change as well so everything is there to see.

  4. The copy code to clipboard function doesnt work?

  5. I keep getting the following when trying the above code in the python 2.7 shell

    Traceback (most recent call last):
    File “/home/pi/port”, line 26, in
    usage = GPIO.gpio_function(port)
    RuntimeError: No access to /dev/mem. Try running as root!

    I have updated and upgraded using the ‘sudo apt-get’ routine but no difference!

Leave a Reply