IR Sensor Module Interfacing with Microcontroller – Arduino, PIC

The IR sensor or infrared sensor module is the most popular and most frequently used type of sensor used to detect surrounding obstacles or objects and also in remote controlling functions. Generally, these sensors include an IR pair that has an IR LED & a photodiode. Here, IR LED is a type of LED, used to emit IR rays and the wavelength of these rays ranges from 700 nm – 1 mm. IR rays are imperceptible to our eyes. On the contrary, a photodiode which is also known as an IR receiver LED, is used to detect the IR rays. This article provides brief information on the IR sensor module interfacing with Arduino.


IR Sensor Module Interfacing with Arduino

IR sensor module interfacing with the Microcontroller Arduino  is shown below. Operation is the same with any other microcontroller except that the connecting PIN numbers and code will change as per the microcontroller. For example we have shown the interface here with Arduino and PIC microcontroller.

IR Sensor Module

IR sensor module hardware mainly includes IR LED/IR Tx, Photodiode/IR Rx, LM393 comparator IC, signal LED, power LED, Trimpot, and pins like VCC, GND & Output which are discussed below.

IR Sensor Module Components
IR Sensor Module Components

Infrared LED/IR Tx

An infrared LED or infrared transmitter is an LED mainly designed to emit IR rays. This light-emitting diode is similar to a normal LED with two terminals where the longer terminal is positive & the smaller terminal is negative. Once the power supply is given to this LED then starts emitting IR rays.

Photodiode Receiver/IR-Rx

A photodiode receiver or Infrared receiver is a semiconductor with a PN junction. This diode operates in reverse bias whenever light drops on it & the amount of flow of current is proportional to the amount. So this property will make it very useful for infrared detection. The photodiode is similar to a light-emitting diode with a coating in black color on its external side. The black color coating helps in absorbing the maximum amount of light. A photodiode is used to detect IR rays. This diode has extremely High resistance in the nonexistence of IR rays & becomes low once IR rays drop on it.

Pins

The VCC pin is a +5V power supply pin, the GND is a ground pin and the OUT pin is a digital output pin.

LM358 Op-amp

LM358 IC is an Op-Amp and is used in the infrared sensor as a voltage comparator. This operation amplifier is used to compare the threshold voltage set with the preset & series resistor voltage of the photodiode.

Trim pot

A trim pot is also known as a variable resistor or potentiometer. This is a 10k preset which is used to set the range of operation. To adjust the distance range of detection, the preset knob needs to be rotated and the efficient operation range is from 2 to 10 cm. If the preset knob is turned in a clockwise direction, then the range of detection will be enhanced. Similarly, if it is turned in a counter-clockwise direction, then the range of detection will be reduced.

Power LED

This power LED on the module is red color LED indicating whether the power supply of the IR Sensor is ON/OFF. Once we turn ON the power supply of the sensor then this power LED will be turned ON.

Output LED

The output LED on the module is green LED, used to indicate whether the IR sensor detects an obstacle or not. Once the sensor detects any obstacle then the output LED will turn ON.

Module Working

When this module is connected to a 5v power supply, the IR LED starts emitting IR rays. These rays arrive at the surface of an object. After that, some of the radiation can be reflected back toward the IR receiver (Photodiode). The IR receiver detects the IR light. Here, the threshold voltage at the Non-Inverting i/p of the IC can be set by simply turning the knob of the potentiometer for setting the sensitivity of the sensor.

Whenever the reflected IR light drops on the IR receiver, its resistance will drop & the voltage across the IR receiver also drops. Thus, a High amount of voltage from the IR receiver is provided to the Inverting input of the IC, and then this IC contrasts this voltage with the threshold voltage. So the Inverting input voltage is larger as compared to the Non-Inverting i/p voltage. Thus the output of the IC will be Low and the output of the sensor will be Low.

Whenever the IR receiver does not detect the IR light, the IR receiver’s resistance will be extremely high. Thus, a low amount of voltage from the IR receiver is provided to the Inverting input of the IC, and then it evaluates this voltage by the threshold voltage. The Inverting i/p voltage is below the Non-Inverting i/p voltage thus the output of this IC is high and the output of the sensor is high.

Arduino Uno

Please refer to this link to know more about; Arduino Uno Board.

LED

Please refer to this link to know more about. LED.

220Ω Resistor

A resistor is a passive electrical component including two terminals which is made with copper wires where these wires are coiled around a ceramic rod & the external side of the resistor is covered through an insulating paint. The main function of a resistor is to either regulate or restrict the current flow within electrical circuits. The resistor decreases the flow of current & also reduces the voltage within any particular part of the electrical circuit.

The interfacing of the IR sensor module with the Arduino uno board is shown below. The connections of this interfacing follow as;

IR Sensor Module Interfacing with Arduino Uno
IR Sensor Module Interfacing with Arduino Uno
  • The pin-13 of the Arduino pin is connected to the Anode pin of the LED through a resistor.
  • The GND pin of the Arduino pin is connected to the Cathode pin of the LED
  • The VCC pin of Arduino is connected to the VCC pin of the IR sensor.
  • The GND pin of Arduino is connected to the GND pin of the IR sensor.
  • The pin-10 of the Arduino pin is connected to the OUTPUT pin of the IR sensor.

Code

The required code for this interfacing is shown below.

