Skip to main content
 首页 » 编程设计

python之树莓派运行时错误 : Conflicting edge detection already enabled for this GPIO channel

2024年10月17日7zfyouxi

我正在按照此处找到的教程进行操作: https://www.linkedin.com/pulse/prepare-your-raspberry-pi-work-aws-iot-kay-lerch

我什至还没有开始它的互联网部分,因为我在电路方面遇到了问题。我使用我的树莓派 3 连接我的电路,如下图所示。

然后我按照教程中所示编写了以下 python 脚本。

import RPi.GPIO as gpio 
 
gpio.setmode(gpio.BOARD) 
gpio.setup(7, gpio.IN, pull_up_down=gpio.PUD_DOWN) 
 
def on_pushdown(channel): 
        print "Button Pushed." 
 
while(True): 
        gpio.add_event_detect(7, gpio.RISING, callback=on_pushdown, bouncetime=200) 
gpio.cleanup() 

当我按下按钮时,这应该打印出“Button Pushed”,但我收到以下运行时错误:

Traceback (most recent call last): 
  File "button.py", line 10, in <module> 
    gpio.add_event_detect(7, gpio.RISING, callback=on_pushdown, bouncetime=200) 
RuntimeError: Conflicting edge detection already enabled for this GPIO channel 

我有 RPi.GPIO 版本 0.6.2,这是发布这篇文章时的最新版本。如果有人能提供任何帮助,我将不胜感激。

请您参考如下方法:

您拥有的代码不断添加事件检测回调(在 while(True) 循环中)。你想要的是添加一次事件检测回调,然后等待边缘。

这page有一个很好的例子,你可能想要通过。

或者,您可以尝试以下方法:

import RPi.GPIO as gpio 
 
gpio.setmode(gpio.BOARD) 
gpio.setup(7, gpio.IN, pull_up_down=gpio.PUD_DOWN) 
 
def on_pushdown(channel): 
    print "Button Pushed." 
 
# only add the detection call once! 
gpio.add_event_detect(7, gpio.RISING, callback=on_pushdown, bouncetime=200) 
 
while(True): 
    try: 
        # do any other processing, while waiting for the edge detection 
        sleep(1) # sleep 1 sec 
    finally: 
        gpio.cleanup()