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.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def my_callback(channel):

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
      if inactive == True:
         inactive = False

pi = pigpio.pi()

if not pi.connected:

if len(sys.argv) == 1:
   G = 4
   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

   while True:
except KeyboardInterrupt:
   print("\nTidying up")

  • 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
    Commented 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
    Commented 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
    Commented 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
    Commented 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
    Commented Feb 23, 2018 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.