I have a home project with a device with an opening that is about 20cm (8 inches) wide. I want to attach two sensors on the sides of the opening, such that if an object passes between them, a switch is shorted (either momentarily and then disconnected, or as long as the object stays in between).

What is the easiest way to do this?

I am a software engineer but I work "close to the metal" - I am reasonably good with EE: I can solder, use meters, put together my own computers from parts. If you tell me what the method is and what the required parts are, I can probably figure out the connections required.

The objects I want to detect passing, are big, at least a quarter of the size of the opening - 5 cm, and they do not move quickly - they may spend at least half a second moving through the opening.

False positives - I can have them occasionally, say once an hour. False negatives are less OK - I would like to have, say, less than 10% chance of non-detection.

The thing is on a balcony shielded from rain, sometimes it is very dark, sometimes it is sunlight.

The objects are cats :) The device is a litter robot, which, while otherwise a perfect device, suffers from one stupid design choice, where a cat depresses a spring upon entering, which is highly unreliable - sometimes the spring gets "stuck" and won't depress, sometimes it stays depressed all the time, and this varies even if the spring is not adjusted and is kept clean. Detection of cat passing through the opening by means of tripping an electronics sensor would be far preferable.

I guess a single "Proximity sensor" that detects a moving object within 20cm or 8 inches, that was not there before, would be good.

  • \$\begingroup\$ How wide is the object compared to the gap? Easy if the object is 10cm wide, not so easy if it is, say, 5.6mm and moving quickly. \$\endgroup\$ Commented Oct 7, 2020 at 14:54
  • \$\begingroup\$ Thank you Sphero! I will post additional information to answer your question. \$\endgroup\$ Commented Oct 7, 2020 at 14:55
  • \$\begingroup\$ Light could still be used even in sunlight if it is looking for a signal in the light, not just on/off. \$\endgroup\$
    – Justin
    Commented Oct 7, 2020 at 15:06
  • \$\begingroup\$ OK Justin, I see, yes, thank you. \$\endgroup\$ Commented Oct 7, 2020 at 15:07
  • \$\begingroup\$ That the object only partially fills the gap is challenging. Please provide a drawing of the setup. Could you offset sensors to look for the reflection of pulsed light, rather than looking for partial obstruction of a beam?. Could you use a video solution, eg, opencv on an embedded Linux board? Could you have a lightly sprung mechanical door the object will push open? Please state what the objects actually are and what purpose this is for, keeping things abstract is silly and inefficient as issues or ideas obvious with that knowledge get overlooked without \$\endgroup\$ Commented Oct 7, 2020 at 15:12

8 Answers 8


The objects are cats :) The device is a litter robot, which, while otherwise a perfect device

If you constrain the opening at least vertically, it should be possible to arrange an optical path across the opening such that the cat's body must entirely block it at some point in the entrance. Cats will actually fit through quite small openings (if they want to) so you could even constrain the opening in width as well as height, but there's no real need to, since a break-beam system can easily cover far more distance than you need. Even with the existing opening you can probably just put the beam at a height to be broken by the center of the cats body during normal walking... they're clever creatures but they probably haven't paid enough attention that time you streamed "Mission Impossible" to hit on trying to slither underneath it in order to stealth a stinker. If there's any aversion to the "robot" give it a ten minute delay so it doesn't activate while the cat is still nearby.

In terms of actual components, you'll want to use an IR LED toggled on and off at 38 KHz, and a matching receiver which looks only for that, and ignores ambient light. Typical receiver modules also include an AGC as they are looking for bursts of pulses (eg from a TV remote) so you'll either need to get a receiver without AGC, or else have your transmitter output a series of 38 KHz pulses, give a gap, then another series so that it looks not unlike a TV remote control signal - try something like 5-10 pulses, then a gap of the same amount of time. Such a pattern is easily implemented in a small MCU, which can also help with the detection rules. So basically you have something along the lines of an "Arduino project" (/ATtiny, Cortex M0, MSP430, whatever) to pulse the LED, monitor the receiver, and then probably activate a small relay to trigger the mechanism (unless you can reverse engineer its circuitry enough to drive directly).

