will be read-only starting December 31st, 2018. For more info pleae look at this Forum Post

Sharp IR Sensors library for Arduino
by Marcal - 2014


Release 1.0 09-JANUARY-2014


Release 1.0 09-JANUARY-2014 Initial Release


Easy to use library to get steady readings from a Sharp IR Range Sensors. This library is inspired by the DistanceGP2Y0A21YK library written by Jeroen Doggen (


Download zip file here

Put the SharpIR folder in "libraries\".

Open up example project included in the zip file. Read the commented out text as it explains what the library exactly does

How it works

I have experienced that Sharp IR sensors do not deliver steady readings. To my experience 12% of the readings give values with an error up to 42% over the actual distance. What this library does is to take a bunch of measurements and calculate a mean distance, taking into account only the measurements that do not differ much from one another.

To use it

1- Import the library into your sketch (you’ve already added the library to your system):
#include <SharpIR.h>

2- Initialize the library:
SharpIR sharp(ir, 25, 93, model);

		ir: the pin where your sensor is attached.
		25: the number of readings the library will make before calculating an average distance.
		93: the difference between two consecutive measurements to be taken as valid (in %)
		model: is an int that determines your sensor:  1080 for GP2Y0A21Y, 20150 for GP2Y0A02Y 					            (working distance range according to the datasheets)

3- get the distance:
int dis=sharp.distance();


A fork of this library is avaliable on GitHub :

Main difference : This lib use median instead of mean for distance computing.