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. 😛

Basic logic analyzer on raspberry pi using PICKit2

I discovered this little trick for all of you out there that uses the rpi for the electronics projects like I do.

First, go to this link and download the pk2-la zip file, and extract it somewhere like a Downloads folder on the rpi.

http://sourceforge.net/projects/pk2-la/

Then go “sudo apt-get install python-usb”, which will install the USB stuff for python, and just run the pk2-la executable through root command line, or “sudo pk2-la”. Make sure your pickit2 is plugged in before you run it tho otherwise it will not run.

I copied the pk2-la file, LA-Format file, and IO-Format file to /usr/bin so I could just call it from anywhere in the command line by using “sudo pk2-la”.

You should see a window pop up after a few seconds looking like this:

Image

You need to hook 5v from your circuit to pin 2 and gnd to pin 3/middle pin, then the other three inputs are shown in this diagram:

Image

And there you go, a 3 channel logic analyzer for your Raspberry Pi 😛

All credit for pictures go to Josejx at http://sourceforge.net/projects/pk2-la/ and Microchip.