Afaq Ahmed Khan
Posts: 2
Joined: Tue Dec 15, 2020 4:28 pm

GPIO.add_event_detect() not working properly!

Wed Dec 30, 2020 8:28 pm

I need to use Interrupts in my main() which contains a lot of code so i won't be attaching all of it here to avoid confusion. The main() should also detect rising edge at GPIO 25 where a push button is attached. If it detects a HIGH at GPIO 25, it should call a function my_func() regardless of whatever is being executed at that time in main(). Here is my simplified code:

Code: Select all

# Function which should be executed upon button-press
def my_func():
	# body of my_func

# Main function being executed infinitely	
main():
	# Import Libraries
	import RPi.GPIO as GPIO
	import time

	# Configure GPIO of Rpi
	GPIO.setwarnings(False) # Ignore warning for now
	GPIO.setmode(GPIO.BCM) # Use GPIO pin numbering
	button1=25 # For input

	# Setup GPIO for input
	GPIO.setup(button1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set GPIO 25 (pin 10) to be an input pin for WhatsApp

	def x1(button1):
    		my_func()

	# Detect button1-press
	GPIO.add_event_detect(button1 ,GPIO.RISING, callback = x1, bouncetime = 100)

	while True:
    		time.sleep(1)
    		print("Press the button.")
    	
    # Execute main()	
    main()
    
But, the problem i am facing is that my_func() is being executed sometimes infinitely and sometimes after some gaps even if i donot press the button for even a single time.

What i have understood is that GPIO.add_event_detect() is detecting a RISING EDGE at button1 even when the button is not pressed although it should read it as LOW as i have connected push-button to ground internally using Pull-down resistor.

One more thing:
If i execute my_func upon button-press using polling like this:

Code: Select all

def my_func():
	# body of my_func

def main():
	# Import Libraries
	import RPi.GPIO as GPIO
	import time

	# Configure GPIO of Rpi
	GPIO.setwarnings(False) # Ignore warning for now
	GPIO.setmode(GPIO.BCM) # Use GPIO pin numbering
	button1=25 # For input

	# Setup GPIO for input
	GPIO.setup(button1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set GPIO 25 (pin 10) to be an input pin 

	while True:
    		time.sleep(0.01)
    
    		if GPIO.input(button1)==GPIO.HIGH:   # ------> Detect button-press
        		my_func()
        		break
    		else:
        		print("Press the button.")

	#Clear GPIO pins after executing my_func()
	GPIO.cleanup()


 main()


The code works just fine and calls my_func() only when the button is pressed.

But as i said i want to execute my_func() as soon as the button is pressed separately in another thread and don't want to miss any button-press EVENT due to other functions in main(). So, I HAVE TO USE INTERRUPTS.

Please help!

User avatar
paddyg
Posts: 2615
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: GPIO.add_event_detect() not working properly!

Thu Dec 31, 2020 10:04 pm

Maybe the wire to the GPIO pin is acting as an aerial and picking up voltage fluctuations, I've had that with any kind of length of wire, even with debounce. What happens a) If you pull up and check for falling (don't see why that would make a difference) b) inside my_func have a tiny test loop to make sure the button is really being pressed

Code: Select all

def my_func():
    for i in range(10):
        if GPIO.input(button1) != GPIO.HIGH:
            return
        time.sleep(0.01)
    # only now do the functionality
c) if you mix tabs and spaces in your code at some stage you will spend hours or days trying to debug incomprehensible behavior. d) I'm not sure what the point of x1() is e) I would strongly recommend that you switch to using gpiozero: there are loads of good documentation with recipes and the module is really well put together.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Afaq Ahmed Khan
Posts: 2
Joined: Tue Dec 15, 2020 4:28 pm

Re: GPIO.add_event_detect() not working properly!

Sat Jan 02, 2021 12:11 am

Thankyou for your recommendations paddyg. I will sure try them out and let you now.
I am also thinking that that the GPIO is picking up false button presses. But the question is how to solve it and use GPIO.add_event_detect() interrupt in my code.

Now to answer your questions:
a) Yes, i have tried it and checked for falling but it still produces the same error and my_func() runs infinitely.

b) Ok i will try it out and let you know the result.

c) I am working in Jupyter notebook, so tabs and spaces or Syntax error is Not the issue here because Jupyter's compiler lets you know every error in detail.

d) I tried it out after some internet research which said that the callback function argument in GPIO.add_event_detect() should have the input channel (in my case, button1) as its argument.So, i called my_func() which already has 2 arguments in my original main() code, using x1(button1) callback function due to argument mismatch. I hope you understand my point.

e) Yes, I did try to use gpiozero and used its function button.when_pressed() to generate interrupt to call my_func() but unfortunately and strangely it also generated the same error and my_func() was being called infinitely, AGAIN!

User avatar
joan
Posts: 15700
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: GPIO.add_event_detect() not working properly!

Sat Jan 02, 2021 8:50 am

Try http://abyz.me.uk/rpi/pigpio/piscope.html

It will show you what is going on with the button.

Return to “Python”