Understanding how to use an Arduino IR sensor opens up a world of projects: obstacle-avoiding robots, TV remote decoders, security tripwires, and automated appliances. IR (infrared) sensors are inexpensive, easy to wire, and one of the most versatile components in any maker’s toolkit. In this tutorial we cover both types of IR sensors used with Arduino – the obstacle avoidance sensor and the IR receiver module – with complete wiring diagrams, real working code, and practical project examples.
Table of Contents
How IR Sensors Work
Infrared light is electromagnetic radiation just beyond the red end of the visible spectrum, invisible to the human eye but detectable by photodiodes and phototransistors. IR-based sensors exploit two phenomena:
IR reflection (used in obstacle sensors): An IR LED emits a beam. When an object is nearby, the beam reflects back and hits an IR photodiode. The sensor outputs a digital LOW when an obstacle is detected and HIGH when the path is clear. Detection range is typically 2-30cm and is adjustable via a potentiometer on the module.
IR modulation (used in remote receivers): TV remotes and similar devices send IR pulses modulated at 38kHz. The TSOP or VS1838-type receiver demodulates this signal and outputs the raw pulse timing that your Arduino reads as serial data packets. The data encodes button codes in formats like NEC, Sony SIRC, or RC5.
Types of IR Sensors for Arduino
| Feature | IR Obstacle Sensor | IR Receiver Module |
|---|---|---|
| Purpose | Proximity/obstacle detection | Remote control signal decoding |
| Output | Digital: HIGH/LOW | Timed pulse train (NEC/Sony/RC5) |
| Components | IR LED + photodiode pair | TSOP/VS1838 demodulator IC |
| Range | 2-30cm (adjustable) | Up to 10m line-of-sight |
| Typical use | Robot line sensing, obstacle avoidance | Home automation, remote-controlled devices |
| Library needed | None (digitalRead) | IRremote by Arduino-IRremote |
| Pins used | 1 digital input | 1 digital input (interrupt-capable preferred) |
Wiring the IR Obstacle Sensor
The IR obstacle avoidance sensor module has 3 pins: VCC, GND, and OUT. Wiring is straightforward:
- Sensor VCC to Arduino 5V
- Sensor GND to Arduino GND
- Sensor OUT to Arduino Digital Pin 2 (or any digital pin)
The module has two LEDs: one green LED indicates power-on, and one red LED lights up when an obstacle is detected within range. There is also a small blue potentiometer on the board. Turning it clockwise increases sensitivity (shorter detection distance); counterclockwise decreases sensitivity (longer detection distance). Adjust it for your specific surface and ambient lighting conditions.
Important notes about IR obstacle sensors outdoors: Sunlight contains strong infrared radiation that can saturate the photodiode and cause false detections. These sensors work best indoors or in shaded environments. For outdoor robots, ultrasonic sensors (like HC-SR04) are more reliable.
Obstacle Detection Code
Reading the IR obstacle sensor requires just digitalRead(). The sensor output is active LOW, meaning it goes to 0 when an obstacle is detected and stays at 1 when the path is clear.
// IR Obstacle Sensor - Basic Detection
const int IR_PIN = 2;
void setup() {
pinMode(IR_PIN, INPUT);
Serial.begin(9600);
Serial.println("IR Obstacle Sensor Ready");
}
void loop() {
int sensorValue = digitalRead(IR_PIN);
if (sensorValue == LOW) {
// LOW = obstacle detected (active low logic)
Serial.println("OBSTACLE DETECTED!");
} else {
Serial.println("Path clear");
}
delay(100);
}
For a robot with two IR sensors (left and right) for line following or dual-obstacle detection:
// Dual IR Sensor - Obstacle Avoidance Logic
const int LEFT_IR = 2;
const int RIGHT_IR = 3;
void setup() {
pinMode(LEFT_IR, INPUT);
pinMode(RIGHT_IR, INPUT);
Serial.begin(9600);
}
void loop() {
bool leftBlocked = (digitalRead(LEFT_IR) == LOW);
bool rightBlocked = (digitalRead(RIGHT_IR) == LOW);
if (!leftBlocked && !rightBlocked) {
Serial.println("Move forward");
// moveForward();
} else if (leftBlocked && !rightBlocked) {
Serial.println("Turn right (left blocked)");
// turnRight();
} else if (!leftBlocked && rightBlocked) {
Serial.println("Turn left (right blocked)");
// turnLeft();
} else {
Serial.println("Both blocked - reverse");
// moveBackward();
}
delay(50);
}
Wiring the IR Receiver Module
The IR receiver is used to decode signals from TV remotes and custom IR remote controllers. Common receiver ICs are TSOP1738, VS1838, HX1838, and IRM-3638T. All work the same way and use 3 pins.
For a standalone TSOP/VS1838 IC (not a breakout module):
- Pin 1 (OUT) to Arduino Digital Pin 11 (or Pin 2/3 for interrupt on Uno)
- Pin 2 (GND) to Arduino GND
- Pin 3 (VCC) to Arduino 5V
- Add a 100 ohm resistor in series on VCC and a 10uF capacitor between VCC and GND to filter power supply noise
For a breakout board module (HX1838 or similar):
- Module VCC to Arduino 5V
- Module GND to Arduino GND
- Module OUT/S to Arduino Digital Pin 11
Decoding Remote Signals with IRremote Library
The IRremote library (by Arduino-IRremote on GitHub) handles the timing and protocol decoding for you. Install it from the Arduino Library Manager: Sketch > Include Library > Manage Libraries > search “IRremote” > install the one by Arduino-IRremote (version 4.x recommended).
Step 1: Map your remote’s button codes
Every remote has unique codes for each button. Use this sketch to discover them:
#include <IRremote.hpp>
const int RECV_PIN = 11;
void setup() {
Serial.begin(9600);
IrReceiver.begin(RECV_PIN, ENABLE_LED_FEEDBACK);
Serial.println("Point remote at sensor and press buttons...");
}
void loop() {
if (IrReceiver.decode()) {
Serial.print("Protocol: ");
Serial.println(IrReceiver.decodedIRData.protocol);
Serial.print("Address: 0x");
Serial.println(IrReceiver.decodedIRData.address, HEX);
Serial.print("Command: 0x");
Serial.println(IrReceiver.decodedIRData.command, HEX);
Serial.println("---");
IrReceiver.resume(); // Ready for next signal
delay(200);
}
}
Open the Serial Monitor at 9600 baud, press each button on your remote, and note the hex command values. You will use these in your actual project code.
Step 2: Control actions with button codes
#include <IRremote.hpp>
const int RECV_PIN = 11;
const int LED_PIN = 13;
const int BUZZER = 8;
// Button codes from your remote (replace with your own)
#define BTN_1 0x45 // Button 1 command code
#define BTN_2 0x46 // Button 2 command code
#define BTN_POWER 0x40 // Power button command code
#define BTN_VOLUP 0x15 // Volume up
bool ledState = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER, OUTPUT);
Serial.begin(9600);
IrReceiver.begin(RECV_PIN, ENABLE_LED_FEEDBACK);
Serial.println("IR Remote Control Ready");
}
void loop() {
if (IrReceiver.decode()) {
uint8_t cmd = IrReceiver.decodedIRData.command;
Serial.print("Button: 0x");
Serial.println(cmd, HEX);
switch (cmd) {
case BTN_POWER:
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
Serial.println("LED toggled");
break;
case BTN_1:
tone(BUZZER, 440, 200); // Play 440Hz tone for 200ms
Serial.println("Buzzer beep");
break;
case BTN_VOLUP:
Serial.println("Volume up pressed");
// Add your logic here
break;
}
IrReceiver.resume();
}
}
Practical Project Examples
Here are three practical projects you can build with IR sensors that are popular among Indian Arduino hobbyists and students:
Project 1: IR Obstacle-Avoiding Robot
Mount two IR obstacle sensors on the front-left and front-right of a wheeled robot chassis. Connect motors through an L298N or L293D motor driver. Read both sensors in the loop and steer away from whichever side detects an obstacle first. This is a classic first robotics project that can be built for under Rs. 500 in parts.
Project 2: IR Remote-Controlled LED Matrix
Use an 8×8 LED matrix with a MAX7219 driver and your IR receiver. Map remote buttons to display patterns, letters, or animations. Button 1 shows a smiley, button 2 shows an arrow, button 3 shows a number. This project teaches remote protocol decoding and SPI display control simultaneously.
Project 3: Smart Home TV Remote Relay Control
Use an IR receiver and a relay module to control a fan or light with any existing TV remote. Map specific unused buttons on your TV remote to toggle relays connected to appliances. This is the simplest form of home automation that does not require Wi-Fi or a smartphone, making it reliable even during network outages.
Combine with the obstacle sensor for added automation: automatically turn off a room fan when IR detects no one is present for 5 minutes (using a PIR sensor is better for this, but IR can work in narrow corridors).
Frequently Asked Questions
Q: What is the difference between an IR obstacle sensor and a PIR motion sensor?
An IR obstacle sensor actively emits IR light and detects its reflection, working at short range (up to 30cm). It detects any object regardless of temperature. A PIR (passive infrared) sensor passively detects changes in IR radiation from warm bodies (humans, animals) and works at longer ranges (up to 7m). Use obstacle sensors for robots and short-range detection; use PIR for room occupancy and security applications.
Q: My IR receiver does not decode any signals. What should I check?
Check in this order: (1) confirm the OUT pin goes to a digital pin that supports interrupts (pins 2 or 3 on Uno), (2) verify IRremote library version – version 4.x has a different API than version 2.x, (3) ensure you call IrReceiver.resume() after each decode, and (4) check for power supply noise by adding a 10uF capacitor between VCC and GND close to the sensor.
Q: Can I use an IR obstacle sensor as a line follower?
Yes, with caveats. IR obstacle sensors can detect dark lines on light surfaces by detecting the difference in IR reflectivity. However, dedicated line-following sensors (like TCRT5000) are better calibrated for this purpose and give more consistent results. The adjustable threshold on standard obstacle sensors makes them usable but less precise than purpose-built line sensors.
Q: How do I send IR signals from Arduino to control a TV?
Use the IRremote library’s send functions: IrSender.sendNEC(address, command, repeats). First discover the address and command codes of your TV by pointing a remote at an IR receiver and logging them. Then use IrSender with an IR LED (with a 100 ohm series resistor) connected to the send pin (default pin 3 on Uno). This lets Arduino act as a remote control for any IR-controlled device.
Ready to Build Your Next Arduino Project?
Shop sensors, modules, and components at Zbotic.in – India’s trusted electronics store with fast shipping across India.
Add comment