Arduino

How to Interface IR Sensor Module with Arduino Nano

Arduino Nano is a compact microcontroller board that can be interfaced with a number of sensors including the IR sensor. An Arduino IR sensor is a device that can detect infrared radiation, a type of electromagnetic radiation that has a wavelength greater than visible light. This article covers complete code and steps required to program IR sensors with Arduino Nano.

1: Introduction to IR Sensor Arduino

An IR or Infrared sensor is a device that measures the infrared radiations around its surrounding by emitting the IR rays and then receiving the reflected ray back. It outputs a digital signal once reflected rays are received back.

These sensors are commonly used in a variety of applications, including remote control systems, motion detectors, and robotics. The Arduino board allows users to program and control the IR sensor using a simple set of instructions. With the ability to sense infrared radiation, the Arduino IR sensor can be used to detect the presence of objects, measure temperature, and even control other devices.

2: Working of IR Sensor Arduino

An IR sensor works by emitting a beam of infrared radiation and detecting when the beam is reflected to the sensor. When the beam is interrupted, the sensor will output a digital signal. This signal can be used to trigger an action or event, such as turning on a light or activating a motor.

Graphical user interface, text, application, chat or text message Description automatically generated

IR sensor feature two main components:

  • IR Transmitter: An infrared LED as a transmitter.
  • IR Receiver: A photodiode is used as a receiver which after receiving reflected rays generates output.

Once the voltage is applied to Infrared Light Emitting Diode it emits an infrared light ray. Light travels through the air and after hitting the object it reflects to the receiving sensor that is a photodiode.

If the object is closer to the IR sensor a strong light will be reflected. As the object moves away the reflected signal received is weaker.

When the IR sensor is active it outputs a LOW signal at its output pin that can be read by any microcontroller board.

Another interesting thing about this board is that it has two on board LEDs, one for the power and second for the output signal when the sensor is triggered by any object.

3: IR Sensor Pinout

An IR sensor typically has three pins:

  • VCC: The VCC pin is the power supply pin, which is used to provide power to the sensor.
  • GND: The GND pin is the ground pin, which is used to ground the sensor.
  • OUT: The OUT pin is used to send the sensor’s output signal to a microcontroller or other device.

Diagram, engineering drawing, schematic Description automatically generated

In addition, IR sensor also has:

  • IR Emitter: Sends the IR ray.
  • IR Receiver: Receives the reflected ray.
  • Potentiometer: Set the distance threshold by setting the sensor sensitivity.

4: IR Sensor Interfacing with Arduino Nano

To use the IR sensor with an Arduino, connect VCC with 3.3V or 5V pin on the Arduino. The OUT pin can be connected to digital pins of the Nano board. The GND pin will be connected to Arduino Nano ground.

Once the connections are made, you can use the Arduino’s programming environment to read the sensor’s output and perform actions based on the detected infrared radiation.

4.1: Schematic

Given table explains the pin diagram of IR sensor with an Arduino Nano:

IR Sensor Pin Arduino Pin
VCC VIN/5V/3.3V
GND GND
OUT D2

The LED at D3 is connected which glows once the object is detected by the Arduino board.

4.2: Code

Connect Arduino Nano with PC and upload below code.

#define IR_Sensor 2 /*D2 IR pin defined*/
#define LED 3       /*D3 LED Pin defined*/
int IR;             /*Variable that will store IR output status*/
void setup()
{
pinMode(IR_Sensor, INPUT);   /*IR Pin D2 defined as Input*/
pinMode(LED, OUTPUT);        /*D3 pin for LED is set as Output*/
}
void loop(){
  IR=digitalRead(IR_Sensor);  /*digital read function to check IR pin status*/
  if(IR==LOW){               /*If sensor detect any reflected ray*/
  digitalWrite(LED,HIGH);   /*LED will turn ON*/
}
else {
  digitalWrite(LED,LOW);  /*if no reflection detected LED will remain OFF*/
}
}

In above given code first we initialized the digital pins for IR sensor and LED. D2 and D3 pins of IR sensor are defined for IR sensor and LED respectively.

Next using pinMode() function IR sensor pin is set as input and LED pin is set as output. If condition is used for IR sensor. If the input received from IR is LOW LED will turn ON. On the other hand, if no reflected wave is detected by the IR sensor the IR output will be HIGH and the LED will remain OFF.

Graphical user interface, text, application Description automatically generated

4.3: Output

After uploading code to the Nano board, we can test the circuit by using any object which comes in front of the infrared sensor.

Below given image shows LED is OFF as IR radiations are not reflected by any of the objects. Sensor is not triggered which means it will send a HIGH signal at its output pin.

A picture containing text Description automatically generated

Now as the object is in front of the IR sensor, radiation gets reflected and received by the photodiode on the IR sensor, so the LED is turned ON. In this case a LOW signal will be generated by an IR sensor.

A picture containing diagram Description automatically generated

Conclusion

IR or infrared sensors can detect the presence of an object. Using Arduino Nano digital pins, we can receive signals from IR sensor output and can trigger response according to need. IR sensors have multiple applications including the remote-control systems, motion detectors, and robotics. This article explains steps to integrate IR sensors with Arduino Nano using Arduino code.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.