Pages: [1]   Go Down
Author Topic: wiring up a sharp GP2Y0A02 distance sensor  (Read 126 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Background Information
I'm new to everything arduino and electronics, and c++.     smiley-lol
Having said that, I'm trying to follow the tutorial listed here:  
http://blog.miguelgrinberg.com/post/building-an-arduino-robot-part-ii-programming-the-arduino

I have a sharp distance sensor, model number 2y0a02-f-2z .  I couldn't find the datasheet for this specific model but i got one that looks similar... found here:http://sharp-world.com/products/device/lineup/data/pdf/datasheet/gp2y0a02_e.pdf

The distance sensor in the tutorial has 2 inputs - a trigger and an echo.  Mine only has one wire for the input.  So I've wired everything up exactly the same as the tutorial shows on the bread board diagram, except that instead of having 2 inputs (A0 and A1) I only have A0.   So, if you ignore the "purple" wire that is connected to A1 in the tutorial's wiring diagram for the distance sensor, that's basically what I have.

Problem
I compiled and ran the code but it doesn't return any distances.  I waved my hand infront of it really close but it doesn't detect it.

What I've tried so far
I've tried to confirm that I've wired it up properly.  I double checked that:
  - my "red" wire on the distance sensor, labelled Vcc is lined up with the 5v on the breadboard...
 -  the black to wire labelled GND to the ground on the board.
 - the yello wire labelled Vo is matched up with the wire that goes from the breadboard to A0 on the arduino board.  it matches the "orange" wire in the tutorial's wiring diagram.

In the code, I've tried to pass 0 (zero), or null in place of the ECHO_PIN, but that didn't fx the problem.
NewPing DistanceSensor(TRIGGER_PIN, 0, MAX_DISTANCE);
NewPing DistanceSensor(TRIGGER_PIN, null, MAX_DISTANCE);

Any suggestions would be appreciated.
Thanks.

Update 1
http://www.ebay.ca/itm/380739069651?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649
that's the component i bought...
and the model number is a little different than whats printed on the unit... so i'm going to search for the datasheet using the model number on ebay.  In the mean time, if you have any other suggestions,please let me know. I'm all ears!!
« Last Edit: April 14, 2014, 10:42:47 am by cpama » Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have an infrared distance sensor, so you cannot use a library made for an ultrasonic distance sensor.

You have to wire it differently, pin 1 goes to A0 (or any other analog input), pin 2 to GND and pin 3 to 5V on the Arduino. You can then read the value using analogRead(0).
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok so you mean don't even bother using the library mentioned in the article and just try the function call you've mentioned?  (after wiring change?)
Is the function you mention just a part of the default library that comes with the arduino IDE?
I'm not at my workbench till tonight and so I can't try what you've suggested till then...

But thanks in advance for the tip.  I'll give it a try and post back.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
ok so you mean don't even bother using the library mentioned in the article and just try the function call you've mentioned?  (after wiring change?)

Yes, because that article is about a completely different technology, although the result may be comparable (getting a distance).
It's like reading an article about cars than take canister of gas on your bicycle and wonder why it's not moving itself. In traffic terms it's absolutely logical that not every machine made for moving humans from one place to the other works identical, in electronics it's almost the same, just because a sensor is able to measure distances doesn't mean that it works the same as the other distance sensor mentioned in an article.

Quote
Is the function you mention just a part of the default library that comes with the arduino IDE?

Yes.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the patient help pylon.
So here's the code i have now:

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

void loop()
{
    
    unsigned int cm = analogRead(0);
    Serial.print("Distance: ");
    Serial.print(cm);
    Serial.println("cm");
    delay(1000);
}
as far as wiring is concerned, I *think* I already have it wired the way you describe.   the fact that I'm getting values ... does that indicate i've wired it up correctly?
http://www.sharpsma.com/webfm_send/1487 is the datasheet and on page 2, it dows your the 3 terminals...
Vo (yellow wire) mapped to the input of A0.  And the black wire (GRND?) to ground and red wire (Vcc) mapped to the 5v coming from the arduino.

when i upload the code to the unit, it gives me values, but nothing that makes sense.   I put my hand directly infront of the sensor but  it prints out values like:

Distance: 118cm
Distance: 119cm
Distance: 118cm
Distance: 119cm
Distance: 119cm
Distance: 118cm
Distance: 119cm
Distance: 120cm
Distance: 119cm
Distance: 273cm
Distance: 119cm
Distance: 118cm
Distance: 118cm
« Last Edit: April 16, 2014, 09:01:29 am by cpama » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So upon closer examination, here are the patterns I see.
when i just leave the sensor on its back... in other words, the light emiter and detector are facing the ceiling, this is the type of data it returns

Distance: 105cm
Distance: 105cm
Distance: 105cm
Distance: 206cm
Distance: 105cm
Distance: 105cm
Distance: 106cm
Distance: 106cm
Distance: 105cm
Distance: 206cm
Distance: 105cm
Distance: 105cm
Distance: 105cm

I don't know why it spikes up to 206...
but here's the other thing i've noticed.  when i do put something directly infront of it, the value jumps higher.

Distance: 106cm
Distance: 105cm
Distance: 105cm
Distance: 105cm
Distance: 105cm
Distance: 105cm
Distance: 105cm
Distance: 105cm
Distance: 106cm
Distance: 206cm
Distance: 104cm
Distance: 105cm
Distance: 106cm
Distance: 105cm
Distance: 106cm
Distance: 105cm
Distance: 106cm
Distance: 114cm
Distance: 204cm
Distance: 105cm
Distance: 378cm       had my hand infront of it here
Distance: 343cm       and here
Distance: 357cm       and here
Distance: 467cm     etc
Distance: 373cm     etc
Distance: 355cm     etc
Distance: 351cm
Distance: 105cm
Distance: 106cm

I'm currently rereading the sensor's manual to make sure i understand what the values returned represent.  I'm sure it's working but i'm just not understanding how to interpret the return values.  Here's something in the manual that i don't understand that might be the issue.
The manual says "This device outputs the voltage corresponding to the detection distance".   So it sounds like I need to convert this value somehow from a voltage number to distance ??
any insights on how to do this would be appreciated.  I'm so green to everything electronics.

thanks
« Last Edit: April 16, 2014, 10:18:57 am by cpama » Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

According to the datasheet anything closer than 20cm is not detected correctly but recognized identically as a far away target.

The read value is not in cm it's just a voltage level. The highest value is at about 510 when the obstacle has a distance of about 20cm. At 50cm it's around 350 and from about 130cm the value stays more or less at approximately 100. (See Fig.2 on page 5 of the datasheet). That curve is not linear, so it's not an easy calculation to get a distance in cm out of that sensor and it probably has to be calibrated first. Do you need absolute values from the sensor? Isn't it enough to know when it approaches an obstacle?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You mention calibrating... how would i do that?
with nothing in front of it, it's returning values of 170-189
then when i put a book infront of it at 20cm... it spikes gradually to 667

I guess I could just baseline what it's doing right now and then write code to do something when it gets too close.  but i'd like to
a) learn how to make it return values that its supposed to - like the values you've listed.  
b) learn how convert from voltage to distance.

