How do you program a sharp gp2d120 sensor?

Oh I got it now!

The real problem here is geography! I just did a quick member check… OF COURSE you are having these problems…

Sedgwick County?!?!?!? –Well, there’s your problem…

Move up to Manhattan where it’s normal! --But then again, you do have The Yard there… I guess that’s a good thing!

lol, yep you better believe
lol, yep you better believe it.

Try this

If you hook your sensor up to the arduino then change the pin in this arduino code to the right pin, upload to your board then click the serial monitor button in your arduino ide then you will be able to see what is being given back from the sensor.

 

http://arduino.cc/en/Tutorial/Graph

 

Once you know all of that is working and you want to see it as a graph then download processing from:

http://www.processing.org/

 

here and run it with the code supplied in that example.

Uploaded code for my robot
I’ve uploaded the code for my robot https://www.robotshop.com/letsmakerobots/node/2372 which shows how I handled it.

**int LED =13;void setup(){ **

int LED =13;

void setup()
{
pinMode(LED, OUTPUT);
}

void loop()

{
int distance = analogRead(0);

if ( distance > 2 )
{
digitalWrite(LED,LOW);
}
else
{
digitalWrite(LED,HIGH);
}
}

Am I’m doing something wrong cause it isn’t reading right. How do I get it to read 2 inches. It seem to always be on and then when I get in the 17 inch and under it goes off. Sorry I’m really new and the arduino board aren’t helping at all. I get much quicker reply here then I have ever anywhere else. Thank guys

The return from analogRead isn’t in inches

The value will be some number that you have to apply some math to in order to get the inches.

The following links will help:

http://www.arduino.cc/playground/Main/ReadGp2d12Range

http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html

 

Measure approx 2 inches away
Measure approx 2 inches away from the bot and see what the distance reading is. Make that number = to the danger distance.

thanks alot, not actually
thanks alot, not actually what I was looking for but it still work. Luckly I try messing around with the serial port to see what it does. Now my question is, how do you get an exact reading though. Like is there an easier way of doing this or is this the best way to do it. I figure I could some how convert some numbers down to get it to read inches. Still I really have to thank you a bunch for this. I was wondering if I could put those numbers in but never even tried.

thanks, the arduino site
thanks, the arduino site didn’t make any sense to me since I haven’t done anything with bytes and the other site I’m going to have to look over more, but think I might be able to figure it out. I can’t believe I didn’t see that cause thats where I bought my sensor at.

**If you cant find a formula**<br><p>If you cant find a formula that someone has already made it might be easier to just measure some arbitrary distances and make them constants in your code. Try the code below. See what numbers it spits out of the serial port. If you put something 2 inches in front and the value is say 500 then you can #define 2INCH 500 in your code and use/adjust it easily.

int LED =13;

void setup()
{
pinMode(LED, OUTPUT);

Serial.begin(9600);
}

void loop()

{
int distance = analogRead(0);
Serial.println(distance);

delay(100);
}

Yea thanks, that what I been

Yea thanks, that what I been doing for the past like 10 minutes or more lol. It spike alot so I just watch for the most common number. Also I went back and measure to just see if I had it right and it was off a little. I think I need something better to detect off of or something lol. So you think this would be the best way to do it though to shorten my code down alot. To me it seem like it, but just want to make sure.

Edit: Didn’t really understand this part #define 2INCH 500. I just put int distance == 500 and it mean turn it on at 2inch. Is the define part better or different, I don’t know much about #define.

#define

#define 2INCH 500 will allow you to do something like:

if ( analogRead(2) < 2INCH)

{

// your code here

}

 

that way when you come back a couple of months later, you can look at your code and remember that you wanted to do something at 2 inches, and not what the magic number 500 mean. This is especially helpful if that magic number is used in more than one place. Otherwise, at least put a comment in your code to help you remember later on what 500 meant.

Sensor Spikes

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

 

I might be dumb
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,

Use a ceramic capacitor, they dont have polarity so you dont 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 :slight_smile:

thanks

so just solder it to the power and ground wire on the back. Thanks, I really hate to solder anything to it, but I might just do it that way if its going to say me a lot of trouble. Now I would need a 10uf ceramic capacitor right, also do they have a volt on them. I bought some from a local electronic place and they have my cermaic disc mark as .47uf and 50V. How much is this really going to help with my spike though.

Equation

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

volts = analogRead(IRpin)0.0048828125;

distance = 65pow(volts, -1.10);

Distance is the distance in centimeters.

My Sharp acts strange… i

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()
{
   int distance = analogRead(0);
   Serial.println(distance);
   delay(200);
}

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

There is nothing wrong with

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.

Confused

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.