Categories: Beginners

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

Published by
Jithin

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

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.

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.


Jithin

Just a normal human being with knowledge in computer networking, penetration testing, programming in C++, embedded C, and electronics stuff such as circuit designing, PCB developing, soldering, and other electronics and robotics field.

View Comments

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

Published by
Jithin

Recent Posts

Transform Your Trash into Treasure: 10 Genius Ways to Reuse Old Electronics!

Discover creative ways to recycle old electronics into new, useful gadgets. Turn your electronic waste… Read More

14 hours ago

Not Just for Lights – Top 10 LED Projects That Will Shock You!

Explore the top 10 advanced LED projects that go beyond basic lighting. Perfect for electronics… Read More

1 day ago

Revolutionizing PCB Fabrication: How UV Resin is Redefining the Art of Circuit Board Design

The integration of UV resin into the PCB production process represents a great achievement. This… Read More

1 month ago

Unlock the Secrets to Thriving in Electronics Engineering: A Must-Read Guide for Aspiring Engineers

Discover essential tips for electronics engineering graduates to gain practical experience, from internships to DIY… Read More

1 month ago

The Game Changer: How AI is Transforming Digital Marketing Strategies

Explore how AI is transforming digital marketing, from personalization to predictive analytics, and revolutionizing strategies… Read More

1 month ago

Unlock the Future: Top 5 AI Tools Transforming The Industry Today

Discover how AI tools are revolutionizing industries, enhancing efficiency, and fostering innovation in our digital… Read More

2 months ago