Sharp Infra Red 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.
The range of devices we support are as follows:-
GP2D12 This measures between 10cm and 80cm
GP2D120 This measures between 4cm and 30cm
GP2D15 This measures between 10cm and 80cm
GP2Y0A02YK This measures between 20cm and 150cm
GPY0A21YK This measures between 4cm and 30cm
GP2Y0A02YK0F This measures 20cm to 150cm
GP2Y0A710K0F This measures 100cm to 550cm
GP2Y0A700K0F This measures 100cm to 550cm
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 !
Regardless of which of these sensors you are using then they can be used in the same way. As an example the GP2D12 can be defined by:-
Sharp_GP2D12 sensor = MAKE_Sharp_GP2D12(ADC0);
where ADC0 is the ADC pin you are using.
The sensor should be initialised in appInitHardware using:-
The sensor can then be read using:-
and then the distance in cm will be in sensor.distance.cm