How does IR Sensor work? | IR Sensor and Arduino Tutorial

Introduction to IR Sensors

If you are a beginner and new to Electronics and Robotics, you might have heard the name of this sensor multiple times – The IR Sensor. But you might not know what it is. This is one of the most widely used sensors in hobby projects like a burglar alarm and line follower robots.

In this post, I will explain everything you need to know about IR Sensor – What is an IR Sensor, How an IR Sensor works and How to connect IR Sensor with Arduino?

What is an IR Sensor?

IR sensor mainly consists of an IR transmitter (IR LED) and an IR receiver (Usually a photodiode).

IR Sensor
IR Sensor

IR LED always emits IR rays to the direction it is pointing to.

Now let us bring it closer to a surface. When the IR rays hit a surface, some rays will be reflected back depending upon the color of the surface. Means, the brighter the color is, the more IR will be reflected back.

Darker the color is, more IR will be absorbed by the surface and lesser IR rays will be reflected back.

These reflected rays are received by the Photodiode and depending upon the intensity of the received IR rays, the resistance of the photodiode varies which will, in turn, varies the output voltage.

Thus it is possible to sense the color of the surface where the robot is running by looking into the reflected IR rays. So it is very easy to measure how bright the surface is which will make it easy for us to track the line.

IR Sensor

There are so many cheap IR sensors available online; you can purchase any of them. You will need at least two of them for making the line follower robot.

Setting up the IR Sensors

Most of the individual sensors will be somewhat like this.

IR Sensor

It will be having 3 pins. One for VCC (5V), GND (0V) and Vout (Output Voltage). VCC will power up the sensor circuit. The Vout pin will give us an output of 5V when the IR light is reflected back to the IR detector.

In some IR sensors, there will be analog output which will give you the voltage reading between 0 and 5 V. You should connect the analog output of this sensor to analog input pin of arduino like A0.

Connections – IR Sensor to Arduino

  • IR Sensor ——- Arduino
  • Vin —–5V of Arduino
  • Gnd —– Gnd of Arduino
  • Vout —– D13 of Arduino
  • Vout (Analog) —– A0 of Arduino

IR Sensor Arduino Code

int ir_analog = A0;
int ir_analog_val;
int ir_digital_val;
int ir_digital=D13;

void setup()
{
pinMode(ir_analog, INPUT);
pinMode(ir_digital, INPUT);
}
void loop()
{
ir_analog_val= analogRead(ir_analog);
ir_digital_val = digitalRead(ir_digital);

Serial.print("Analog Value - ");
Serial.println(ir_analog_val);
Serial.print("Digital Value - ");
Serial.println(ir_digital_val);
}

Just select the board and upload the sketch. Thats it Guys. Your IR Sensor is now ready to use. Just point the IR sensor to something and it will start showing you the values corresponding g to the colour of the object in front of it.


Similar Posts

One Comment

  1. This tutorial really helped me in Understanding how to use the IR sensor. Well explained. Keep the good work.

Leave a Reply

Your email address will not be published. Required fields are marked *