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


Sharp Infra Red wide field distance sensors.
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.
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. 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°
Since these devices have more pins then their MAKE command is bigger. For example:
Sharp_GP2Y3A002K0F sensor = MAKE_Sharp_GP2Y3A002K0F(ADC0, B0,B1,B2,B3,B4, B5);
The first parameter specifies the ADC pin used to read the distance for the currently selected LED.
The next five pins are general output pins used to select which LED is active. Note that only one LED is active at a time.
The final parameter is an output pin used to turn the device on/off. Since we continually poll the device then this pin is normally held high the whole time. If you are short on output pins then specify 'null' for this parameter and connect Vin of the device to Vcc so that it is always active.
The sensor should be initialised in appInitHardware using:-
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. Note that distanceRead 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.
Here is an example of how to use it:
    // We have finished reading one LED and it has started reading the next one
    // The value in sensor.pinNo represents the next LED we are now starting to read so the
    // 'just-read' LED is the previous one
    if(sensor.pinNo == 0){
        // We have just completed reading all 5 LEDs
        // The individual distance values are in sensor.led_cm[x] where x is 0 to 4
        // The overall 'minimum' distance is in sensor.distance.cm
    // The sensor is busy reading

Valid XHTML 1.0 Transitional