Issue
My question is: GPIO.add_event_detect() is detecting a false Rising edge continuously in an infinite loop and running the call_back function() infinitely even though I do not press the push-button connected to GPIO 25 even once But the call_back function() keeps executing.
Here is my code, where I want to call the call_back function x1() which contains the function WhatsApp(lat_o,long_o) only when the button is pressed but WhatsApp(lat_o,long_o) keeps executing without me pressing the button. Also, I put WhatsApp(lat_o,long_o) inside x1() to remove the problem of passing arguments to the call_back function.
# INTERRUPTS (NOT WORKING)
# Sample Coordinates
lat_o=33
long_o=72
# Import Libraries
import RPi.GPIO as GPIO
from time import sleep
def x1(channel):
WhatsApp(lat_o,long_o)
# Configure GPIO of Rpi
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BCM) # Use GPIO pin numbering
button1 = 25 # For WhatsApp
# Setup GPIO for Whatsapp
GPIO.setup(button1, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) # Set GPIO 25 (pin 22) to be an input pin for WhatsApp
# Detect button1-press
GPIO.add_event_detect(button1 ,GPIO.RISING, callback = x1)
try:
while True : pass
except:
GPIO.cleanup()
Please help! I don't want to execute WhatsApp(lat_o,long_o) using Polling (i.e., using if-else in a while loop) in my Final Code for my Final Year Project because I want GPIO to detect button press continuously and using Polling here will drain a lot of power of my Raspberry Pi 4.
Solution
According to this discussion on the raspsberrypi discussion
A rising edge will be detected at approx. 1.25V. However, when the voltage lowers again and the input voltage drops below the 1.16V level, a stream of interrupts will start, and continue until the level is below 1.12V.
This is most dramatic when slower rising or falling edges are present, as an example, when excessive anti-debounce measures have been taken.
This is the given solutuion
In software, I use two other techniques to get rid of the false hits. First, after the edge has been recognized by the system, we don't know if it is the one we expect. By waiting for 5mSec, and then reading the input again but now with a Logic Level command, we can determine if the edge is indeed what we expect and we then avoid and discard all other (false) edges.
And finally the code for rising edge
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
Input_Sig = 23 # any plain GPIO pin
# if there is no external pull, use the internal one (pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(Input_Sig, GPIO.IN)
def interrupt_service_routine(Input_Sig):
sleep(0.005) # edge debounce of 5mSec
# only deal with valid edges
if GPIO.input(Input_Sig) == 1:
print("RISING")
return
# define the event; bountime of 5mSec means that subsequent edges will be ignored for 5mSec
GPIO.add_event_detect(Input_Sig, GPIO.RISING, callback=interrupt_service_routine, bouncetime=5)
def main():
try:
while True:
pass # your code
except KeyboardInterrupt:
pass
finally:
print("\nRelease the used pin(s)")
GPIO.cleanup([Input_Sig])
if __name__ == '__main__':
main()
Answered By - kinshukdua