Short tutorial on Raspberry Pi’s GPIO pins in Python

Here is a short tutorial on how to use the GPIO pins in python and the labeling on them. Alot of people seem confused about this and I figured I’d explain how the labeling works. There’s a third labeling part too, but from what Ive discovered, it’s only used with shell commands and C/C++, not with python.

In python with the RPi.GPIO package, there’s two common numbering systems to describe the pins on the GPIO header, there’s the BCM layout and Board layout.

BCM labeling: The pin labeling from the BCM2835 IC itself (The main Processor), Here’s an example in python:

import time
import RPi.GPIO as GPIO
LED = 4               #Physical pin 7, BCM pin GPIO.4 on the BCM2835
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED, GPIO.OUT)

while 1:
GPIO.output(LED, True)
time.sleep(0.5)
GPIO.output(LED, False)
time.sleep(0.5)

Board Labeling: the physical pinout on the GPIO header, and here’s the same example using the same pin but using board layout:

import time
import RPi.GPIO as GPIO
LED = 7               #Physical pin 7, BCM pin GPIO.4 on the BCM2835
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED, GPIO.OUT)

while 1:
GPIO.output(LED, True)
time.sleep(0.5)
GPIO.output(LED, False)
time.sleep(0.5)

This tabel below is the labeling of both the physical board pinout (inside numbers) to the BCM layout (outside labels):

Image

I hope this helps those that are starting out with programming the GPIO pins in python on your Raspberry Pi.

Update: on version 2 raspberry pi boards, GPIO 21 is renamed GPIO 27, so dont think the pin was broken like I did. 😛

Advertisements

One Response to Short tutorial on Raspberry Pi’s GPIO pins in Python

  1. Aisha Cumbie says:

    Thank you a lot for sharing this with all of us you really recognize what you’re
    speaking about! Bookmarked. Kindly additionally visit my website
    =). We may have a link exchange contract between us

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: