User Guide

163
import RPi.GPIO
import time
6. On a new line, type the following statement:
RPi.GPIO.setmode(RPi.GPIO.BCM)
setMode() is a function in the RPi.GPIO module that sets how you
are going to refer to pins. There are two options for this:
BOARD states that when you pass the value 2 into
functions in the RPi.GPIO module, you are referring to
pin 2.
BCM states that when you pass the value 2 into
functions in the RPi.GPIO module, you are referring to
GPIO2.
Now you need to configure GPIO2 as an output:
• On a new line, type the following statement:
RPi.GPIO.setup(2, RPi.GPIO.OUT)
The function setup() accepts two arguments: the first is the pin or
GPIO number (depending on whether you use BOARD or BCM
in the call to setMode()); and the second can be RPi.GPIO.IN or
RPi.GPIO.OUT. To make a pin an output, use RPi.GPIO.OUT.
Now add the following code to the script and save the file:
while True:
RPi.GPIO.output(2, True)
time.sleep(1)
RPi.GPIO.output(2, False)
time.sleep(1)
The function output() sets the output pin high when the second
argument is True. It sets the output pin low when the second
argument is False. If you prefer, you can use RPi.GPIO.HIGH and
RPi.GPIO.LOW instead of True and False.
The call to sleep() causes Python to wait one second before
moving on to the next instruction. If you want to blink the LED
slower, increase the value that you pass into sleep(). For