IR Sensor With Arduino: wiring and code explained

4.1
(9)

Last Updated on 13th April 2024 by peppe8o

In this tutorial, we’ll interface the IR sensor with Arduino Uno including coding, connection diagram, and listing the components.

To make Line Following Robot and detection of object, IR sensor is best to use in such situation

IR Sensor Information

In order to be able to interface IR sensor with Arduino, it is necessary to understand what IR sensor actually is. IR stands for the infrared region. Sensors capable of working with that region of light are IR sensors. IR technology is widely used in industries and our daily life. For example in the TV remote, IR technology is used for the communication between the TV and it’s remote allowing the user to have a level of comfort by controlling the TV from a fair amount of distance rather than manual labour. Since the IR sensor uses low power that makes them user-friendly and uses widely.

In the electromagnetic spectrum, IR radiations lie in the visible and microwave region. The range of these waves is between 0.7 micrometers and 100 micrometers. IR spectrum can divide into three regions namely near-infrared, mid and far-infrared region. The range of near-infrared is between 0.7 and 3 micrometers. From 3 micrometer to 6 micrometer lies mid-infrared region and higher than 6 micrometer is a far-infrared region.

Features of IR sensor

Consisting of three pins, a potentiometer, and two LEDs, the IR sensor is an electronic device that emits radiations in order to get a sense of the surrounding. IR sensor has the capability to measure the heat of the object and can also detect motion. Such types of sensors that get the sense of the surrounding by only detecting/receiving or absorbing IR radiations rather than emitting IR radiations are passive IR sensors.

IR sensor

The human eye cannot detect IR radiations but to IR sensor these radiations are visible. At the end of the module, there are two LEDs. One is RED led which performs the function of the emitter and the other one is photodiode which works as a receiver. The emitter emits infrared radiations which after hitting some object receives by the detector photodiode is sensitive only to the radiations of the infrared region. When the emitted IR radiations fall on the photodiode, it produces resistance and voltage which is in proportion to the radiations fell upon.

Working of IR Sensor

The working of IR sensors in the basic sense is similar to that of ultrasonic sensors because both of them involve a transmitter and receiver. The sensor constitutes of an IR LED and an IR photodiode. By appearance, IR LED is similar to that of standard LED and it works as a transmitter that transmits infrared radiations which are not visible to the human eye.

On receiving side there is a photodiode which is different from standard photodiodes. Usually, photodiodes are sensitive to any light but the IR photodiode which is used in IR sensor is sensitive only to infrared radiations emitted by IR LED. The infrared photodiode responsive to infrared radiations emitted by infrared LED transmitter, generates resistance or voltage which is in proportion to the number of infrared radiations received by IR photodiode. This change in resistance or voltage is sent to Arduino and thus the motion of an object or its distance measures. This is the fundamental working principle of the IR sensor.

One of the major applications of IR sensors is their usage in sensing the white or black track for line-following robots. Such types of Robots use to follow a certain track to deliver the goods to a destined place. IR sensor through the pin OUT gives either logic 0 or 1. There is a threshold value of voltage. Any value under the threshold considers to be logic 0 and anything above that consider being logic 1. For example, if there is a white or black track and a Robot requires to remain on the track then it becomes necessary for the robot to sense that track. IR sensor returns logic 0 or 1 after detecting black or white track.

line_follower_robot_using_arduino_l298_motor_driver_and_ir_sensor_rbsPmUjP1z
Line following Robot

Role of Potentiometer

The potentiometer is present on the sensor for calibration. The threshold value which determines the logic 0 or logic 1 depends on the potentiometer. By rotating the potentiometer, you can calibrate or tune the sensor. In order to get the IR sensors working properly, calibration is important and the potentiometer present on the sensor provides that control to the user.

IR sensor potentiometer

Pinout of IR sensor

There are three pins on the sensor: 1) Vcc 2) Ground and 3) Out. VCC is a power pin where 5 volts from the microcontroller is provided. GND pin of IR sensor connects with the ground pin of Arduino. OUT pin connects to any digital pins.

Sr. IR Sensor pinArduino Uno Pin
1VCC pin5V of Arduino Uno
2GND pinGND pin of Arduino
3OUT pin Pin 2 or any other digital pin of Arduino Uno
IR sensor

What We Need

Arduino Uno R3 board

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:

Step-by-Step Procedure

Wiring Diagram

Please prepare wiring according to the following picture.

There are three pins for IR Sensor, Starting from the right side, pin 1 is VCC which needs to connect with VCC. Pin 2 is a GND that connects to the GND of Arduino. Pin 3 is the OUT wire to Arduino pin 2:

Connection diagram of IR sensor with arduino
Wiring Diagram

Get the IR Sensor Code

Connect your PC to Arduino and open Arduino IDE. For the very first steps, you can refer to Connecting Windows PC with Arduino tutorial. You can get the .ino code from my download area with the following link:

IRsensorcode.ino

Code Explanation Step by Step

The required code is really simple as IR logic is already built into the IR Sensor.

In the beginning, we declare the ir_sensor_pin with the number of Arduino pin we’ll use. Then in the setup function, we set this pin as an input.

int ir_sensor_pin = 2; // IR sensor pin
void setup()
{
  Serial.begin(9600); // Serial monitor at baud rate 9600
  pinMode(ir_sensor_pin, INPUT); // Pin set as input
}

In the loop function, this sensor pin is read. If it returns logic 1 then it means that an object (or white/black line) has been detected. Otherwise, it means that no objects (or lines) have been detected.

void loop() {
  if (digitalRead(ir_sensor_pin)) // if Pin logic is HIGH
  {
    Serial.println("Object detected"); // display on Serial monitor when object detected 
  }

  else {
   Serial.println("Object not detected"); // display on Serial when object not detected 
  }
}

Enjoy!

Please find more tutorials on Arduino in peppe8o Arduino archives.

Umar Jamil

For any queries and help for work, please contact me at:
Whatsapp: +92-346-661-7017
Email: [email protected]

How useful was this post?

Click on a star to rate it anonymously!

Average rating 4.1 / 5. Vote count: 9

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?