# How do you program a sharp gp2d120 sensor?

I'm not finding any information on the net to explain how to program the gp2d120 with an arduino.  I have seen stuff in other codes but don't understand any of it.  So what I'm wondering is how do I actually tell it what to do at a certain distance.  I understand that it use a certain amount of volt for different distance but wasn't sure how you actually put that in a code.   I have everything I need to get started on my robot and hoping I can just figure this sensor out so I can post it up on here.  If someone know a tutorial for me that would be awesome.

## Comment viewing options

My Sharp acts strange... i get the highes reading of 520-560 by around 8 cm (yes i can not handle those imperial units ;-)) in 20 cm i get 300-330 and in 30 cm 195-205.

all the way to the rooms ceiling (180 cm above the table) spit out a reading of 62-67

all readings by closer than 7cm getting smaller values till down to 144 in 0.5 cm distance.

I used this code:

void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(distance);
delay(200);
}

What did i do wrong? My sensor is labeled as Sharp GP2D 12 F 77

That looks like a similar curve.  Remember that I reversed the curve to make it more easily understandable to a poor human.  (me.) Here is what the plot of the original data would look like (in red)

Since other's minds perhaps don't work like mine... Let's see what your data will show.

When you mentioned the readings below 7 cm getting smaller, mine does the same thing below 5 cm, but I did not plot that portion. For mine, if I drop below 5 cm. (which is ~2 inches) the readings got smaller, also. Since that part of the curve is misleading as though the object were suddenly getting further away again, I will position my sensors on the robot about 5 cm back from the edge (or for you, 8 cm), so objects cannot get that close.

Now from 8 cm outward, you have a similar odd curve.

Ok, let me see if I can plot those numbers you gave me...

The curve is similar, but not exactly the same shape... That is easily the difference between the different sensors. ---And actually, my "probable" curve may have too much bend to it and could be closer the straight plotline.

Thanks Dan, I think now it's all logical...and yes, i got 2 Sharps here and got different readings...not much but different...

There is nothing wrong with your sensor. This is the normal behaviour for the analogue Sharp GP2xxx sensors. At the end of this article you can see a diagram 'Output voltage by distance'. You will need to write a function to convert the input voltage to cm.

because of the posts here i was really thinking i get the numbers right out of the sensor :-))) I tested it on "Steven X" and it was a success. Just have to figure out the distance...no need to convert it in cm (save memory and processing power ;-))

With "distance > 500" I am getting a danger distance of around 10cm and this is what i need. Now have to figure out how to detect an edge reliable.

This is for a Sharp GP2Y0A02 but I guess it should work for your sensor with some adaptions

distance = 65*pow(volts, -1.10);

Distance is the distance in centimeters.

I have a Sharp GP2Y0A21YK0F which I am guessing will be the same.  If the readings are not the same, then they will at least be similar.

The .pdf file for the sensor does not give any figures on what reading you will get at a certain distance.  That tells me the sensors may vary from one to the next.  Therefore I made up my own table of measurements for my particular sensor, using a fixed sensor position and a nice, easily readable ruler. The reasons some of these show a range is that I took multiple readings at each distance. Some distances I got the same number and on others I got different values, so show them as a range.

Now you can either use the numbers directly (faster code, since the robot doesn't care if the number relates to anything or not) OR what I did to make it easier for a poor human to visualise, (was noticing the readings never went above 164) I reversed the curve by subtracting the observed number from 165.

That second number is what I plotted as follows:

Hmmm, label cropped off.

First column is INCHES:

Everything closer than 2 inches is ignored, since readout numbers reverse themselves. (A Sharp-brand sensor quirk.) Easy way around it is mount the Sharp sensor so the sensor is 2 inches back from the (front or whichever) edge of the robot so readings can never BE closer than 2 inches (5 cm --ignoring the slight difference that 2 inches is actually 5.08 cm)

Two things:

1. Do you have a 10uf capacitor across the voltage & ground connections of your sensor?
2. You may need to do some smoothing of your data. Basically, you take multiple samples to help filter out spikes.  See:  http://www.arduino.cc/en/Tutorial/Smoothing

So it says place an capacitor between Ground and Power on the Sensor.  Does that mean I that the power from my board to the + side of the capacitor and then to the power on the sensor and take the ground from my board to the - side of my capacitor then to the ground on my sensor.  Or   + from my board to Power on Sensor to + on capacitor then - on capacitor to Ground on sensor to Ground on Board.  I'm just having one of those brain freeze right now for some reason.

Use a ceramic capacitor, they don`t have polarity so you don`t have to worry which pin is which.

On the back of the sensor you should see the solder points for the connector. Find which are + and - and solder the cap on, 1 leg to each pin. Make them short so the capacitor will hide behind the sensor.

The idea is that the capacitor is as close to the sensor as possible. Can`t get much closer than stuck to the back of it :)