An Arduino library for Sharp distance sensors

I was looking for an Arduino library to convert the signal from the Sharp GP2Y0A60SZLF Analog Distance Sensor (5V) I use in my Mini-Sumo project to a distance value. The features I was looking for in a library for were:

  1. Possibility of using a fifth order polynomial fit curve
  2. Possibility to perform real-time (moving window) median filtering on the signal with an adjustable filter window size.

I searched for existing libraries that would offer these features but didn’t find any. The libraries I found were either using look-up tables or power functions instead of polynomial. Some offered median filtering but not in real-time (filtering on a batch of readings at a time only).

So I decided to write my own library. This is the first library I write for Arduino. Most of my coding experience so far was in Python or Matlab so the process of learning how to write classes in C++ and package the code to meet the Arduino library specification was a great learning experience. The Arduino Library Tutorial as well as simply looking at the different Pololu libraries were of great help in the process.

I made the library to be simple to use with default calibration coefficients for the Sharp GP2Y0A60SZLF Analog Distance Sensor (5V) built-in whilst still offering the possibility to define custom calibration coefficients, allowing its use with different sensors and/or with different units. Hopefully the library is documented well enough so that it can be useful to others.

Also, after reading @ryantm’s blog post on continuous testing with PlatformIO and Travis CI, I decided to give it a try and put my library to the test, which succeeded!

As this is my first Arduino library and I am far from being a professional programmer, feel free to have a look and provide me feedback on how I could improve it.

P.S. To the forum moderators; I posted this in the “Software” section, feel free to move it to “Sensors” if you feel it is more appropriate.


Hello, DrGFreeman.

Thanks for sharing your Arduino library with us! I’m sure others will find it quite useful, especially the features you mentioned.

- Amanda

1 Like

Thanks to @ryantm’s suggestion and support, the DrGFreeman/SharpDistSensor library has been added to the Arduino Library Manager and can be installed and updated directly from the Arduino IDE under the name SharpDistSensor.

The latest version also has a new method to set the calibration by pre-defined model names (see usage in the reference section of the README file and in the example files).

For now, only one model is defined (SharpDistSensor::GP2Y0A60SZLF_5V) however, pull requests to add other models are welcome. Alternatively, you can contact me directly to add a model if you have the calibration curve.

Hoping it can be useful for your projects!

1 Like


We shared this on the Pololu Blog:

Thanks for all your contributions!


1 Like