How to Use the IR Sensor Module with Arduino: Beginner’s Guide
Introduction
This guide will introduce you to how IR sensor modules function and how to integrate them with an Arduino. By the end of this article, you’ll know how to set up and program an IR sensor module to enhance your projects with reliable object detection.
What is an IR Sensor Module?
An Infrared (IR) sensor module is a device that detects infrared radiation from objects in its field of view. These sensors are widely used for detecting motion, measuring distances, and identifying the presence of objects without physical contact. The module typically consists of an IR transmitter (usually an LED) and an IR receiver (often a photodiode or phototransistor).
Use cases of IR Sensor Module
The IR sensor module has several practical applications across different fields. In robotics, it is commonly used for obstacle detection, helping robots avoid collisions by identifying nearby objects. It’s also vital in line-following robots, where the sensor detects lines on surfaces to guide movement. The module serves as a proximity sensor, detecting objects or people within a short range, making it useful for automated doors that open when someone approaches. Additionally, it’s frequently employed in consumer electronics, such as TV remote controls, to detect signals for seamless user interaction.
Pinout of IR Sensor Module
IR sensor modules generally have three pins:
- VCC: Connects to a 5V power supply.
- GND: Ground connection.
- OUT: Digital output pin that goes HIGH or LOW based on the sensor’s detection status.
How It Works
The IR transmitter emits infrared light, which reflects off an object and is captured by the IR receiver. Based on the intensity and angle of the reflected light, the sensor module can determine the presence and proximity of the object. If an object is detected, the module sends a signal through its output pin.
What You’ll Need :
- Arduino Uno
- IR sensor module
- Jumper wires
- Breadboard
- LED
Circuit Diagram
- Connect the VCC pin to the 5V pin of the Arduino.
- Connect the GND pin to the ground of the Arduino.
- Connect the OUT pin to a digital pin on the Arduino ( D7).
Code
1. // IR Sensor Module code with Arduino
2.
3. int irPin = 7; // IR sensor output pin
4. int ledPin = 13; // Built-in LED for indication
5.
6. void setup() {
7. pinMode(irPin, INPUT); // Set IR sensor pin as input
8. pinMode(ledPin, OUTPUT); // Set LED pin as output
9. Serial.begin(9600); // Initialize serial communication
10. }
11.
12. void loop() {
13. int irValue = digitalRead(irPin); // Read IR sensor value
14.
15. if (irValue == HIGH) {
16. // If object is detected
17. digitalWrite(ledPin, HIGH); // Turn on LED
18. Serial.println("Object detected");
19. } else {
20. // If no object is detected
21. digitalWrite(ledPin, LOW); // Turn off LED
22. Serial.println("No object detected");
23. }
24.
25. delay(100); // Small delay for readability
26. }
This Arduino code is designed to use an IR sensor module to detect objects by reading its output signal. The irPin is assigned to pin 7 on the Arduino, where the sensor’s output pin is connected. The ledPin is assigned to pin 13, which controls the built-in LED on the Arduino. This LED shows whether an object has been detected by the sensor.
In the setup() function, the pinMode() commands configure the pins. The irPin is set as an input, meaning the Arduino will read values from the IR sensor, while the ledPin is set as an output so the Arduino can turn the LED on or off.
The Serial.begin(9600); command initializes serial communication, allowing the Arduino to send messages to the serial monitor, which helps monitor sensor activity in real-time.
Within the loop() function, the code continuously checks the sensor’s state. The IR sensor value is read using digitalRead(irPin);. If the value is HIGH (meaning an object is detected), the LED turns on, and “Object detected” is displayed on the serial monitor. If the value is LOW (no object detected), the LED turns off, and “No object detected” is displayed. A minor delay of 100 milliseconds ensures that the readings are spaced out for clarity. This setup provides visual and serial feedback on the presence of objects near the sensor.
Conclusion
The IR sensor module is a versatile and easy-to-use component in many electronics projects. It’s ideal for beginner projects like obstacle-avoiding robots or simple proximity sensors. In future projects, you can combine multiple IR sensors for complex systems like line-following robots or use them in home automation for touchless object detection.