Robotics: How to Use an LED to Determine Distances
John Keogh | May 1, 2013
I looked at a few options for measuring distance for a small robot I'm working on, but it occurred to
me that I should be able to have the robot simply shine an LED on something, and, based on the amount of
light returned and the scatter of the light, be able to determine how far an object is from the
robot. This post describes how the experiment worked and how to do it yourself. The language used is
Objective-C, but the algorithm should be straight-forward to port if you are using another language.
This blog post has an accompanying video which briefly shows the robot that I mentioned above in action, and also
demonstrates using the technology this blog post covers:
The accuracy of the technique is reasonable, with the inaccuracy mostly related to the automatic adjustment of the iDevice camera to
changed ambient light levels when the LED is switched on.
These are the things that need to be done:
Make sure that the LED headlights are off
Pause for iDevice light level adjustment
Save image - this is the headlight off image
Turn LED headlights on
Do not pause for iDevice light level adjustment, the next image is saved quickly
Save image - this is the headlight on image
Compare pixel luminosity of headlight off and headlight on images.
take the inverse square of the ratio of changed pixels to the ones that remained the same and multiply that by an experimentally determined value
The calculation assumes an inverse square relationship between light intensity and distance, but it is actually more
complicated than that since light can be focused to attenuate more slowly over distance. Nonetheless, the assumption of
an inverse square relationship turned out to be reasonable.
There is also some choreography needed to do things in the right order. This references classes that
I am not going to release, but the choreography may be useful to have. The timing of saving of frames
is critical, since you need to have the LED on, but you need to avoid the automatic light level adjustment
of the iDevice camera, which will cause incorrect readings of number of pixels changed.