GPIO Cheatsheet

import os
import time
import RPi.GPIO as g


pins = {'LED_': 1, 'LED_': 2}

def setup():
    for key, value in pins.iteritems():
        if key.startswith('LED'):
            g.setup(value, g.OUT)
        elif key.startswith('BUTTON'):
            g.setup(value, g.IN)

import RPi.GPIO as g              # import RPi.GPIO module  

# choose BOARD or BCM
g.setmode(GPIO.BCM)               # BCM for GPIO numbering
g.setmode(GPIO.BOARD)             # BOARD for P1 pin numbering

# Set up Inputs
g.setup(port_or_pin, GPIO.IN)     # set port/pin as an input
g.setup(port_or_pin, GPIO.IN,  pull_up_down=g.PUD_DOWN) # input with pull-down
g.setup(port_or_pin, GPIO.IN,  pull_up_down=g.PUD_UP)   # input with pull-up 

# Set up Outputs
g.setup(port_or_pin, g.OUT)               # set port/pin as an output
g.setup(port_or_pin, g.OUT, initial=1)    # set initial value option (1 or 0)

# Switch Outputs
g.output(port_or_pin, 1)     # set an output port/pin value to 1/GPIO.HIGH/True
g.output(port_or_pin, 0)     # set an output port/pin value to 0/GPIO.LOW/False  

# Read status of inputs OR outputs
i = g.input(port_or_pin)     # read status of pin/port and assign to variable i
if g.input(port_or_pin):     # use input status directly in program logic

# Clean up on exit

# What Raspberry Pi revision are we running?
g.RPI_REVISION #  0 = Compute Module, 1 = Rev 1, 2 = Rev 2, 3 = Model B+

# What version of RPi.GPIO are we running?

# What Python version are we running?
import sys; sys.version
    while 1:
        if g.input(25):
            print "Port 25 is 1/GPIO.HIGH/True - button pressed"  
            print "Port 25 is 0/GPIO.LOW/False - button not pressed"  
        sleep(.1)         # wait 0.1 seconds  
except KeyboardInterrupt:  
    g.cleanup()         # clean up after yourself  



Leave a Reply

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

You are commenting using your 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