A distance sensor is the most important sensor for any robot. It's usually referred to as the "eyes" of a robot. Distance sensors are very useful as we can make systems that react based on how close we are to them or based on the presence of various obstacles.

There are two common technologies used in amateur distance sensing: infrared sensors, such as the classic Sharp IR, and ultrasonic sensors, usually called sonars. Now, let's build a distance-controlled LED!

To build a distance-controlled LED, we will need the following parts:

  • An Arduino board connected to a computer via USB
  • One LED
  • A Sharp infrared proximity sensor or other analog distance sensors. We can find such sensors at Sparkfun, Adafruit, Pololu or other stores.
This instructable and many more can be found in my Arduino Development Cookbook available here. :D

Step 1: How to Connect the Sensor

Connecting a Sharp IR is easy. Maybe this is why it's so popular. Follow these simple steps to connect one:

  1. Each Sharp IR has three pins. One is the power input, which we connect to 5V. Another is the ground that we will connect to one GND pin. Lastly, there is the analog output pin that needs to be connected to an analog input. Here, we used pin A0.
  2. We will make a small illegal connection here. We will directly connect the LED to the Arduino without any resistor. For low-power LEDs, there is no problem, and neither the Arduino nor the LED will be affected. Plug the negative terminal to GND and the other terminal to one of the pins close by. Here, we used pin 11 for its PWM functionality.

But please don't exceed a 3 mm, 10–20 mA LED. No high-power LEDs here! It could easily fry the LED or the Arduino. If we don't know how powerful our LED is, we should just mount a 220-ohm resistor in series.

Step 2: Code

The following code will read the value of the sensor, print it on the serial connection, and vary the LED intensity using PWM, to match the distance:

// Declare the used sensor pin <br>int sensorPin = A0;
int LED = 11; // Declare the connected LED
void setup(){
  // Start the Serial connection
void loop(){
  // Read the analog value of the sensor
  int val = analogRead(A0);
  // Print the value over Serial
  // Write the value to the LED using PWM
  analogWrite(LED, val/4);
  // Wait a little for the data to print

Step 3: Code Breakdown

First, we declare two variables for the built-in LED and for the used analog port to which we connected the Sharp IR sensor:

int sensorPin = A0;<br>int LED = 11;

In the setup() function, we only start the serial connection. We don't need to declare the LED pin as output because we use the analogWrite() function, which doesn't require a declaration:

void setup(){

In the loop() function, we continuously read the value of the sensor using the analogRead() function; then we print it on the serial:

int val = analogRead(A0);

Since PWM takes values from 0 to 255 and the analogRead() function returns values from 0 to 1023, we divide the value of analogRead() by 4 when we use it in analogWrite():

analogWrite(LED, val/4);

Step 4: More About Distance Sensors

Distance sensors have a huge market with hundreds upon hundreds of different models. Generally, in the cheap, hobbyist section, we can either find infrared sensors—such as the Sharp IR—or ultrasonic sensors.

An infrared sensor has a very narrow sensing beam. This means it can detect objects in tight places without interfering with other objects. However, if an object is too thin or has holes in its body, the sensor beam might go through it and give false readings.

An ultrasonic sensor, typically called sonar, uses sound above the normal hearing frequency to detect distance. It does so by emitting a short sound pulse and waiting for it to return. It measures the time it takes for the sound to travel, bounce on objects, and then travel back to the sensor.

Because the speed of sound is known, the total time it takes for the sound to return is dependent on the distance to the objects. This creates a very wide sensing beam. It is useful in many applications, especially when we need to detect large, complex objects. However, the wide beam will create interference if we have two objects in range. It will always detect the closer one.

An important thing to remember is that infrared sensors are dependent on the color of the measured objects while sonar is generally not affected by parameters except the shape and distance of the object.

More topics regarding sensors such as noise reduction, accelerometers or GPS can be found in my Arduino Development Cookbook available here. :D

thanks a lot I've been finding how to use it. nice job
<p>hey can u give me some ideas on how to make projects through interfacing?? plss pls pls</p>
Thanks for the instruction. <br>I've taken a couple similar sensors from printers, and plan later to play with very tiny ones from cameras. <br>Here is a question. After finding which wires give power and signal I connected to an Arduino and read the signal. The device is more than just the IR and pick-up, it's on a tiny pc with a sms 8 pin if and an array of resistors and caps. There were only the three gray wires to use. Using Crtek's Ible on PLX-DAQ data through Excel I graphed the data and saw a clean sine wave just outside a second in length. Why would an optical sensor perform in this manner? Or simply, have you run across this and know anything about it?
<p>Well, I tried both sensors for a simple robot and all you say is true. However, I found quite some flaws in the sonar sensors. Yes they work fine when trying to detect diffuse objects like your shoe, cat or a chair. It goes completly off when approaching a hard surface like a wall. The wall will refelct the sound waves as if it is a mirror. Any reflections (if at all received by the sensor) will appear to come from the mirrored object i.e. behind the wall. </p><p>I made a separate test set-up after experiencing these issues and found that the ultra-sound beam is quite narrow. I placed a hard object say 40cm away in front of and perpendicular to the sensor. Then I gradually turned it some degrees to check at what angle the reflection miss the sensor and the object 'dissappears'. Already at angles between 10 - 15 degrees this is the case (depends on distance).</p>
It depends a lot on the sensor of course.<br>For example, Maxbotix had a series of sonars with various beam parameters: <br>http://www.maxbotix.com/Ultrasonic_Sensors.htm#XL-EZ <br>
Thanks! Can you make this detect something through plexiglass or some way to make it waterproof from rain ?
Hmm... the wavelength of the sensor is 850nm +- 70 nm. It should work through glass and maybe thin plexiglass. <br>One thing for sure: Put the glass very close to the sensor and put a small blank piece of material between the transmitter and the receptor so that light doesn't bounce on the surface of the glass, into the sensor.<br><br>Another solution would be to put it inside, at a distance from the exterior wall, so that water can't get in.<br><br>Third solution, buy a sensor which is waterproof. Industrial proximity sensors from Sick, Turk or Omron can work even in 1 meter water! (But they are expensive)
Good job !. Is it possible to integrate the sensor with labview without using Arduino ?.<br><br>Thank you,
<p>You need some sort of Analog-Digital Converter (ADC) connected to your computer, which LabView can access. Easiest and cheapest thing I have in mind is indeed the Arduino.</p>
Good tutorials! You have a good, easy to read and understand writing style!

About This Instructable




Bio: Inventor, Author, Senior R&D Engineer, Entrepreneur, Forbes 30 Under 30
More by cornelam:Arduino Servo Motors Arduino Distance Sensors Arduino Stepper Motors 
Add instructable to: