Python

GPIO Python

Setup as BCM or Board

import RPi.GPIO as GPIO 

# for GPIO numbering, choose BCM 
GPIO.setmode(GPIO.BCM) 

# or, for pin numbering, choose BOARD 
GPIO.setmode(GPIO.BOARD) 

# but you can't have both, so only use one!!!



Setup IO Input / Output

import RPi.GPIO as GPIO 
GPIO.setmode(GPIO.BCM) 

# set up BCM GPIO numbering 
GPIO.setup(25, GPIO.IN) 
# set GPIO 25 as input

Example

  1. import RPi.GPIO as GPIO  
  2. from time import sleep     # this lets us have a time delay (see line 12)  
  3. GPIO.setmode(GPIO.BCM)     # set up BCM GPIO numbering  
  4. GPIO.setup(25, GPIO.IN)    # set GPIO 25 as input  
  5.   
  6. try:  
  7.     while True:            # this will carry on until you hit CTRL+C  
  8.         if GPIO.input(25): # if port 25 == 1  
  9.             print "Port 25 is 1/GPIO.HIGH/True - button pressed"  
  10.         else:  
  11.             print "Port 25 is 0/GPIO.LOW/False - button not pressed"  
  12.         sleep(0.1)         # wait 0.1 seconds  
  13.   
  14. except KeyboardInterrupt:  
  15.     GPIO.cleanup()         # clean up after yourself