I guess calibrating will solve point a.  
as far as point b is concerned, perhaps the arduino analog input is also converting the numbers it receives some how?
i've measured the voltage on the black and red wires going into the breadboard from the sensor and i get beween 0.48 and 0.49 (when i have it facing up - nothing infront of it.  Ceiling is is approx 7 ft above)
I'm trying to compare this number with the chart on page 3 under the "Electro-Optical Characteristics section".
How am i supposed to understand the line that has the parameter "Output Voltage"?  does it mean that if I measure 0.4 volts on the wires, then value i get should be divided by 150?
It'd be nice to get the algorithm correct for learning purposes and for being able to control my robot with accuracy...

« Last Edit: April 16, 2014, 12:06:36 pm by cpama » Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You mention calibrating... how would i do that?

That's easy: put a big obstacle in front of the sensor, read the value it puts out and measure the distance manually. Do this for several distances and use that as the reference table.

Quote
as far as point b is concerned, perhaps the arduino analog input is also converting the numbers it receives some how?

The Arduino is not receiving the numbers, it's measuring them. The value you get is telling you that that many times a 1023th of the reference voltage (if you've not chosen something different explicitly, that's the voltage you're powering the Arduino with) is currently present on the analog input pin. The problem here is that if you're powering the Arduino by USB this voltage may vary over time so you don't get really usable values (although you might get a trend in any case).

Quote
I'm trying to compare this number with the chart on page 3 under the "Electro-Optical Characteristics section".

I don't have a chart there, just a table.

Quote
How am i supposed to understand the line that has the parameter "Output Voltage"?  does it mean that if I measure 0.4 volts on the wires, then value i get should be divided by 150?

No, it means, if you get a value of 82 (which equals 0.4V if you power with a stable 5V) the obstacle is about 150cm away. But as you also can see there, that value can be in the range between 51 (=0.25V) and 112 (=0.55V). That's why I told you that you might have to calibrate by looking what your sensor is giving you at that distance. If you look at page 4 you can see the curve that describes the relationship between the measured voltage and the distance the obstacle has from the sensor. The calculation is not done by a simple division. If you want a realistic value you have to do at least a square approximation.

Quote
It'd be nice to get the algorithm correct for learning purposes and for being able to control my robot with accuracy...

I don't know if that's the right sensor if you want accuracy for your robot.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hmm... you make an interesting point about the USB that I didn't think of before.
I will try to create a proper power supply for it and retest.
thanks.
Logged

Pages: [1]   Go Up
Jump to: