Go Down

Topic: Library for Distance Sensors (Read 7 times) previous topic - next topic


Dec 19, 2011, 10:44 pm Last Edit: May 21, 2012, 10:27 pm by doggenj Reason: 1
I am starting this topic to inform you about a small lookup table based library for the Sharp GP2Y0A21YK analog IR distance sensor.
The main goal is to provide a library that simplifies the usage of this particular sensor in Arduino projects.


  • Download the source

  • Place the DistanceGP2Y0A21YK folder in your Arduino1.0+ "libraries" folder

  • Open example sketch: "file", "Examples", "DistanceGP2Y0A21YK", "Centimeter" (or "Voltage" or "Raw")

  • Connect the analog sensor to port A0 (and connect Vcc and GND)

  • Compile & upload code

  • Sensor data should be arriving over the serial port




So this lib is pretty usefull. But i wonder, how can I interpret a distance measurement with a displacement at the same time? Like having a xx mm/sec or  inch/min. Just like a feed rate
If you may have a hint, i'm stall on this and have not much idea.

thanks again


Jan 21, 2012, 01:15 pm Last Edit: Jan 21, 2012, 01:17 pm by doggenj Reason: 1
Depending on your program/goals you could:

  • Sample the sensor from time to time and use "millis()" to calculate the time that has passed and then calculate the average displacement

  • Sample the sensor using fixed timing, e.g. 10 samples per second, using a periodic wake-up of the board, and then calculate the average displacement. Periodic wake-up can be achieved using this nice library: https://github.com/rocketscream/Low-Power

I would prefer the second option.


Hi doggenj,

good work,

Have you checked my multimap article - http://arduino.cc/playground/Main/MultiMap - Wrote that because the standard map function could not handle the non-lineair function.
Multimap uses two small lookup tables with interpolation between points.

You could try to make your lib more generic by keep the device specific stuff separate.

2 cents

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


Jan 22, 2012, 08:45 pm Last Edit: Jan 22, 2012, 08:47 pm by doggenj Reason: 1
Is was not aware of your multimap code, but it might be interesting to add your approach as an extra option since it will result in a smaller code size, at the expense of some execution speed.

What you are doing in the multimap function is almost what I did in a spreadsheet to calculate my LUT values.
I measured some values and calculated the intermediate points using two polynomial approximation of the tranfer function. (one for 80 to 20 cm, an other one for 20 to 5 cm).
You can find the (messy) spreadsheet here: (the comments are in Dutch): http://code.google.com/p/gp2y0a21yk-library/source/browse/Calculations/CalculationsDistanceSensorTransferfunctionLUT.xls

How exactly would this make the lib more generic? Since the only hardware specific code is the actual LUT.
I plan to make one generic "DistanceSensor" lib, that supports other distance sensors. (e.g. http://code.google.com/p/srf04-library ('sonar' sensor))

Go Up