int LEDpin = 13; // Define LED pin//
int obstaclePin = 10; // Define obstacle pin//
int hasObstacle = LOW; // There is no obstacle if it is low//
void setup() {
pinMode(LEDpin, OUTPUT); //Set LED pin as output//
pinMode(obstaclePin, INPUT); //Set obstacle pin as input//
Serial.begin(9600); // begin serial communication//
}
void loop() {
hasObstacle = digitalRead(obstaclePin);

if (hasObstacle == HIGH) // It is high if there is a obstacle//
{
Serial.println(“Stop something is ahead!!”);
digitalWrite(LEDpin, HIGH);
}
else {
Serial.println(“Path is clear”);
digitalWrite(LEDpin, LOW);
}
delay(200); // Delay 200ms//
}

Here integer type variables are first defined like LED pin, obstacle pin, and hasObstacle. Assigned 13 to LED, 10 to obstacle pin, and set the LOW state to hasObstacle. This is used mainly to read the status of the Arduino board’s pin-7. If the status is either HIGH/ LOW means, if there is an obstacle then it is HIGH, and if not then it is LOW.

In void setup(), need to define the mode of a pin; obstaclePin as input and LED as output. After that need to set the baud rate to 9600 for serial communication with the Serial.begin()function.

First in void loop(), we are reading the state of obstaclePin(10) is it either LOW/HIGH. Whenever we connect an infrared sensor over there, this sensor senses any obstacle by transmitting & receiving waves it provides a HIGH signal and sets Arduino’s obstaclePin(10) to HIGH. If the IR sensor does not sense any obstacle then it gives a LOW signal and sets Arduino’s obstaclePin(10) to LOW. This signal is stored within the variable hasObstacle, thus there are mainly two conditions for hasObstacle either LOW/HIGH.

After that, we will verify it in if condition. If it is HIGH then if the condition is matched and the message is printed on the serial monitor as Stop something is ahead and Turn ON the LED by making LEDpin-13 HIGH. If not the signal will be LOW, thus in the ‘else block’ a message will be printed on the serial monitor as the Path is clear and the LED will be Turned off. So this whole cycle will be delayed through 200 msec.

The working of this interfacing is; that whenever any obstacle is detected by a sensor, a message will be displayed on the serial monitor then the LED will be turned ON. Similarly, if there is no obstacle, then the LED will be turned OFF. In this manner, the IR sensor module is interfaced with Arduino Uno and it can be used in autonomous robotic-based projects, automated light systems, Smart Street lights, etc.

Here’s is How to interface an IR sensor with a PIC Microcontroller:

Components Needed:

  • PIC Microcontroller (e.g., PIC16F877A)
  • IR Sensor (e.g., TSOP1738)
  • Resistors
  • Capacitors
  • Breadboard and jumper wires
  • Power supply (5V)

Connection Steps:

1. Connect the IR Sensor:

  • VCC (Power): Connect the power pin of the IR sensor to +5V on the PIC microcontroller.
  • GND (Ground): Connect the ground pin of the IR sensor to the ground (GND) on the PIC microcontroller.
  • Signal Pin: Connect the signal pin of the IR sensor to any digital input pin on the PIC microcontroller.

2. Connect the PIC Microcontroller:

  • VCC and GND: Connect the VCC and GND pins of the PIC microcontroller to the +5V and GND rails on the breadboard.
  • Crystal Oscillator: Connect the crystal oscillator to the OSC1 and OSC2 pins of the PIC microcontroller for clock generation.
  • MCLR (Reset): Connect a pull-up resistor (typically 10kΩ) between the MCLR pin of the PIC microcontroller and +5V.
  • Programming Pins: Connect the programming pins (PGC and PGD) to the programmer if you plan to program the PIC using an external programmer.

3. Add Pull-up Resistor:

  • Connect a pull-up resistor (e.g., 10kΩ) between the signal pin of the IR sensor and +5V.

4. Connect a Capacitor (Optional):

  • Connect a capacitor (e.g., 10uF) between the VCC and GND pins of the IR sensor to stabilize the power supply.

Code Example : 

Here is an example code using MPLAB X IDE and XC8 compiler for a PIC16F877A microcontroller.

#include <xc.h>

#include <stdint.h>

// Configuration bits

#pragma config FOSC = HS        // High-Speed Crystal Oscillator

#pragma config WDTE = OFF       // Watchdog Timer Disabled

#pragma config PWRTE = OFF      // Power-up Timer Disabled

#pragma config BOREN = ON       // Brown-out Reset Enabled

#pragma config LVP = OFF        // Low-Voltage Programming Disabled

#pragma config CPD = OFF        // Data Memory Code Protection Disabled

#pragma config WRT = OFF        // Flash Write Protection Disabled

#pragma config CP = OFF         // Flash Program Memory Code Protection Disabled

// Function prototypes

void init(void);

void main(void) {

init();  // Initialize the system

while (1) {

  // Read IR sensor value

if (PORTBbits.RB0 == 0) {

  // Object detected

// Implement your action here

} else {

   // No object detected

}

}

return;

}

void init(void) {

 // Set the oscillator frequency to 8 MHz

OSCCON = 0b01110000;

// Set RB0 as input (assuming the sensor is connected to RB0)

TRISB0 = 1;

}

Thus, this is brief information on the IR sensor module interfacing with a microcontroller. An IR sensor is a type of electronic module mainly used for detecting an object near the IR sensor and emitting IR radiation. These sensors can also determine the heat being produced by an object & detect motion. Here is a question for you, what is the wavelength range of IR Sensor?