ledPin = 11 # RPi Board pin11
def setup():
GPIO. setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO. setup(ledPin, GPIO. OUT) # Set ledPin to output mode
GPIO. output(ledPin, GPIO. LOW) # Set ledPin to low level to turn off led
p rint ('using pin%d'%ledPin)
In loop(), there is a while cycle, which is an endless loop. That is, the program will always be executed in this
cycle, unless it is ended outside. In this cycle, set ledPin output high level, then LED is turned on. After a period
of time delay, set ledPin output low level, then LED is turned off, which is followed by a delay. Repeat the
cycle, then LED will start blinking.
def loop():
w hile Tr ue:
GPIO.output(ledPin, GPIO. HIGH) # led on
print ('...led on')
time.sleep(1)
GPIO.output(ledPin, GPIO. LOW) # led off
print ('led off...')
time.sleep(1)
Finally, when the program is terminated, subfunction will be executed, the LED will be turned off and then the
IO port will be released. If close the program terminal directly, the program will be terminated too, but destroy()
function will not be executed. So, GPIO resources won’t be released, in the warning message may appear next
time you use GPIO. So, it is not a good habit to close the program terminal directly.
def destroy():
GPIO. output(ledPin, GPIO. LOW) # led is turned off
GPIO. cleanup() # Release resource
This is a Python module to control the GPIO on a Raspberry Pi. It includes basic output function and input
function of GPIO, and function used to generate PWM.
Set the mode for pin serial number of GPIO.
mode=GPIO.BOARD, which represents the GPIO pin serial number is based on physical location of RPi.
mode=GPIO.BCM, which represents the pin serial number is based on CPU of BCM chip.
Set pin to input mode or output mode. “pin” for the GPIO pin, “mode” for INPUT or OUTPUT.
Set pin to output mode. “pin” for the GPIO pin, “mode” for HIGH (high level) or LOW (low level).
For more functions related to RPi.GPIO, please refer to:
https://sourceforge.net/p/raspberry-gpio-python/wiki/Examples/
“import time” time is a module of python.
https://docs.python.org/2/library/time.html?highlight=time%20time#module-time