Initialization

  • Importing the ‘RPI.GPIO’ library
import RPi.GPIO
  • Select a pin numbering system
#BOARD numbering system
GPIO.setmode(GPIO.BOARD)

#BCM numbering system
GPIO.setmode(GPIO.BCM)
  • Configure channel as an input:
#With Pull-up resistor
GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#With Pull-down resistor
GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  • Configure channel as an output:
GPIO.setup(channel, GPIO.OUT)

I/O configuration

  • Poll GPIO pins
if GPIO.input(channel):
    print('Input was HIGH')
else:
    print('Input was LOW')
  • Input detection using Wait_for_edge() function
GPIO.wait_for_edge(channel, GPIO.RISING)
  • Set GPIO pin as high/low
#Set output as high
GPIO.output(<CHANNEL>, GPIO.HIGH)

#Set output as low
GPIO.output(<CHANNEL>, GPIO.LOW)

Clean-up

To clean-up any used resources, you can call the following :

GPIO.cleanup()

Source