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

前端 未结 2 2027
一生所求
一生所求 2021-01-07 04:10

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

相关标签:
2条回答
  • 2021-01-07 04:47

    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()
    
    0 讨论(0)
  • 2021-01-07 04:52

    Doddie's answer has the problem that the program exits after the first button press. Since the author mentioned an eternal loop, I would like to propose the following:

    #!/usr/bin/python3
    
    import RPi.GPIO as gpio
    import time
    
    pin = 7 
    
    def on_pushdown(channel):
        print "Button Pushed."
    
    try:
        # Setting GPIO layout
        gpio.setmode(gpio.BOARD)
        # Set pin as input pin pulled down to GND
        gpio.setup(pin, gpio.IN, pull_up_down=gpio.PUD_DOWN)
    
        while True:
            if not 'event' in locals():
                event = gpio.add_event_detect(pin, gpio.RISING, callback=on_pushdown, bouncetime=200)
            else:
                time.sleep(1)
    
    finally:  
        gpio.cleanup()
    

    If the button is pressed and an event is detected, the variable event is being deleted. Hence, the add_event_detect-function is not called twice in a row.

    Additionally two things:

    1. Depending on your setup, I would use a pull-up resistor. A pull-down setup is more prone to noise and an induction flank from somewhere might be interpreted as a button press.
    2. Although this is not best practice: If your setup is really small (like inside the RPi case) and you don't accidentally set gpio.OUT and you just have a push button, you might consider to just take the build-in pull-up resistor and directly connect the push button to GND: GND --/ -- GPIO.IN (and then detect for gpio.FALLING).
    0 讨论(0)
提交回复
热议问题