You'll also want to build a sort of tubular shield around the emitter and detector to make sure they only see each other directly through the a narrow region of the center of the path to be occupied by the cat.

where a cat depresses a spring upon entering, which is highly unreliable - sometimes the spring gets "stuck" and won't depress, sometimes it stays depressed all the time, and this varies even if the spring is not adjusted and is kept clean.

You could also consider some sort of flap, and detect movement of the flap optically or magnetically rather than by means of electrical contact to avoid the flaws of the present "spring"

There is another option overlooked, of detecting the cat inside the litter box. If the box is tall enough, that is the place you could potentially put an ultrasonic sensor, by having it inside looking down, but due to ringing issues these have a minimum detection distance which you may or may not be able to achieve. Such a method would definitely point to software interpretation - you need software range-gating to ignore the floor of mounded litter, and see the cat as a closer reflection; then you probably also need to entirely ignore the situation for a bit after triggering the cleaning mechanism, so you don't re-trigger off that. As mentioned in comments, the response of the cat to an ultrasonic system should be evaluated before committing to such a path. At the cost of higher power, identifying a cat inside the litter box (or at least a "blob") should also be well within the capability of computer vision algorithms watching a webcam given suitable IR illumination.

If you had an ultrasonic system with direct software signal interpretation of the received signal, then you could fire it out the door and look for a reflection from an approaching cat in addition to the spurious one from the door aperture. But the cheap "Arduino project" modules just give you the output of the first strong reflection, and you may have a lot of trouble making the door aperture not be that. Doppler ultrasound could work, too, but again points to having a far more custom circuit where you can actually see the analog response, and not just get an pre-(mis)interpreted output as from a little HC-SR04 module. One advantage of detecting the cat inside the box is that you have longer to do it, so you could have a battery powered system which polled at a much lower rate to preserve battery life, but designing systems which are truly low power without unintended power drain when they are supposed to be asleep is practically far more tricky than it first looks.

Microwave doppler motion detects exist, too. Getting one to see the cat would be easy; the problem would be having it not see far beyond the enclosure to say tree branches swaying in the wind. And doppler radar just loves fans of any sort... was testing a module and couldn't figure out why the output was incessant, until I looked up at the slowly rotating ceiling fan.

  • \$\begingroup\$ Thank you! However after some consideration, I think the easiest thing for me to do, is to decouple the spring whose behaviour is too unpredictable in the whole device, from the device. That is, I will cut the spring mechanism out and mount in on the floor nearby, without the spring I can now fix the position of the robot, and then mount a small board where the cats step so that it moves up and down and presses the spring (which can now be adjusted to react to a slightest push). \$\endgroup\$ Commented Oct 7, 2020 at 19:03
  • \$\begingroup\$ +1 for "stealth a stinker" \$\endgroup\$ Commented Oct 8, 2020 at 5:31
  • 4
    \$\begingroup\$ Cats are reported to be able to hear up to 64 kHz, so it might be best to find out if they are scared off by an ultrasonic (to us) sensor before getting far into using one. \$\endgroup\$ Commented Oct 8, 2020 at 9:03
  • \$\begingroup\$ Ultrasonic would be great, but it must be ultrasonic for cats as well, as Andrew noted. Cats are fuzzy, so there would be tons of contrast for the sensor to detect, but lots of hobby ultrasonics work around 40kHz, which would definitely be noticeable to the cats. Getting up to 80-100kHz can get expensive, and there are fewer choices. \$\endgroup\$
    – J...
    Commented Oct 8, 2020 at 13:10

I would put the whole litter box on a scale (ie, weight sensor). When the cat gets in, it'll register heavier weight, and when the cat leaves, lower weight. You could even make statistics on how much "stuff" the cat left in the litterbox...

