Step 3: Code time!

Picture of 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"
  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
    else{  // beeps faster when an obstacle approaches
      delay(150-distance);  // adjust this value for your convenience
      delay(150-distance);  // adjust this value for your convenience
  else{  // off if there is no obstacle

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.
jrešetar1 year ago
I did it as you said and I have one problem.
It works great but only when light is on. When I turn light off LEDs turn on with no obstacle in front of it.
Why is that so?
Please help me.
MauS092 years ago
Hello. I was wondering if you could answer this question for me. Do you know an Arduino program code and a way to add an extra Infrared sensor that can be used as a on and off switch so i can cut the project off and on whenever an object passes by the infrared sensor? I apologize if my question is confusing. Hope you understand.
iaraújo2 years ago
hi, could i use the HXD Buzzer instead HYDZ, as you used? because i'm using the first one and it's not working (i can't hear any sound)

Thanks you!
Is there any way to get approximate distance from this? It seems like different objects will scatter IR differently and that the intensity of returned IR would vary considerably between environments. Is this mainly used to tell if there is an IR reflective object within a foot of the sensor?
ggutshal2 years ago
Couldn't your readIR function be more simply written as:

int readIR(int times)
for(int x=0;x {
ambientIR = analogRead(IRpin);
obstacleIR = analogRead(IRpin);
distance += (ambientIR-obstacleIR);

There is no need to use an array to supply the average reading unless the individual values that contribute to the final average are also needed for something.
jabujavi2 years ago
¿?¿? for(int x=0;x digitalWrite(IRemitter,LOW); ¿?¿?
How do you do the for bucles? and in function readIR you dont use tha variabla times until return? could you explain this code?
ricardouvina (author)  jabujavi2 years ago
I'm sorry there was an error in this line of the code that is displayed at the step, but if you download the file it is just fine. I will fix it, thanks for warning.
The variable "times" is to repeat the "for" loop as many times you want, the more it repeats more accurate the value returned will be. In my code I repeat it for 5 times.