树莓派GPIO用法示例(Python)

非阻塞用法示例如下:

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

btn_input = 4;
LED_output = 17;

# GPIO btn_input set up as input.
GPIO.setup(btn_input, GPIO.IN)
GPIO.setup(LED_output, GPIO.OUT)

# handle the button event
def buttonEventHandler_rising (pin):
    # turn LED on
    GPIO.output(LED_output,True)
    
def buttonEventHandler_falling (pin):
    # turn LED off
    GPIO.output(LED_output,False)

	
GPIO.add_event_detect(btn_input, GPIO.RISING, callback=buttonEventHandler_rising) 
GPIO.add_event_detect(btn_input, GPIO.FALLING, callback=buttonEventHandler_falling)
 
try:  
    while True : time.sleep(0.1)  
finally:
    GPIO.remove_event_detect(btn_input)
    GPIO.cleanup()

参考链接


发布者

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注