Sharp gp2d120 tutorial anywhere???

I'm not finding any tutorial for a sharp sensor. All I really want to do right now is use the sensor to detect an object 10cm away and turn on an LED. I just not sure how I measure distance. If someone could point me in the right direction that would be great. Thanks a bunch.

int LED =13;

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

void loop()

{
   int distance = analogRead(0);
   
   if ( distance > 2 )
   {
      digitalWrite(LED,LOW); 
   } 
   else
   {
     digitalWrite(LED,HIGH); 
   } 
}

When I run this code the Led is on all the time but when I get in the 17inch and under it goes off. So how do I get it to read cm.

I figure it out.

Hi, a sharp gp2d120 has just arrived.
can you post your code that works? it would be appreciated :slight_smile:

thank you :slight_smile:

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println(analogRead(0));
  delay(1000);
}

This right here you can measure the distance and write down what the number is for each distance.

int LED =13;

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

void loop()

{
   int distance = analogRead(0);
   
   if ( distance == 321 )
   {
      digitalWrite(LED,HIGH); 
   } 
   else
   {
     digitalWrite(LED,LOW); 
   } 
}

Here is just something to turn and Led on when it equal to the distance I got from the serial print which is 3 inch, yours might be a tad bit different number. Sorry took so long to get back. Let me know if you have any othe question and I will try to help you out.

Hi - someone asked me about this the other day so here is the info I gave him:

--Roy

This is the sensor I used:
http://www.acroname.com/robotics/parts/R48-IR12.html

comparison of the Sharp sensors:
http://www.acroname.com/robotics/info/articles/sharp/sharp.html#e8

I got a better price at http://www.pololu.com/ as I recall - but wherever you buy it *Be Sure to get the connector with it - it's a non-standard connector that is hard to find.

Here's some code for reading the sensor:
http://www.tigoe.net/pcomp/code/category/arduinowiring/44

Using a capacitor with the Sharp sensor (this was important):

http://www.gorobotics.net/articles/sensors/effectively-using-the-sharp-gp2d12-rangefinder
http://www.oopic.com/oirrange.htm

thank you so much! i'm starting now to experiment! :slight_smile:

Unfortunely I've made a mistake on ordering sensor :frowning:

I have bought this one GP2D15
http://www.acroname.com/robotics/parts/R49-IR15.html

that is digital one type.
no pain :wink: but I tried to make it working but i have a problem, as when i test the digital response from the sensor, the reply is not accurate.

this is the code:

/*
digital sensor reading SHARP GP2D15
code by A.Richetta
V. 0.1 26 APRIL 2009
version: NOT WORKING
*/

int POS = 3; // initializing sensor
int LED_ROSSO = 6; // initializing RED LED
int LED_VERDE = 4; // initializing GREEN LED

void setup()
{
Serial.begin(9600); //SPEAKING WITH SERIAL
pinMode(POS, INPUT); // SENSOR IS AN INPUT TYPE
pinMode(LED_ROSSO, OUTPUT); // LED IS OUT
pinMode(LED_VERDE, OUTPUT); //LED IS OUT

}

void loop()
{
if (digitalRead(POS) == 1) // READING SENSOR, IF 1 IS BACK THERE IS AN OBJECT
{
digitalWrite(LED_ROSSO,HIGH); // TURN RED LED ON
digitalWrite(LED_VERDE,LOW); // TUNR GREEN OFF
Serial.println("ostacolo"); // PRINT ON SERIAL OBJECT!!
delay(1000);
}
else
{
digitalWrite(LED_VERDE,HIGH); // TURN GREEN LED ON
digitalWrite(LED_ROSSO,LOW); //TURN RED LED OFF
Serial.println("nessun ostacolo"); // SERIAL PRINT NO OBJECT IN FRONT OF ME
delay(1000); // WAIT SOME TIME
}
}

Problem is, when no obstacle is in front of the sensor it's ok, green led is correctly on.
when i put my hand in front of the sensor, it starts to turn on red and off, turn green onn and off and so on.

i made mistakes in code?

thank you for any reply

thank you all for kind reply! :wink:
i've figured out

I am currently working on my senior capstone project for Engineering Development and Design, and it sounds like it is somewhat similar to what you are attempting to do. I have a gp2d120 but have no idea how to wire it for it to turn on an led, and have no idea how to write a code. I'm wondering if anyone here can give me a crash course on this business?