# How can you measure distance with light?

(How to make your own distance measurements similar to Sharps.)

Note: Apologies to OddBot. It seems I posted something he has already explored. My mistake for not reading his page(s) more carefully. I thought they were just proximity detectors, but he is actually using analog reading.  Find his page here: http://letsmakerobots.com/node/2907

which was the predecessor of his compound eye: http://letsmakerobots.com/node/10822

Added note:  Let me clarify something. Because of some of the responses I've gotten, I wish to mention that this method will NOT work perfectly in all conditions. It will work in places where you have obstacles that are smooth and will reflect back light. Just as a line-follower trying to find its way on a shag carpet without lines, it will not work so well against uneven surfaces that do not reflect light back evenly.

However, it WILL work in many common situations, and may save you money where you are able to use it, [especially if you turn up the brightness of the infrared LED either by additional current (lower series resistance) or by using more than one LED as the light source and adjusting your reading accordingly.

________________________________________________

How does an IR distance detector (such as Sharp) work?  Or more specifically, is it possible make a cheap sensor that uses a simple method to measure distance so we do not need to buy the more expensive Sharp sensors?

Some people think Sharp sensors work like radar or lidar, measuring the time that it takes for a light beam to hit a target and bounce back.

However, to find distance by measuring the time it takes for a pulse of light to go out to an object only a few centimetres away, and reflect back to a detector requires a device capable of timing in fractions of a nanosecond (less than one thousand millionth of a second). This method will not work with a microcontroller.  Normal microcontrollers cannot measure time less than a microsecond, which is a thousand times greater than what you need for such measurements.  Light travels roughly 1 foot (or 30 cm) in a nanosecond or 300 meters per microsecond.  If you were measuring distance in kilometres, you could do it with a microcontroller, but anything less, —no.

Sharp sensors measure distance by triangulation, using a PSD (position sensitive device) to figure out the angles that light travels to its target and back and contain a precision lens that bends the light further to get a good measurement.

.

This is one type of commercially available PSD.

A PSD is a special detector somewhat similar to a PIR, but the output will vary according to where the light hits the sensor.  Thus it is immune to brightness/darkness effects. It will work no matter how much light is reflected back to the sensor. The surface of the sensor can be thought of as having a series of light sensitive bands or islands. If the light hits in the center of the sensor you will get a balanced reading at the outputs. As the light strikes further from the center the output will change indicating the angle at which the light came in.

Thanks to Merser's investigation, it appears that Sharp sensors are indeed PSDs.  A PSD is essentially a CCD array, such as found in a camera to take pictures. In the case of a PSD, the various pixels in each row are used (with a light-bending focusing lens) to determine a distance mathematically according to which pixels are lit up by light bouncing back to the sensor array.

>>Regardless of the actual method used by Sharp, the method I give here can be used to some success employing a method which is much cheaper and that you can make at home in your own robot workshop. You will use a sensor arrangement like what you use for proximity detectors.

.

**********

This new detector will contain a simple light-detecting diode, (which has no way of telling angles). When building this new sensor, you will mount an infrared LED and an infrared sensitive detector solidly in place on a backing such as a piece of circuit board, but with a slight inward angle dependant on the range the sensor is meant for. (A new job for our robot-tested standby, hot-glue?)

So what is this simpler method of measuring distance using the light?

Since the PSD is basically a camera pickup, all the pixels are the same, so you can ignore the brightness of the light hitting the sensor and just measure the angle by reading out where in the matrix the light is hitting. This can be quite accurate, but is hard to duplicate in a home robot-shop environment unless you actually use a small camera device and video processing to get your answers.

We are looking instead at something much simpler that you can do at home, will be cheap and is sufficiently accurate for "most" robot projects.  We are all familiar with the "funny response curve" we get using those "Sharp-type" devices.

Here is what this curve tells me.

First of all the useful distance for the sensor in this diagram is given as 10 cm to 80 cm. At less than 5 or 6 cm the input of light is being blocked by the plastic ridge which is intended to prevent light from the LED from directly hitting the detector. When the sensor tries to respond to an object closer than that distance, the light is blocked more and more, the closer the object is.

Since it appears Sharp is actually using a PSD device, the amount of light hitting a pixel should not matter. The PSD should only answer the question, "Is light hitting that pixel or not?"  If it is, then the light is coming in at a certain angle in order to hit that pixel and therefore the distance is "XX". Because of that I do not see why the curve should look the way it does.  Compare the response curve drawing to an illustration of the inverse square law. If we ignore the part of the curve where the light is being partially blocked by the plastic case, we see that the curves are "essentially" identical.

[They may not be absolutely identical because of the actual response of a photodiode when light of various levels hits it, or the response of other components in the circuit.] Why are these curves even close?  With a PSD pickup, there is no reason for a curve at all. The PSD in a Sharp appears to be one with enough external leads that it can tell where in the array the light is hitting and as I mentioned, it is either hitting certain pixels or it is not. The response should be a straight line. (I can only presume that this curve is introduced due to some design oddity. (Perhaps in the way they average the distance readings when light is hitting more than one pixel.)

- - - - - - - - - - - - -Plot of the Inverse Square Law - - - - - - - - - - - - - - -

So why the similarity?   What is this inverse square law thing, anyway?

- - - - - - - - - - This example shows why the amount of light relates to the distance. - - - - - - - - - -

When a bright light (in this case infrared light) hits a regular photo-detector, the resistance of the detector decreases to a very small value. On the other hand, if the detector is completely dark, then it's internal resistance is quite high. By varying the amount of light hitting the detector, you will vary the internal resistance at the same time.   As shown in the following illustration, when the distance light travels is doubled the area covered by that same light is quadrupled (multiplied by four), so the light is only one–fourth as bright at a certain spot. If the first distance (shown by the letter 'r') were 10 cm, then at 20 cm (2r) the brightness would be only one quarter what it was at 10 cm. At 40 cm (4r) the brightness would be only one sixteenth what it was at 10 cm. By the time you get out to 80 cm (or 8r) since 8x8=64, the light will be only one sixty-fourth as bright. As you can see this will give a wide range of readings for a small difference in distance.  However, you will also notice that since the amount of light is changing by the square of the distance, when you plot it out, the graph will be a curve rather than a straight line. In fact the curve will look like those above.

The working part of our simple sensor contains a light emitter in the form of an infrared LED and a light detector in the form of an infrared-sensitive photodiode (or phototransistor, etc.).  Infrared is used because it tends to reflect back fairly evenly from surfaces of various visible light colours.

We have used an LED and photo detector pair before in making a proximity detector, so how do we use the same components and suddenly make it detect distances? That is simple. We hook the output of the detector to an analog input on our micro-controller, instead of a digital input. Hooked to a digital input we get only a yes/no or high/low response, but if we tie it to an analog input, we find a broad range of numbers. All that is needed then it to convert these output values into distances. I seem to recall that we have to do that with the Sharp sensors, don't we?

Sosee, it really is simple after all.  There is nothing new to learn...  Just hook your detector output to an analog input rather than a digital one.  Now why didn't we think of that before?

.

In the drawing above, I presented a sampling of ways a light sensor may be connected to the input of a microcontroller.  One thing I should point out here is that you will likely need to alter the 10 K resistors in the drawing above to different value. You may want to experiment by putting a 50KΩ to 100KΩ potentiometer in its place until your distances are falling in an acceptable range.  Here is what I mean. The amount of light coming from the LED will result in the light hitting the photo detector falling different places in the voltage range.  It may take you several tries to get the values correct, so the output from the sensor falls in a spot where the curve falls within the right range to give useable readings, —ones that may be easily translated into proper distances.

If you do not have the exact current through the LED correct, you can fix it in software, by adjusting the readings.

That is really all we have to do.

Another method I may give more time to later involves flashing the LED on and off at a set rate (frequency) and then checking the incoming signal at the detector for light at that same frequency.

________________________________________________

## Comment viewing options

Indeed it does look that way.

.

Yes, I tried to look into a sharp through the lens to see if I could see a matrix layout, but it was too dark to let me do that. All I can go by is that there are more than 2 contacts on the back.  (A diode would only have two, whereas a PSD would have a minimum of three.)

While the circuit you have described would work fairly well for detecting the distance to a surface of uniform reflexivity it wouldn't work as well as the sharp sensor in an environment with objects of different colors and at different angles.

I think the sharp sensor actually works very similarly to the WiiDar

Yes, non-uniform reflexivity can be a problem. I am not saying it is a perfect replacement for a true PSD sensor. I am saying that is should work reasonably well for most applications. If it turns out you have too many rough or non-reflective surface where your robot is operating then it will not be a good choice. Places where you have smooth walls, boxes, or other smooth, hard objects, then it should work for you.

As to different colours, the homemade sensor should work exactly the same, since it uses the same light source, an infrared light-emitting diode.  I am not sure if different angles might work differently, but I do know that I mounted physical bumpers on my last robot, Edwin, because his predecessor relied only on a Sharp sensor and two ultrasonic ones, and did not always see the walls until the last instant, when he came in at an odd angle. This is true of most sensors that rely on reflection of a signal (light, ultrasonic, etc.) If the surface it is trying to locate is at the wrong angle, the signal reflects away, rather than back towards the detector. This is the principle behind aircraft passive stealth technology. The surfaces of the plane are angled so they reflect radar signals away rather than letting them reflect back.

However, there is a problem which the homemade sensors will have that a true PSD sensor would not. As you point out, the method I described will not work as well on a textured surface, since a textured surface will not reflect back as much light as will a smooth surface, and that will make a robot with this type sensor think the surface is further away, whereas a position sensitive device does not care how much signal reflects as long as there is some.

And for those of you who would not like to use analog compare. like if you are making a rotary encoder or a proximity detector, so your robot wont crash into things.

you might have to change the resistors but it should work with most generic IR-led's and photodiodes. and if you want me to remove this from your post Dan let me know. I just wanted to share it.

Yes, that will work, but as you say the resistor values may need experimentation.

This is one variation of the standard method we have all used before (tying a photodiode to a digital input). You do not really need the comparator since you are using a micro controller that is capable of differentiating along with varying the resistor supplying current to the photodetector. You could also use a transistor amplifier instead if you need it.

well, the idea of using the LM339 is because this way i get a TTL 0 or 1 depending on 1. the level of received light. 2. the threshold value set by the potentiometer. 3 it also depends on which input i have on the - input to the 339 since that one gets inverted. and the output will be readable without using the ADC on the microchip. thats the reason why i put a 339 there. And those resistor value works perfect for me but they might have to change to fit your specs on the ir-led and photodiode. Oh and Dan, Nice write up. :)

Thank you.

You certainly can use a comparator, but I'm saying you do not really have to.  You can use a straight digital input without any wave shaping, by simply adjusting the level by changing the resistor value in series with the infrared detector, so the voltage at the input swings either side of center. (Use a potentiometer to find the right value, and then once you have one that works well, you can put a fixed resistor of that value in its place.)

LM339s are relatively cheap, though, so you are not out much by using one to get a more positive switching action.

Ok, I think the wording is better. (Sorry, I left it incomplete before.)

"uses an array of photodiodes ( a Position Sensitiv Detector or PSD) and some simple optics to detect distance"

http://www.sharpsma.com/webfm_send/1205