The Sharp GP2D12 is an infrared ranging sensor that uses triangulation to determine distance with a range of 10 to 80cm. The sensor comes with a JST 3 pin connector. I didn't have any appropriate connectors laying around, so I just removed the connector and soldered wires to the board instead. I also mounted the smoothing cap on the sensor. The data sheet recommends a 4.7uF capacitor, but I found too much jitter in the readings. I fooled around with different cap sizes, and settled on 33uF, a lot more than called for. However, much less does increase the jitter, and more doesn't help.

Sharp GP2D12

The output of the GP2D12 is not linear. Therefore, a little math is required to determine the distance. I was floundering around without much success, so I asked Tracy Allen of Electronically Monitored Ecosystems, Basic Stamp guru and an integer math genius. He came up with a formula based on the data sheet, and I set up a test to try it out.

Unfortunately, it wasn't as accurate as one would hope. To solve this problem, Tracy asked me to take a series of careful reading at 10cm increments. It became obvious that there is a lot of individual variation between sensors, which further complicates the problem of resolving distance accurately. To deal with these issues, Tracy came up with a calibration routine and a formula to reduce the readings to cm. His routine calls for the user to set the sensor and target carefully at 80cm and then 20cm. The program takes the results and uses Tracy's magic formula to determine the calibration factors.

The code (which you can download HERE) assumes that you are using a TLC2543 analog to digital converter, set to read in milli-volts For a complete explanation about using the TLC2543, check out Tracy's site HERE. It further assumes that you have a normally open momentary switch connected to pin 0. The schematic can be seen below. Note that the schematic is NOT chip-centric.

Note about the Vref: I was lazy and tied VDD to the Vref pin. This is easy but reduces the accuracy a little. The most accurate solution is to use a 5.012 precision voltage reference. However, I am using the sensors on the HydroBot, and adding another Vref to the existing board would be a pain. Therefore, I decided to accept the slight hit to accuracy.

In order to use the calibration program, you need to have the circuit above and set up an easy to use target and measurement system. I have some rolls of paper tape that have a grid in cm's printed on them. I took 100cm of this and taped it to the floor. I used a piece of wood for the target and made a holder for the sensor by screwing the sensor to another block of wood. This made it easy to take repeated measurements quickly with decent accuracy.

When first starting the program, the output will be incorrect, because the program loads whatever is in the first four bytes of the Stamp's EEPROM for the calibration factors. Once you calibrate the sensor though, the correct calibration factors will be stored for future use. Take care to set the distances as carefully as you can. The first measurement is taken at 80cm, and the second at 20cm. The program takes 10 readings and averages them to smooth out the results. Each reading is displayed so the user can make sure there are no erroneous readings that will skew the average. If you are getting oddball readings, look through your hardware and try different cap values. I was driven nuts by one sensor until I figured out that it didn't like the connection to the breadboard. I moved it over one slot and bingo, no more wacky reads.

Overall, I really like the sensor. It seems to be fairly insensitive to ambient light and not picky about the color of the surface it is reflecting from. Like any sensor, it has conditions and angles it does not like. For example, mirrored or shiny surfaces are a problem as is direct sunlight. One nice aspect to it is that it seems to be a good complement to ultrasonic sensors because they have different "blind" angles.

On the downside, the sensors do require individual calibration, and can return a valid measurement when there is nothing in range. The precision leaves a little to be desired, I find that in the first half of the sensors range it is generally accurate within a cm or so. However, in the latter half of the range it can be as much as 4cm off. For most general robotics applications this is acceptable, and Tracy says that the accuracy could be improved with a three point calibration routine. When you take into account the price, ease of use and relative indifference to surfaces and lighting conditions, it is a pretty good sensor.