You can get a sensor for free in a busted kitchen scale. Or you could just use a switch under a spring, adjust spring strength so it doesn't switch with just the litterbox, but it does switch with a cat on top.

Note this doesn't detect movement (ie, cat goes through the opening), it detects a state (cat inside the box or not) which gives more information.

Unlike IR sensors, weight sensors consume very low power, which is an advantage if the thing is battery powered.

Failing that, you can get readymade IR trip sensors for so cheap that it isn't worth rolling your own. These things are commonly mounted on shop doors to ring a bell when a customer enters.

  • \$\begingroup\$ Thank you, but scale does not work. You have varying amount of litter, varying weight of litter per volume, cats depositing weight :) \$\endgroup\$ Commented Oct 7, 2020 at 18:31
  • 2
    \$\begingroup\$ @MarkGaleck That's easy enough to solve in software. For example, weight_now > weight_average_1_hour + cat_weight * 0.5 \$\endgroup\$
    – jpa
    Commented Oct 8, 2020 at 6:17
  • \$\begingroup\$ If you want to go with the weight idea, the thing to do would be to look for the weight to increase during the visit, and then decrease again to pretty close to what it was before. The robot doesn't have to activate instantly, and it probably should wait at least a minute after departure is detected to avoid annoying the cat. In practical terms, you can scrounge a load cell out of some consumer scale and wire it instead to an HX711 and from there to an Arduino or similar. \$\endgroup\$ Commented Oct 8, 2020 at 15:42

I think you could use a set of IR receiver modules and LEDs opposite (typically you'd pulse them at something like 38kHz, a 555 would work, the frequency should match the receiver modules). Put the IR receivers and transmitters in black tubes (rectangular or round) to limit their field of view.

The modulated light and optical filtering greatly reduces the sensitivity to ambient light.

If any of the receivers are obstructed then that would be interpreted as an object in the way. Since the module linked above has a low output when the modulated light is detected, you would want an 'n' input "AND" gate to detect the loss of any signal.

Edit: As @Chris mentions in a comment, should pulse the modulated light so the on-chip AGC can do its magic. So you may as well use something like a $2 Arduino Nano to generate the pulses, do the AND gate and do whatever other logic makes sense. You would want to use a 5V module in that case.

  • 1
    \$\begingroup\$ This won't actually work with just a 555, as the AGC in those detectors wants to see a burst of pulses not a steady stream, which it will typically ignore. One could use a 556, with the second timer creating bursts of the first, but then the receiver needs to be a missing pulse detector so you need a third. And maybe a fourth for the output pulse. In theory it's all possible, but it points so strongly towards software on a low end MCU, which can add extra rules to for false re-triggering, clean once a day regardless, etc. \$\endgroup\$ Commented Oct 7, 2020 at 16:07
  • \$\begingroup\$ @ChrisStratton Okay, good point. In which case OP might as well use an Arduino. \$\endgroup\$ Commented Oct 7, 2020 at 16:08

IR Remote receiver is a simple-to-use 3-pin module that has an engineering history that makes them very robust in various optical environments like darkness and sunlight.
IR Remote detector
Sensitivity is so high (an infrared light beam from about 10m away can be detected) that you may have to drive an infrared LED light source with a very small current. 20 cm should be no problem. The IR receiver includes an automatic gain control circuit (AGC) that accommodates a wide range of optical signal strengths.
IR LED drives IR Remote detector
The biggest problem is driving the infrared LED transmitter with appropriate signal. It must be turned on/off at about 39,000 pulses per second. Not only that, but it must be further-pulsed at about 1kHz. This complex pulse train is appropriately done with a microcontroller. The infrared LED can be driven from a digital input-output pin,and the IR Remote receiver can be read from a digital input pin. Usually a resistor is added in series with the infraRed LED to limit current - the value of this resistor can be adjusted to set appropriate sensitivity.
pulse train
If your object-passing-through this optical gate has a complex shape, you may detect the passing of object parts (legs, heads, tails) rather than objects themselves. When these objects block the infra-red beam, the IR receiver output pin remains logic high.
Images taken from Vishay IR-remote data sheets.


Maybe this is a case of having a hammer making every problem look like a nail, but as you're a software engineer, have you considered a software solution? Namely using a camera connected to something like a Raspberry Pi or a home server.

There are a few related projects already out there:

These hint at the requirements but your problem is easier- just cat vs. no cat, and for that you could use OpenCV looking for big enough changes.


If it can be placed in the dark: Put an LED and a photodiode on the other side and a reflector on the other. This way you only have to run wires on one side. Or you can put the components on different sides too: https://www.homemade-circuits.com/how-to-connect-ir-photodiode-sensor/ There are ready-made kits also available (photogate / light gate / light curtain)

Otherwise you could use a ultrasonic range finder: https://www.circuitbasics.com/how-to-set-up-an-ultrasonic-range-finder-on-an-arduino/

  • 1
    \$\begingroup\$ A simple photodiode interpreted by level is unrealistically primitive; to work over this range in the open the light source should be switched and a receiver which specifically detects that switching used. Affordable ultrasonic solutions will not work over this short a distance unless the sensor can be offset from the start of the detection window - there's a minimum detection distance to account for ringing of the transmitter. \$\endgroup\$ Commented Oct 7, 2020 at 15:06
  • \$\begingroup\$ Use a phototransistor for this instead of a photodiode, an opaque black visible immune one. Much simpler. \$\endgroup\$
    – DKNguyen
    Commented Oct 7, 2020 at 15:11
  • 2
    \$\begingroup\$ @DKNguyen no, use an IR receiver module which looks for light pulsed at a particular frequency. But the object will probably not block enough of the beam. \$\endgroup\$ Commented Oct 7, 2020 at 15:13
  • \$\begingroup\$ There isn't enough enough information to give the exact answer. LEDs and photodiodes/-transistors are the DIY option if you want to learn electronics. If this is a serious industrial application I'd probably go with a light curtain or some other laser based industrial sensor. \$\endgroup\$
    – Ralph
    Commented Oct 8, 2020 at 8:52
  • \$\begingroup\$ And as you mentioned, smaller modules are the perfect solution for many applications. \$\endgroup\$
    – Ralph
    Commented Oct 8, 2020 at 8:58

If these are always the same cat(s), you could use an RFID or other proximity-read device embedded in a collar or attached to each cat in some way. Provided you use readers with the right range (you don't want long-range - then you don't know if the cat simply wandered by but didn't enter) this could work quite well. RF, so not subject to the same interference issues as visible light and (to a lesser degree) infrared.

  • \$\begingroup\$ If the cats are microchipped, use that - also RFID but internal so they can't remove it unlike any collar that isn't a strangulation hazard. Microchipping is quite common for cats in many places (and where I am it's compulsory for dogs). \$\endgroup\$
    – Chris H
    Commented Oct 8, 2020 at 7:00

Use a dedicated Infrared optical sensor chip for this purpose.

Using Just Photodiode and Infrared LED will cause, lots of problem like,

  • Interference from Sun Light.
  • Interference from other Household Light.
  • No 50/60 Hz AC Noise Filtering.
  • Bad Accuracy.

It's better to go with a specialized IR optical sensor for this purpose. They have decent accuracy and have all kinds of filters. Some of the features are,

  • PWM for IR to control IR LED Power.
  • Filtering of 50/60Hz AC Noise.
  • Programmable sensing distance.
  • Object Distance measurement.
  • Small footprint, less space on PCB.
  • 2
    \$\begingroup\$ Yes, but there's a lot more to it than that... \$\endgroup\$ Commented Oct 7, 2020 at 16:15

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.