what is the easiest way to make a gate that detects if something passes through a gap?
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 &quot;Mission Impossible&quot; to hit on trying to slither underneath it in order to stealth a stinker.  If there's any aversion to the &quot;robot&quot; 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 &quot;Arduino project&quot; (/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 &quot;stuck&quot; 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 &quot;spring&quot;
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 &quot;blob&quot;) 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 &quot;Arduino project&quot; 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.