Guide to IR Sensor- Pinout, Working & Arduino Project

Last updated on March 23rd, 2024 at 04:33 pm

IR Sensor is a device that detects the object in front of it using IR or Infrared waves. It is also used to differentiate between black and white colors. Hence it is commonly used as the main sensor in a line follower robot.

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.

Every IR sensor has two main parts. IR Transmitter and IR Reciever. The work of an IR transmitter is to transmit 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.

Note: The single potentiometer on most of the these 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 an infrared sensor, one is a power indicator and the other one is the output/obstacle indicator.

 Watch this video for a detailed explanation :

How does an IR Sensor work?

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 sensor, the transmitted infrared waves from the IR transmitter reflect from that object and are received by the 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 a black surface

When there is no object in front, the transmitted infrared waves from the IR transmitter are not reflected and received by the IR receiver. The 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).

Different versions of IR sensor with pinout

Depending on the number of Pins and potentiometers, you may come across different versions of this sensor.

Based on the number of pins

1. With three pins

IR sensor

It 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).

Pinout of 3 Pins IR sensor
Pinout of 3 Pins IR sensor
  • Ground
  • 5 volt
  • Vout (digital)

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

2. With four pins(output type)

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

The 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)

3. With four pins(Enabling/disabling)

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

It 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.

Pinout of 4 Pins IR sensor
Pinout of 4 Pins IR sensor
  • Ground
  • 5 volt
  • Vout (digital)
  • Enable

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

Image Source: Osoyoo.com

Based on the number of potentiometers

1. With a single potentiometer

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

2. With two potentiometers

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

Using IR sensor with Arduino

Now let’s interface the sensor with an Arduino UNO board. Gather these components:

  • Arduino UNO
  • IR sensor
  • LED

Circuit diagram

Connect the components as shown below:

LED control using IR sensor with Arduino
Circuit diagram

Connect the sensor’s VCC and GND pins to the Arduino’s 5V and GND pins, respectively. Connect the digital input pin of the sensor to digital pin 7 of UNO. The anode terminal(long leg) of the LED to pin 13 and the cathode terminal to the GND pin.

Program

Then upload this code to your Arduino board:

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 the code from here


Photo of author

Ankit Negi

I am an electrical engineer by profession who loves to tinker with electronic devices and gadgets and have been doing so for over six years now. During this period, I have made many projects and helped thousands of students through my blog and YouTube videos. I am active on Linkedin.

15 thoughts on “Guide to IR Sensor- Pinout, Working & Arduino Project”

  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
      • Hi i want to control 2 9v dc motor and 2 ir sensor so like when my hand come near A sensor the A motor will work and can i also get the graph of all wires connected to the arduino
        thanks

        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