Sharp GP2Y0A21YK Infrared Distance Sensor

This is where you talk about the NXJ hardware related topics such as the brick, sensors, LEGO pieces, etc

Moderators: roger, 99jonathan, imaqine

Sharp GP2Y0A21YK Infrared Distance Sensor

Postby clonejo » Sat Feb 11, 2012 4:38 pm

Hi!

I'm currently trying to hook up a Sharp GP2Y0A21YK sensor (Datasheet) directly to the NXT. I took the configuration of the different wires in a NXT sensor/motor cable from here: http://www.roberta-home.de/sites/defaul ... v1-1_0.pdf (German), page 17 (or page 9 as printed on the page). The document says the first wire (white) of the cable is used as analog input. I supposed to be able to measure the voltage this way. But using the following code, I just get values of -1:

Code: Select all
import lejos.nxt.ADSensorPort;

public class SharpIRSensor {
   
   private ADSensorPort port;
   
   public SharpIRSensor(ADSensorPort port) {
      this.port = port;
   }
   
   public int getRaw() {
      return port.readRawValue();
   }

}


Is my approach wrong in general or am I just using the API wrong?
clonejo
New User
 
Posts: 10
Joined: Thu Dec 16, 2010 2:38 pm

Re: Sharp GP2Y0A21YK Infrared Distance Sensor

Postby gloomyandy » Sat Feb 11, 2012 5:24 pm

You need to initialize the port to define how the various sensor lines are used and what voltage is supplied to the device. Something like
port.setTypeandMode(SensorPort.TYPE_CUSTOM, SensorPort.MODE_RAW);
Should do the job...

Note that there is a 10K pullup resistor on the analogue input line connected to 5V. Note also that this pin can also be connected to a 9V current source depending upon the sensor port mode settings. So if your sensor is not able to tolerate this sort of voltage you must take care to ensure that it is never connected to a port that has been configured for say an Ultrasonic sensor...

Good luck...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3012
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Sharp GP2Y0A21YK Infrared Distance Sensor

Postby clonejo » Sun Feb 12, 2012 1:07 pm

This way I get values around 900 with very small variation. The variation gets larger when I move the sensor around, but not significantly (+/- 5). When I remove the cable from the NXT the value switches to 1023.

Update: I've tested the sensor using a multimeter, it still works fine. I suppose, I'm using the wrong parameters
clonejo
New User
 
Posts: 10
Joined: Thu Dec 16, 2010 2:38 pm

Re: Sharp GP2Y0A21YK Infrared Distance Sensor

Postby gloomyandy » Sun Feb 12, 2012 1:28 pm

How do you have the device connected to the other pins in the sensor port?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3012
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Sharp GP2Y0A21YK Infrared Distance Sensor

Postby clonejo » Sun Feb 12, 2012 3:16 pm

Code: Select all
IR sensor - color  -- color - NXT
v0        - yellow -- white - Pin 1: analog input
GND       - black  -- black - Pin 2: GND
vcc       - red    -- green - Pin 4: power supply
clonejo
New User
 
Posts: 10
Joined: Thu Dec 16, 2010 2:38 pm

Re: Sharp GP2Y0A21YK Infrared Distance Sensor

Postby gloomyandy » Sun Feb 12, 2012 4:30 pm

If you attach it to the nxt and run our program what voltages do you see on the analogue output?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3012
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Sharp GP2Y0A21YK Infrared Distance Sensor

Postby clonejo » Sun Feb 12, 2012 5:30 pm

If the NXT's analog input is connected with the sensor's output, the voltage is fixed at ~4,5V. If I disconnect that wire and just measure the output using the multimeter I get voltages in the range from 0 to 5V.
clonejo
New User
 
Posts: 10
Joined: Thu Dec 16, 2010 2:38 pm

Re: Sharp GP2Y0A21YK Infrared Distance Sensor

Postby gloomyandy » Sun Feb 12, 2012 6:36 pm

Hmm looks like your sensor is not able to drive the standard analogue input line probably due to the 10K pullup resistor. Ok you could try the following...
1. Instead of using the standard analogue input on pin 1 connect it to the DIGI1 line (pin 6).
2. Modify the port initialization code to add the line:
port.setSensorPinMode(SensorPort.SP_DIGI1, SensorPort.SP_MODE_ADC);
after you have set the type and mode.
3. Modify your read routine to be:
return port.readSensorPin(SensorPort.SP_DIGI1);

What the above does it to make use of the ADC built into the AT91 chip (which is connected to pin DIGI1), instead of the ADC in the ATMega (connected to pin 1). This line does not have the 10K pullup to 5V. It does have a more limited input range 0-3.3v but according to the spec you linked to this should be ok.

NOTE: I have not tried the above, so make your own judgement if you want to try it and don't blame me if anything goes wrong...

Good luck

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3012
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Sharp GP2Y0A21YK Infrared Distance Sensor

Postby clonejo » Sun Feb 12, 2012 8:20 pm

While I haven't tested your last idea, I think if you actually wanted to use this sensor with an NXT you will need a voltage sensor. Or just use the IR sensors build for the NXT.

I would like to thank you for your great help, Andy. I deeply respect all the work (and time!) you put in this project, especially the support.

Best regards,
clonejo
clonejo
New User
 
Posts: 10
Joined: Thu Dec 16, 2010 2:38 pm

Re: Sharp GP2Y0A21YK Infrared Distance Sensor

Postby gloomyandy » Sun Feb 12, 2012 9:34 pm

Hi,
just to be clear I have no reason to believe this will do any damage (it basically uses a mode used to support the Lego color sensor). But obviously thre is always some risk when connecting none Lego hardware to your NXT. If you do decide to try it let me know how you get on...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3012
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK


Return to NXJ Hardware

Who is online

Users browsing this forum: No registered users and 1 guest

cron
more stuff