An Arduino library for interfacing with Sharp IR range finders.
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is even with nlamprian:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


This is an Arduino library for interfacing Sharp IR range finders. Currently there are three models supported, GP2D120XJ00F, GP2Y0A21YK, GP2Y0A02YK0F. Their transfer functions have been extracted from the datasheets, by fitting a model on a series of datapoints. These collections of data are present for reference in the curve_fitting folder.

Curve Fitting

Apparently, not all units have the same response characteristic. So, I have also implemented a process to extract such a characteristic for your own sensor.

Curve Fitting

In the curve_fitting folder:

  • There is a Python script, accompanied by an Arduino sketch CurveFitting, so as those two together, and with a little bit of your help, they are going to take some measurements, do a bit of processing, and put the data in a file datapoints.csv.
  • Next, there is a Python script and a MATLAB script curve_fitting.m, and you can use either one (it's up to you which one), to fit a model to the data, evaluate the results, and compare the estimated model with the one from the datasheet. You can find more details on the headers of the corresponding files.

It's not neccessary to execute this process, it might be that your sensor is already working pretty well. But if you think you need that extra accuracy for your project, now you can have it out of the box.

Basic Functionality

  • Set a model of your choice for the sensor (Sum of exponentials, 5th order polynomial).
  • Get values from the sensor in raw form, in volts, and in units of length (cm, in).
  • Set/Reset the voltage reference for the sensor's output measurement.
  • Take multiple measurements that are averaged, in order to cancel out some of the sensor's output fluctuation.
  • Set a point of reference for the sensor's measurements. You can measure the distance of an object from a point (on the same axis) other than the sensor's position.
  • Operate the sensor in discrete mode by providing a threshold for the distances of objects.


There is one sketch for the curve fitting process, CurveFitting, and two additional example sketches, DistanceFromObject and StayAway, that expose some of the interface and functionality of the library.