Simple IR Proximity Sensor With Arduino

650K580164

Intro: Simple IR Proximity Sensor With Arduino

Hello guys! In this instructable I'll teach you how to make a very simple proximity sensor using infrared LEDs and Arduino.
After several times trying to optimize it, I finally came up with something that is quite simple e precise. Just like my first instructable, this project is perfect for beginners in the arduino's world, with a few components anyone can make it. I hope you all enjoy it.

STEP 1: Materials

For this instructable you are gonna need:
- Arduino
- 1 IR LED receiver (photodiode with 2 pins, not the phototransistor with 3 pins)
- IR LED emitters (as much as you can get, but at least 2)
- 100K resistor (brown black yellow)
- Jumper wires
- Breadboard
- Electrical tape
- Buzzer(optional)

Be careful to don't mix up the LED receiver with the LED emitter, they all look the same.

STEP 2: Preparing the Sensor

Before mount it, we should prepare the IR LED receiver to don't receive light from the sides, so than the sensor is more directional.
I use electrical tape to make it, but you can use a straw or anything that blocks the light from the sides.
Cut a small piece of electrical tape and wrap it around the IR LED receiver, forming a tube. Trim the edge with a pair of scissors until it's about 1cm long.
Have a look at images to see how I made it with electrical tape.

STEP 3: Code Time!

Write the code above on the Arduino program and upload it to the Arduino.
I also added an buzzer to this project if you wanna "hear" the distance.

// Simple Proximity Sensor using Infrared
// Description: Measure the distance to an obstacle using infrared light emitted by IR LED and
//   read the value with a IR photodiode. The accuracy is not perfect, but works great
//   with minor projects.
// Author: Ricardo Ouvina
// Date: 01/10/2012
// Version: 1.0

int IRpin = A0;               // IR photodiode on analog pin A0
int IRemitter = 2;            // IR emitter LED on digital pin 2
int ambientIR;                // variable to store the IR coming from the ambient
int obstacleIR;               // variable to store the IR coming from the object
int value[10];                // variable to store the IR values
int distance;                 // variable that will tell if there is an obstacle or not

void setup(){
  Serial.begin(9600);         // initializing Serial monitor
  pinMode(IRemitter,OUTPUT);  // IR emitter LED on digital pin 2
  digitalWrite(IRemitter,LOW);// setup IR LED as off
  pinMode(11,OUTPUT);         // buzzer in digital pin 11
}

void loop(){
  distance = readIR(5);       // calling the function that will read the distance and passing the "accuracy" to it
  Serial.println(distance);   // writing the read value on Serial monitor
  // buzzer();                // uncomment to activate the buzzer function
}

int readIR(int times){
  for(int x=0;x<times;x++){

    digitalWrite(IRemitter,LOW);           // turning the IR LEDs off to read the IR coming from the ambient
    delay(1);                                             // minimum delay necessary to read values
    ambientIR = analogRead(IRpin);  // storing IR coming from the ambient
    digitalWrite(IRemitter,HIGH);          // turning the IR LEDs on to read the IR coming from the obstacle
    delay(1);                                             // minimum delay necessary to read values
    obstacleIR = analogRead(IRpin);  // storing IR coming from the obstacle
    value[x] = ambientIR-obstacleIR;   // calculating changes in IR values and storing it for future average
  }

  for(int x=0;x<times;x++){        // calculating the average based on the "accuracy"
    distance+=value[x];
  }
  return(distance/times);            // return the final value
}


//-- Function to sound a buzzer for audible measurements --//
void buzzer(){
  if (distance>1){
    if(distance>100){ // continuous sound if the obstacle is too close
      digitalWrite(11,HIGH);
    }
    else{  // beeps faster when an obstacle approaches
      digitalWrite(11,HIGH);
      delay(150-distance);  // adjust this value for your convenience
      digitalWrite(11,LOW);
      delay(150-distance);  // adjust this value for your convenience
    }
  }
  else{  // off if there is no obstacle
    digitalWrite(11,LOW);
  }
}

Of course you can edit it to fit in your own project. You can for example make a robot change direction or velocity based on the distance read from the IR sensor.

STEP 4: Connect It Up.

