Search code examples
pythonpython-2.7raspberry-pigpioraspberry-pi3

Raspberry Pi RuntimeError: Conflicting edge detection already enabled for this GPIO channel


I was following a tutorial found here: https://www.linkedin.com/pulse/prepare-your-raspberry-pi-work-aws-iot-kay-lerch

I have not even begun the internet part of it as I was having issues with the circuit. I wired my circuit just like it is shown in this diagram below using my raspberry pi 3. enter image description here

I then wrote the following python script as shown in the tutorial.

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()

This should print out "Button Pushed" when I push the button but I get the following runtime error:

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

I have RPi.GPIO version 0.6.2 which is the latest version at the time o fthis post. I would appreciate any help that anyone can provide.


Solution

  • The code you have is adding an event detection callback constantly (in the while(True) loop). What you want is to add the event detection callback once and then wait for an edge.

    This page has a good example you might want to go through.

    Alternatively, you could try something like:

    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()