Beginners Guide To IR Sensor and how to use it with Arduino

Hey guys! Today we are going to learn about IR sensors, their working, types, and pinout. After covering the basics of IR sensors, you will learn how to use them with an Arduino board.

 

What is an IR Sensor?

IR sensor is a type of sensor which uses Infrared waves or light to detect the object placed ahead of it. Since this sensor uses Infrared waves to detect the object, it is called an Infrared sensor. 

IR sensor
IR Sensor

Just like visible light, Infrared wave/light is part of the electromagnetic spectrum. But unlike sunlight/visible light, the human eye cannot see IR waves. Although it can be detected as heat.

You may not be aware that you encounter Infrared waves every day. For example, a TV remote uses light waves just beyond the visible spectrum of light—infrared light waves—to change channels on your TV. 

And Ir sensor uses the same waves to detect the obstacle.

Board layout of an IR Sensor

Every IR Sensor or Infrared Sensor has two main parts. IR Transmitter and IR Reciever. The work of an IR transmitter or Infrared transmitter is to transmit the infrared waves.

Whereas the work of the IR receiver is to receive these transmitted infrared waves. IR receiver constantly sends digital data in the form of 0 or 1 to the Vout pin of the sensor.

The single potentiometer on most of the IR sensors is used to set the sensitivity of the sensor or the distance up to which it detects the obstacle. So by rotating the potentiometer you can set the detection distance.

There are generally two LEDs on the IR sensor, one is a power indicator and the other one is the output/obstacle indicator.

Different versions of IR sensor:

Now depending upon the number of Pins and potentiometer you may come across different versions of Ir Sensor.

IR sensor with three pins:

IR sensor

The IR sensor with three pins has the following pinout: Power pin, Ground pin, and digital output pin.

The digital output pin provides data in digital form i.e. 0(HIGH) or 1(HIGH)

IR sensor with four pins(Output type):

IR sensor with 4 pins(output type)
IR sensor with 4 pins(output type)

The IR sensor with four pins has the following pinout: Power pin, Ground pin, digital output, and analog output pin.

The analog output pin provides data in analog form i.e. between 0 volts and 5 volts(voltage at power pin)

IR sensor with four pins(Enabling/disabling):

IR Sensor with 4 pins(Enable)
IR Sensor with 4 pins(Enable)

The IR sensor with four pins has the following pinout: Power pin, Ground pin, digital output pin, and Enable pin.

The enable pin is used to enable/disable the IR sensor output.

The IR sensor with Single potentiometer:

The single potentiometer on most of the IR sensors is used to set the sensitivity of the sensor or the distance up to which it detects the obstacle. So by rotating the potentiometer you can set the detection distance.

The IR sensor with Two potentiometers:

The second potentiometer is used to set the frequency of IR waves transmission.

Working of an IR Sensor

Working of IR transmitter and receiver on White Surface
Working of IR transmitter and receiver on White Surface

If there is an object in front of the IR sensor, the transmitted infrared waves from the IR transmitter reflect from that object and are received by the IR receiver. IR sensor gives 0 in this condition. 

Similarly, if there is a white surface in front of the sensor, the IR waves are reflected back from the surface(the white surface does not absorb any light).

Working of IR transmitter and IR Receiver on Black Surface

When there is no object in front of the IR sensor, the transmitted infrared waves from the IR transmitter are not received by the IR receiver. And IR sensor gives 1 in this condition.

Similarly, if there is a black surface in front of the sensor, the IR waves are not reflected back from the surface(the black surface absorbs the light completely).

IR sensor pinout(3 pins):

1)  Ground

2)  5 volt

3)  Vout (digital)

**IR receiver constantly sends digital data in the form of 0 or 1 to the Vout pin of the sensor.

Pinout of 3 Pins IR sensor
Pinout of 3 Pins IR sensor

IR sensor pinout(4 pins):

1)  Ground

2)  5 volt

3)  Vout (digital)

4) Enable

**IR receiver constantly sends digital data in the form of 0 or 1 to the Vout pin of the sensor.

Pinout of 4 Pins IR sensor
Pinout of 4 Pins IR sensor

Image Source: Osoyoo.com

Watch this video for a detailed explanation :

 

How to use Arduino with IR Sensor:

Circuit Connections:

Connect the Vout pin of the sensor to digital pin 7 of Arduino.

Connect the Vin pin of the sensor to the 5 V pin of Arduino.

Connect the Ground pin of the sensor to the GND pin of Arduino.