Connect the resistor from the 5V pin to the anode pin of the IR LED receiver. All the anodes pins of the IR LEDs emitters to the digital pin 2. A wire goes from the analog pin 0 to the anode pin of IR LED receiver. Don't forget to connect all the cathode pins of the LEDs to the ground pin.
The buzzer is optional, but if you are using it connect to the digital pin 11 and the ground.
Infrared light isn't visible to naked eye, but you can see it thru a digital camera, it helps to see if the LED is working or not.
Take a look at the pics.

STEP 5: It's Done!

The sensor responds to 10 inches (25cm) or closer, so it's good for small distances.
Feel free to modify this project to your way, and tell me your progress. Comments are welcome.
Here is a video I've made.

155 Comments

The code when uploaded says it is missing an h. file?
How can we replace it with the blinking bulbs. Because the sound is disturbing

Hi, Can this detect if your eye is open or closed.

what is the accuracy and range of the IR sensor.
hi, according you is this accurate and constant enough to use it as proximity sensor for a 3d printer? I'd like to enable auto bad levelling in my 3d printer

I made this circuit and it controls one led. It works Great!!

I was wondering if anyone knew how to make this work with multiple IR receivers on different analog pins?

Can it detect black color?

If it detects black color, then how do I differentiate between white and black color?

Also, what are the changes to be done in the code if I am using only one transmitter?

I will be very excited if you reply as soon as possible!

Also, what changes should I do in the code for me to detect only black color objects which will be placed at a small distance (~2cm-3cm) from the led pair.

Again I will be excited if you reply as soon as possible!

This isnt a color detector. Use LRD (light sensor to do that) this thing is a distance detector so with this you can make a buglar alarm, Objet avoider etc.

can i have controller to send a signal from sensor wireless to another controller to make a buzzer go off?

I have problem that my sensor is just reacting on ambient light how can i solve this?

how if using two or more sensors?

This Is the code with the buzzer working!!

int IRpin = A0; // IR photodiode on analog pin A0
int IRemitter = 2; // IR emitter LED on digital pin 2
int ambientIR; // variable to store the IR coming from the ambient
int obstacleIR; // variable to store the IR coming from the object
int value[10]; // variable to store the IR values
int distance; // variable that will tell if there is an obstacle or not

void setup(){
Serial.begin(9600); // initializing Serial monitor
pinMode(IRemitter,OUTPUT); // IR emitter LED on digital pin 2
digitalWrite(IRemitter,LOW);// setup IR LED as off
pinMode(11,OUTPUT); // buzzer in digital pin 11
}

void loop(){
distance = readIR(5); // calling the function that will read the distance and passing the "accuracy" to it
Serial.println(distance); // writing the read value on Serial monitor
buzzer(); // uncomment to activate the buzzer function
}

int readIR(int times){
for(int x=0;x<times;x++){
digitalWrite(IRemitter,LOW); //turning the IR LEDs off to read the IR coming from the ambient
delay(1); // minimum delay necessary to read values
ambientIR = analogRead(IRpin); // storing IR coming from the ambient
digitalWrite(IRemitter,HIGH); //turning the IR LEDs on to read the IR coming from the obstacle
delay(1); // minimum delay necessary to read values
obstacleIR = analogRead(IRpin); // storing IR coming from the obstacle
value[x] = ambientIR-obstacleIR; // calculating changes in IR values and storing it for future average
}

for(int x=0;x<times;x++){ // calculating the average based on the "accuracy"
distance+=value[x];
}
return(distance/times); // return the final value
}


//-- Function to sound a buzzer for audible measurements --//
void buzzer(){
if (distance>1){
if(distance>100){ // continuous sound if the obstacle is too close
digitalWrite(11,HIGH);
}
else{ // bips faster when an obstacle approaches
digitalWrite(11,HIGH);
delay(150-distance); // adjust this value for your convenience
digitalWrite(11,LOW);
delay(150-distance); // adjust this value for your convenience
}
}
else{ // off if there is no obstacle
digitalWrite(11,LOW);
}
}

Which arduino board will interface both IR sensors and image input...... And give one output at a time..... My project is smart traffic light control using IR sensors and image processing technique

I think arduino mega or due will do the job! :)

sir. can u help me with my project. im working on project that using this sensor. my project smart street light that can detect car movements then the light will be on. can u help me please.

I did not understand your project much but if you replace the buzzer with an led it will work as well! Good luck and let me know if you need anything else!

Hello, can any one tell which arduino board will interface both IR sensors and image input..... And give one output
More Comments