I've been struggling to properly measure distances with a Sharp IR Distance Sensor GP2Y0A21.

Despite the sensor being rated for distances of 10-80 cm, I've found that after about 30-33cm, the sensor seems to no longer pick up the object it's measuring and reverts back to its output of 20cm (which it usually displays if there's no nearby object.)

I've tried many things - I bought another of the same sensor, used the sensor on different objects, and tried different code - but this issue keeps occurring.

Why could the sensor be behaving in this way? as anyone else had this problem before? And, what can I do to fix it?

By the way, I've seen people say that it's a good idea to use a capacitor with this sensor to get rid of voltage spikes. I haven't tried that yet because I think that's a different problem than what I'm facing right now.

Would using a capacitor also be worth using a try?

The two different codes I've been using to turn the voltage output into distance is from these two videos:




1 Answer 1


It can be just that your circuit is missing the important bypass cap and cannot measure properly due to ripple on supply.

Or, your object, whatever it may be, is not detectable by this sensor due to it's optical properties.

Or, the sensor is exposed to other light from other sources and can't measure properly under your light conditions.

  • \$\begingroup\$ Alright, I'll try using a capacitor on the sensor. I don't think lighting is the issue because I tried in both bright and dark conditions and it gave the same result. Also I don't think the object being measured is the issue because even using the sensor against a big object like a wall didn't work properly. \$\endgroup\$ Aug 16, 2023 at 21:36

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.