Connect the LED to digital pin 13.

Program: LED Control using IR Sensor input

void setup() {
  pinMode(7,INPUT);
  Serial.begin(9600);
  pinMode(13,OUTPUT);
}

void loop() {
  
Serial.print("IRSensorip  ");
Serial.println(digitalRead(7));

if(digitalRead(7)==0)
{
  digitalWrite(13,HIGH);
  }
 else{
    digitalWrite(13,LOW);
    }

}
DOWNLOAD THIS CODE

13 thoughts on “Beginners Guide To IR Sensor and how to use it with Arduino”

  1. Hi
    I want to build circuit with 2 relays , IR sensor and manual switch.
    I have a problem with code sketch. Please send me email to trucker.poland @gmail com and let me know if are you able to help me or not .
    Please answer as soon as possible.
    Kind regards Greg

    Reply
    • Hi Gary,
      Do you want to control each LED from an individual sensor? Please provide more details so that we can help you out.

      Reply
      • Ankit,
        Yes I would like to control 5 or 6 LED’s with indivdual sr 505 IR sensors. The object is
        to know the location of a model train in a tunnel.
        Thanks,
        Gary

        Reply
  2. yes Ankit,
    five or six SR501 infrad detectors controlling one Led each. I would like to use a arduino nano for this.
    I am trying to tell the location of a model trains location in a tunnel.
    Thank you for your quick response.

    Gary

    Reply
    • Hi Gary,
      I have compiled this code for you as fast as I could. I haven’t tested it on the actual setup but this should work.

      Controlling 5 different LEDs from 5 different IR Sensors:

      
      const int IR1 = 2, IR2 = 3, IR3 = 4, IR4 = 5, IR5 = 6;// IR sensors connected from digital pin 2 to 6
      const int LED1 = 9, LED2 = 10, LED3 = 11, LED4 = 12, LED5 = 13;// LEDs connected from digital pin 9 to 13
      
      void setup() {
        for (int i = LED1; i <= LED5; i++) // LED's connected from pin 9 to 13
        {
          pinMode(i, OUTPUT);
        }
        
        for (int i = LED1; i <= LED5; i++) // Initially all LED's are kept OFF
        {
          digitalWrite(i, 0);
        }
      
      }
      
      void loop() {
      
        if (digitalRead(IR1) == 0)
          digitalWrite(LED1, HIGH);
        else
          digitalWrite(LED1, LOW);
      
        if (digitalRead(IR2) == 0)
          digitalWrite(LED2, HIGH);
        else
          digitalWrite(LED2, LOW);
      
        if (digitalRead(IR3) == 0)
          digitalWrite(LED3, HIGH);
        else
          digitalWrite(LED3, LOW); 
          
        if (digitalRead(IR4) == 0)
          digitalWrite(LED4, HIGH);
        else
          digitalWrite(LED4, LOW);
          
        if (digitalRead(IR5) == 0)
          digitalWrite(LED5, HIGH);
        else
          digitalWrite(LED5, LOW);
      }
      

      You can use serial commands for real-time sensor data collection.
      Let me know if this works or not.

      Reply
      • Ankit,
        I have had a quick test with two IR detectors (waiting for the mail to bring more) and it does
        work. I am not sure what you mean by using serial commands?

        I guess I would have to add the command: serial.begin (9600); but not sure were it goes.

        This is my first attempt with arduino and I really appreciate the help.
        Gary

        Reply
        • Hi Gary,
          Serial commands are used for serial communication between the Arduino board and Arduino software to receive data(such as from the sensor, keyboard)/send data. The data gets printed onto the Serial monitor of the Arduino software. You can also send data to the Arduino board using this serial monitor(using appropriate serial commands).
          In your case, you can use the Serial monitor to print the real-time sensor readings(0 or 1) for sensor calibration and better control.
          Serial.begin(9600) command initialize the communication hence goes into the void setup(). Any other serial command will not work if this is not used in void setup().
          And I understand that learning Arduino can be very difficult if you are an absolute beginner.
          You can learn Arduino programming from the internet/youtube as there are a ton of quality material available there. If you like, you can also go through this article on Arduino programming for beginners posted on our website:
          Beginners guide to Arduino programming

          Reply
  3. Ankit,
    Yes, I would like to control 5 or 6 LED’s with individul SR505 IR Sensors. The object is to know
    the location of a model train in a tunnel.
    Thanks,
    Gary

    Reply

Leave a Comment