I have a problem working with some IR Sensors. They are supposed to trigger "my_callback" when they do not sense an IR signal. At the moment they trigger as soon as they DO recieve an IR signal.
I made following script to show what my problem is:
import RPi.GPIO as GPIO
import time
channel = 23
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def my_callback(channel):
print("test")
while True:
GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback, bouncetime=10)
"test" gets printed when the sensors recieve a signal but it should be the other way around. I have tried GPIO.FALLING and PUD_UP but it still acts the same way.
Does someone know how to do this?
Thanks in advance.
1 Answer 1
The IR sensors I have come across are active low, i.e. they are normally high but will pulse low and high as they receive an IR signal.
This together with the fact you only get transitions between low and high when a signal is being received explains what you are seeing.
The following script shows the sort of logic you need to add to detect this state. pigpio has a watchdog function which triggers when a GPIO is inactive which makes the logic slightly simpler.
You can test by switching PWM on and off for the GPIO.
E.g. pigs p 4 100
will show active and pigs p 4 0
will show inactive.
#!/usr/bin/env python
# listen.py
# 2018年02月16日
# Public Domain
# listen.py # listen on GPIO 4
# listen.py 23 # listen on GPIO 23
import sys
import time
import pigpio
inactive = False
def cbf(GPIO, level, tick):
global inactive
if level == pigpio.TIMEOUT:
if inactive == False:
inactive = True
print("Inactive")
else:
if inactive == True:
inactive = False
print("Active")
pi = pigpio.pi()
if not pi.connected:
exit()
if len(sys.argv) == 1:
G = 4
else:
G = int(sys.argv[1])
cb = pi.callback(G, pigpio.EITHER_EDGE, cbf)
pi.set_watchdog(G, 30) # get alert if quiet for 30 milliseconds
try:
while True:
time.sleep(60)
except KeyboardInterrupt:
print("\nTidying up")
cb.cancel()
pi.stop()
-
Okay, your explanation for the behavior makes sense to me. From what I see I can only check for GPIO.RISING or GPIO.FALLING. Is there some way to check for != RISING or FALLING?Dave 494– Dave 4942018年02月16日 11:18:05 +00:00Commented Feb 16, 2018 at 11:18
-
You mean can you check that there is no change? I would start a timer after the last change up/down and say it was static after 20 milliseconds or so.joan– joan2018年02月16日 11:21:52 +00:00Commented Feb 16, 2018 at 11:21
-
What I'm trying to do is calling "my_callback" once something is between my IR sensor and IR LED. I just don't know how this is possible with event_detect because both FALLING and RISING call when the sensor gets a signal..Dave 494– Dave 4942018年02月16日 11:40:27 +00:00Commented Feb 16, 2018 at 11:40
-
There is no such event. When active the line will be pulsing up and down several times a millisecond. There will be a 10 millisecond or so quite gap between repeats if the remote is held. After 20 milliseconds or so with no pulses you can assume there is no IR signal.joan– joan2018年02月16日 12:44:11 +00:00Commented Feb 16, 2018 at 12:44
-
Your program on it's own works really great for me. I, however, do not really understand how you call cbf. All I can see is you using cbf in pi.callback. As you may have noticed by now, I'm not exactly good with python. I don't even really understand how calling functions and stuff works. Would you potentially help me integrate the code you wrote into my script?Dave 494– Dave 4942018年02月23日 19:05:52 +00:00Commented Feb 23, 2018 at 19:05