WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation

Sharp GP2 5 LED

Sharp Infra Red 'wide field' distance sensors.
These are single boxed units but contain five LEDs pointing at various angles (often 25%). Each LED can be selected individually and a single ADC pin is used to measure the result for that LED.
There are a number of different sensors but they are all used in the same way. The difference between them is the usable range over which they can detect objects.
The devices work by shining a beam of light which is reflected back from an object. The further away the object is then the more it displaces the light to the sides. Therefore: you will notice that the devices which can detect further distances are physically larger because the 'eye' which sees the reflection needs to be further away from the transmitter. Equally the transmitter needs to be more powerful and is therefore larger.
Because these devices uses a beam of light then they are not very good at detecting thin objects - like chair legs. This happens because the beam of light may move from one side of the leg to the other and so therefore it misses the fact that the leg is there. For a less precise detector you should consider using a sonar.
The devices output a voltage and so should be connected be to an ADC pin and use the +5V regulated supply to power them. A word of caution: these devices are inherently noisy and require spikes of current. The noise can be reduced by connecting the black casing to ground (yes - it looks like plastic but it is actually conductive). If your robot has a metal shell then bolt the sensor to the shell and connect the shell to ground. The current spikes can be minimised by fitting a capacitor of around 10uF close to the device - preferably by soldering an SMD capacitor into the device itself.
You will still find some spikes in the readings and so, if these spikes confuse your robot, then you should consider writing a low pass filter in software to filter out these spikes. But a word of caution: that spike may actually be correct ie something has suddenly come into view.
Here are some examples:-
GP2Y3A002K0F This measures between 20cm and 150cm and has a total arc of 25°
GP2Y3A003K0F This measures between 40cm and 300cm and has a total arc of 25°
Note that the larger the maximum distance then the more peak current these devices will require. It is common for them to require a peak current of 0.3 amps !
Since these devices take around 25ms to obtain a single LED reading (ie 125ms for a full scan of all LEDs) then they are quite slow and so this library reads one LED at a time. Note that read() can return a TRUE or FALSE. When reading these devices it will only return TRUE once a valid reading has been taken for the current LED. If a FALSE is returned then no sensor data has changed.
Assuming you have called your sensor 'myDistance' in Project Designer then it can be accessed as follows:-
if(myDistance.read() == TRUE){
    // We have finished reading one LED and it has started reading the next one
    // The value in getBeamNumber() represents the next LED     
    // we are now starting to read so the
    // 'just-read' LED is the previous one
    if(myDistance.getBeamNumber() == 0){
        // We have just completed reading all beams
        for(uint8_t beam = 0; beam < 5; beam++){
             cout << "Beam #" << beam << "=" << myDistance.getBeam(beam);
        // The overall 'minimum' distance
        cout << "Minimum:" << myDistance.getDistance();
    // The sensor is busy reading
Or dumped to the standard out destination using:-
cout << myDistance;
Function Summary

Valid XHTML 1.0 Transitional