Interfacing IR Sensor Module With Arduino

by Rachana Jain in Circuits > Arduino

102 Views, 0 Favorites, 0 Comments

Interfacing IR Sensor Module With Arduino

pic 2.jpg

Interfacing an IR sensor module with an Arduino is useful project for anyone interested in robotics, automation, or electronics. Infrared (IR) sensors are commonly used to detect objects, measure distances, and even in communication systems. This project focuses on using an IR sensor module to detect obstacles and respond accordingly. By following this guide, you will learn how to connect the IR sensor to an Arduino, understand its working principles, and utilize its readings for practical applications. This

Supplies

  • Arduino Uno
  • IR Sensor Module
  • Breadboard
  • Jumper wires
  • USB cable (to connect Arduino to the computer)
  • LED (optional, for visual indication)
  • Resistor (220 ohms, if using an LED)

Working of an IR Sensor

Untitled design (5).jpg

An Infrared (IR) sensor is an electronic device that measures and detects infrared radiation in its surrounding environment. The IR sensor module works based on the reflection of infrared light. When an object comes into the proximity of the IR sensor, the emitted infrared light hits the object and reflects back toward the sensor. The nature and intensity of the reflection depend on the object's distance, shape, and surface characteristics.

The reflected infrared light is detected by the photodiode. The photodiode converts the received infrared light into an electrical current. The amount of current generated by the photodiode is proportional to the intensity of the received infrared light. The weak electrical signal generated by the photodiode is then fed into an operational amplifier. The op-amp amplifies the signal to a higher voltage level suitable for digital processing.

The amplified signal is processed to generate a digital output. In most IR sensor modules, this output is either HIGH (no object detected) or LOW (object detected). This digital output can be directly interfaced with the Arduino, for further processing.

The microcontroller reads the digital output from the IR sensor module. Based on the state of this output, the microcontroller can take appropriate actions. For example, if the output is LOW, indicating the presence of an object, the microcontroller might turn on an LED, activate a buzzer, or send a signal to other components of the system.

IR Sensor Pinout

IR Sensor Module Pinout.jpg

An IR sensor module typically has three pins: VCC, GND, and OUT. Here’s a detailed explanation of each pin:

VCC (Power Supply Pin): This pin is used to provide power to the IR sensor module. It is connected to the positive terminal of the power supply, typically 5V.

GND (Ground Pin): This pin is connected to the ground (negative terminal) of the power supply. It completes the electrical circuit by providing a common reference point for the sensor module. Connecting this pin to the ground of the Arduino or any other microcontroller ensures a common ground, which is essential for the proper functioning of the sensor.

OUT (Output Pin): This pin provides the digital output signal from the IR sensor module. The output signal indicates whether an object is detected or not.

Interfacing IR Sensor Module With Arduino

Interfacing IR Sensor Module with Arduino Uno.jpg

To interface the IR sensor module with an Arduino, follow these steps:

  • Connect the VCC pin of the IR sensor module to the 5V pin on the Arduino.
  • Connect the GND pin of the IR sensor module to one of the GND pins on the Arduino.
  • Connect the OUT pin of the IR sensor module to a digital input pin on the Arduino, for example, digital pin 7.

Code

// Define the pin connections
const int irSensorPin = 7;  // IR sensor output pin connected to digital pin 7
const int ledPin = 13;      // LED connected to digital pin 13 (optional)

void setup() {
  pinMode(irSensorPin, INPUT);  // Set IR sensor pin as input
  pinMode(ledPin, OUTPUT);      // Set LED pin as output (optional)
  Serial.begin(9600);           // Begin serial communication for debugging
}

void loop() {
  int sensorValue = digitalRead(irSensorPin);  // Read the value from the IR sensor

  if (sensorValue == LOW) {
    // Obstacle detected
    digitalWrite(ledPin, HIGH);  // Turn on LED (optional)
    Serial.println("Obstacle detected!");
  } else {
    // No obstacle
    digitalWrite(ledPin, LOW);   // Turn off LED (optional)
    Serial.println("No obstacle.");
  }

  delay(100);  // Small delay for stability
}

Code Explanation

Defining Pin Connections

  • const int irSensorPin = 7; - This line defines a constant integer variable irSensorPin and assigns it the value 7. This indicates that the IR sensor's output pin is connected to digital pin 7 on the Arduino.
  • const int ledPin = 13; - Similarly, this line defines a constant integer variable ledPin and assigns it the value 13. This indicates that an optional LED is connected to digital pin 13 on the Arduino.
// Define the pin connections
const int irSensorPin = 7;  // IR sensor output pin connected to digital pin 7
const int ledPin = 13;      // LED connected to digital pin 13 (optional)


Setup Function

The setup() function runs once when you press reset or power the Arduino board. It is used to initialize variables, pin modes, start using libraries, etc.

  • pinMode(irSensorPin, INPUT); - This configures the pin connected to the IR sensor (irSensorPin, which is pin 7) as an input. This means the Arduino will read the signal coming from the IR sensor on this pin.
  • pinMode(ledPin, OUTPUT); - This configures the pin connected to the LED (ledPin, which is pin 13) as an output. This allows the Arduino to send a signal to turn the LED on or off.
  • Serial.begin(9600); - This starts the serial communication at a baud rate of 9600 bits per second. Serial communication allows the Arduino to send data back to the computer for debugging purposes.
void setup() {
 pinMode(irSensorPin, INPUT); // Set IR sensor pin as input
 pinMode(ledPin, OUTPUT);   // Set LED pin as output (optional)
 Serial.begin(9600);      // Begin serial communication for debugging
}


Loop Function

The loop() function runs continuously in a loop. This is where the main logic of your program is executed repeatedly.

  • int sensorValue = digitalRead(irSensorPin); - This reads the current state of the IR sensor's output pin (irSensorPin). The digitalRead() function returns HIGH (1) if the pin is receiving a high voltage (no obstacle detected) and LOW (0) if it is receiving a low voltage (obstacle detected). The result is stored in the sensorValue variable.
  • if (sensorValue == LOW) { - This condition checks if the sensorValue is LOW, which means an obstacle is detected.
  • digitalWrite(ledPin, HIGH); - If an obstacle is detected, this line sets the ledPin (pin 13) to HIGH, turning on the LED.
  • Serial.println("Obstacle detected!"); - This line sends the message "Obstacle detected!" to the Serial Monitor for debugging purposes.
  • } else { - This block executes if the sensorValue is not LOW (i.e., it is HIGH), meaning no obstacle is detected.
  • digitalWrite(ledPin, LOW); - If no obstacle is detected, this line sets the ledPin to LOW, turning off the LED.
  • Serial.println("No obstacle."); - This line sends the message "No obstacle." to the Serial Monitor for debugging purposes.
  • delay(100); - This adds a small delay of 100 milliseconds at the end of each loop iteration. This helps stabilize the sensor readings and reduces the load on the processor by not constantly polling the sensor at maximum speed.
void loop() {
 int sensorValue = digitalRead(irSensorPin); // Read the value from the IR sensor

 if (sensorValue == LOW) {
  // Obstacle detected
  digitalWrite(ledPin, HIGH); // Turn on LED (optional)
  Serial.println("Obstacle detected!");
 } else {
  // No obstacle
  digitalWrite(ledPin, LOW);  // Turn off LED (optional)
  Serial.println("No obstacle.");
 }
 delay(100); // Small delay for stability
}

Image Source: Play with Circuit