# An Arduino library for Sharp distance sensors

**URL:** https://forum.pololu.com/t/an-arduino-library-for-sharp-distance-sensors/12190
**Category:** Software and microcontrollers
**Created:** [March 18, 2017, 1:28am UTC](https://forum.pololu.com/t/an-arduino-library-for-sharp-distance-sensors/12190 "2017-03-18T01:28:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DrGFreeman](https://forum.pololu.com/user_avatar/forum.pololu.com/drgfreeman/32/5198_2.png) [@DrGFreeman](https://forum.pololu.com/u/DrGFreeman)
#### Post date: [March 18, 2017, 1:28am UTC](https://forum.pololu.com/t/an-arduino-library-for-sharp-distance-sensors/12190/1 "2017-03-18T01:28:45Z")

</div>

I was looking for an Arduino library to convert the signal from the [Sharp GP2Y0A60SZLF Analog Distance Sensor (5V)](https://www.pololu.com/product/2474) I use in my [Mini-Sumo project](https://forum.pololu.com/t/new-mini-sumo-project/12072) 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](https://github.com/DrGFreeman/SharpDistSensor). 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](https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification) was a great learning experience. The [Arduino Library Tutorial](https://www.arduino.cc/en/Hacking/LibraryTutorial) as well as simply looking at the different [Pololu libraries](https://github.com/pololu) 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)](https://www.pololu.com/product/2474) 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](https://www.pololu.com/blog/654/continuous-testing-for-arduino-libraries-using-platformio-and-travis-ci) 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.

---

<div class="post-metadata">

### Author: ![AmandaS](https://forum.pololu.com/user_avatar/forum.pololu.com/amandas/32/753_2.png) [@AmandaS](https://forum.pololu.com/u/AmandaS)
#### Post date: [March 21, 2017, 12:14am UTC](https://forum.pololu.com/t/an-arduino-library-for-sharp-distance-sensors/12190/2 "2017-03-21T00:14:02Z")

</div>

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

---

<div class="post-metadata">

### Author: ![DrGFreeman](https://forum.pololu.com/user_avatar/forum.pololu.com/drgfreeman/32/5198_2.png) [@DrGFreeman](https://forum.pololu.com/u/DrGFreeman)
#### Post date: [April 5, 2017, 2:48am UTC](https://forum.pololu.com/t/an-arduino-library-for-sharp-distance-sensors/12190/3 "2017-04-05T02:48:48Z")

</div>

Thanks to @ryantm’s suggestion and support, the [DrGFreeman/SharpDistSensor](https://github.com/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!

---

<div class="post-metadata">

### Author: ![ryantm](https://forum.pololu.com/user_avatar/forum.pololu.com/ryantm/32/8302_2.png) [@ryantm](https://forum.pololu.com/u/ryantm)
#### Post date: [April 6, 2017, 7:45pm UTC](https://forum.pololu.com/t/an-arduino-library-for-sharp-distance-sensors/12190/4 "2017-04-06T19:45:29Z")

</div>

Hi.

We shared this on the Pololu Blog:

> **[Pololu - Arduino library for Sharp distance sensors](https://www.pololu.com/blog/683/arduino-library-for-sharp-distance-sensors)**
>
> Julien de la Bruère-Terreault (also known as DrGFreeman on the Pololu Forum, creator of the Custom Mini Sumo robot and the Romi and Raspberry Pi robot shared on this blog) made “SharpDistSensor” an Arduino library for analog Sharp distance sensors....

Thanks for all your contributions!

Ryan
