Overview
Infrared (IR) sensors allow your Vumbua Synapse Kit to see the invisible. Operating on digital pin D2, these sensors detect infrared light reflected from objects, enabling object detection, distance measurement, and remote control capabilities.
Unlike ultrasonic sensors that use sound waves, IR sensors use light waves just beyond the visible spectrum. This makes them perfect for detecting objects, following lines, and receiving commands from remote controls.
Infrared light has wavelengths longer than visible light but shorter than microwaves. Humans can't see it, but electronic devices like your IR sensor can detect it perfectly!
Prerequisites
Before starting, make sure you have completed:
Completed Lessons
- Lesson 1-2: Digital I/O Basics
- Lesson 3: Buzzer Output
- Lesson 4: Analog Input (Potentiometer)
- Lesson 9: Servo Motor Control
- Lesson 10: Ultrasonic Sensor
- Understanding of Serial Monitor
Hardware Requirements
- Vumbua Synapse Kit with IR sensor
- Various test objects (white/black paper)
- IR remote control (optional)
- USB Type-C cable
- Computer with Arduino IDE
The IR sensor is pre-connected to the Vumbua board on Digital Pin D2. You can immediately start detecting objects without any wiring!
Learning Objectives
By the end of this lesson, you will be able to:
- Understand infrared light and IR sensor operation
- Connect and read digital IR sensor on pin D2
- Implement basic object detection algorithms
- Create a line-following robot logic
- Work with IR remote controls
- Differentiate between reflective and transmissive modes
- Calibrate IR sensors for different surfaces
- Build practical applications with IR sensing
- Troubleshoot common IR sensor issues
What is Infrared Light?
Beyond the Visible Spectrum
Infrared (IR) is electromagnetic radiation with wavelengths between 700 nanometers and 1 millimeter. It lies just beyond the red end of the visible light spectrum.
- Near IR: 700-1400nm (used in sensors)
- Mid IR: 1400-3000nm (thermal imaging)
- Far IR: 3000nm-1mm (heat radiation)
IR in Daily Life
Infrared technology is everywhere around us:
- TV Remote Controls: Send IR signals to devices
- Security Systems: Motion detection
- Thermal Cameras: See heat signatures
- Communication: IrDA data transfer
- Medical: Pulse oximeters, thermometers
- Automotive: Night vision systems
IR Sensor Types
Common IR sensor configurations:
- Reflective Sensors: Emit and detect reflection
- Transmissive Sensors: Object breaks IR beam
- IR Receivers: Detect modulated IR signals
- Thermal Sensors: Detect IR radiation from objects
IR Sensor Connections on Vumbua
The IR sensor module on Vumbua Synapse Kit has 3 pins:
The IR sensor on Vumbua is a reflective type with:
- Detection Distance: 2-30cm (adjustable with potentiometer)
- Output Type: Digital (HIGH/LOW)
- Response Time: <10ms
- Wavelength: 940nm (near infrared)
- Detection Angle: 35°
Adjustable Potentiometer
Your IR sensor module has a small blue potentiometer. Adjust it to:
- Clockwise: Increases sensitivity (detects farther)
- Counter-clockwise: Decreases sensitivity
- Calibration: Adjust until LED indicator just turns ON at desired distance
How IR Sensors Work
Working Principle
The IR sensor module contains two main components:
- IR LED: Emits infrared light continuously
- Photodiode/Phototransistor: Detects reflected IR light
- Comparator Circuit: Compares received signal to threshold
- Digital Output: Provides HIGH/LOW signal to Arduino
Output Logic
The sensor output works as follows:
| Condition | IR Detected | Output (D2) | Module LED |
|---|---|---|---|
| No object detected | No reflection | HIGH (1) | OFF |
| Object detected | Reflection present | LOW (0) | ON |
Note: This is active-LOW logic. The sensor outputs LOW when it detects an object.
Detection Distance
Basic Object Detection
Let's start with a simple program to detect objects with the IR sensor:
ir_basic_detection.ino
/* Vumbua Synapse Kit - Lesson 11: Basic IR Object Detection IR sensor connected to digital pin D2 Detects objects and indicates with LED and Serial Monitor */ const int irSensorPin = 2; // IR sensor on digital pin D2 const int ledPin = 13; // Built-in LED on pin 13 const int indicatorLed = 9; // External LED on pin 9 const int buzzerPin = 3; // Buzzer on pin 3 // Variables bool objectDetected = false; unsigned long lastDetectionTime = 0; const unsigned long detectionCooldown = 1000; // 1 second void setup() { // Initialize pins pinMode(irSensorPin, INPUT); pinMode(ledPin, OUTPUT); pinMode(indicatorLed, OUTPUT); pinMode(buzzerPin, OUTPUT); // Start Serial communication Serial.begin(9600); Serial.println("IR Object Detection System - Vumbua Kit"); Serial.println("======================================="); Serial.println("Place an object in front of IR sensor to detect"); Serial.println(); } void loop() { // Read IR sensor (LOW means object detected) int irValue = digitalRead(irSensorPin); // Check if object is detected if (irValue == LOW) { // Cooldown check to avoid continuous triggering if (millis() - lastDetectionTime > detectionCooldown) { objectDetected = true; lastDetectionTime = millis(); // Visual and audio indicators digitalWrite(ledPin, HIGH); digitalWrite(indicatorLed, HIGH); tone(buzzerPin, 1000, 200); // Beep for 200ms // Serial output Serial.println("[OBJECT DETECTED] - Time: " + String(millis() / 1000.0) + "s"); } } else { if (objectDetected) { // Object moved away objectDetected = false; digitalWrite(ledPin, LOW); digitalWrite(indicatorLed, LOW); Serial.println("[OBJECT REMOVED]"); } } // Continuous status display static unsigned long lastStatusTime = 0; if (millis() - lastStatusTime > 500) { lastStatusTime = millis(); Serial.print("Status: "); Serial.print(irValue == LOW ? "OBJECT PRESENT" : "CLEAR"); Serial.print(" | Sensor Value: "); Serial.print(irValue); Serial.print(" | Time: "); Serial.println(String(millis() / 1000.0) + "s"); } // Small delay for stability delay(50); }
Testing the Program
1. Upload the Code
Upload to your Vumbua Synapse Kit. The IR sensor is pre-connected to digital pin D2.
2. Open Serial Monitor
Open Tools → Serial Monitor (Ctrl+Shift+M) with baud rate 9600.
3. Test Object Detection
Place an object (like your hand or a book) in front of the IR sensor:
- Built-in LED (pin 13): Should light up
- External LED (pin 9): Should light up
- Buzzer: Should beep once
- Serial Monitor: Should show "OBJECT DETECTED"
4. Calibrate Sensor
Use the small potentiometer on the IR module to adjust sensitivity:
- Turn clockwise to increase detection distance
- Turn counter-clockwise to decrease distance
- Adjust until LED just turns ON at desired distance
IR sensors can be affected by strong ambient light (sunlight, fluorescent lights). The Vumbua IR sensor includes ambient light rejection, but for best results:
- Avoid direct sunlight on the sensor
- Test in consistent lighting conditions
- Use dark or non-reflective test objects initially
- Adjust potentiometer to compensate for lighting
Distance Detection with IR
While IR sensors primarily provide digital output, we can estimate distance by varying sensitivity:
ir_distance_estimation.ino
/* Vumbua Synapse Kit - IR Distance Estimation Estimate object distance using IR sensor sensitivity */ const int irSensorPin = 2; const int ledPin = 9; const int buzzerPin = 3; void setup() { pinMode(irSensorPin, INPUT); pinMode(ledPin, OUTPUT); pinMode(buzzerPin, OUTPUT); Serial.begin(9600); Serial.println("IR Distance Estimation"); Serial.println("Move object closer/farther to see distance estimation"); } void loop() { int irValue = digitalRead(irSensorPin); if (irValue == LOW) { // Object detected - estimate proximity int detectionCount = 0; // Count how many times sensor detects in 100ms for (int i = 0; i < 10; i++) { if (digitalRead(irSensorPin) == LOW) { detectionCount++; } delay(10); } // Estimate distance based on detection consistency float distanceEstimate; int brightness; if (detectionCount >= 9) { // Very close (< 5cm) distanceEstimate = 3.0; brightness = 255; tone(buzzerPin, 1500, 100); } else if (detectionCount >= 7) { // Close (5-10cm) distanceEstimate = 7.5; brightness = 200; tone(buzzerPin, 1000, 100); } else if (detectionCount >= 5) { // Medium (10-15cm) distanceEstimate = 12.5; brightness = 150; tone(buzzerPin, 800, 100); } else if (detectionCount >= 3) { // Far (15-20cm) distanceEstimate = 17.5; brightness = 100; tone(buzzerPin, 600, 100); } else { // Very far (>20cm) or intermittent detection distanceEstimate = 25.0; brightness = 50; tone(buzzerPin, 400, 50); } // Control LED brightness based on distance analogWrite(ledPin, brightness); // Display results Serial.print("[DETECTED] Distance: ~"); Serial.print(distanceEstimate); Serial.print("cm | Detection Score: "); Serial.print(detectionCount); Serial.print("/10 | LED: "); Serial.print(brightness); Serial.println("/255"); } else { // No object detected analogWrite(ledPin, 0); noTone(buzzerPin); static unsigned long lastClearTime = 0; if (millis() - lastClearTime > 2000) { lastClearTime = millis(); Serial.println("[CLEAR] No objects detected"); } } delay(100); }
Object
Reflective vs Transmissive IR Sensors
Understanding the two main operating modes of IR sensors:
Reflective Mode
(Used on Vumbua Kit)
- IR emitter and detector on same side
- Detects reflections from objects
- Best for proximity sensing
- Affected by object color and reflectivity
- Typical range: 2-30cm
Transmissive Mode
(Break-beam sensors)
- Emitter and detector face each other
- Object breaks the IR beam
- Best for counting objects
- Unaffected by object color
- Can work at longer distances
Test different materials to see how reflectivity affects detection:
| Material | Reflectivity | Detection Distance | Notes |
|---|---|---|---|
| White paper | High | Maximum | Best for testing |
| Black paper | Very Low | Minimum | Hard to detect |
| Aluminum foil | Extremely High | Beyond normal | May cause false readings |
| Wood | Medium | Average | Typical object |
| Glass | Variable | Inconsistent | IR may pass through |
Line Follower Robot Implementation
Create the logic for a line-following robot using IR sensors:
line_follower_simulation.ino
/* Vumbua Synapse Kit - Line Follower Robot Simulation Simulates a 3-sensor line follower using single IR sensor Demonstrates logic for line following robots */ const int irSensorPin = 2; const int leftMotor = 5; // Simulated left motor control const int rightMotor = 6; // Simulated right motor control const int buzzerPin = 3; // Line follower states enum RobotState { ON_LINE, LEFT_OF_LINE, RIGHT_OF_LINE, LOST_LINE }; RobotState currentState = ON_LINE; int lineDetectionCount = 0; void setup() { pinMode(irSensorPin, INPUT); pinMode(leftMotor, OUTPUT); pinMode(rightMotor, OUTPUT); pinMode(buzzerPin, OUTPUT); Serial.begin(9600); Serial.println("Line Follower Robot Simulation"); Serial.println("Place black line under sensor to simulate line following"); } void loop() { int irValue = digitalRead(irSensorPin); // Simulate 3-sensor array with timing static unsigned long lastScan = 0; static int scanPosition = 0; // 0=left, 1=center, 2=right if (millis() - lastScan > 100) { lastScan = millis(); // Simulate scanning across 3 positions bool lineDetected = (irValue == HIGH); // Black line = HIGH (no reflection) // Update state based on simulated sensor readings if (scanPosition == 1) { // Center sensor if (lineDetected) { currentState = ON_LINE; lineDetectionCount++; } else { // Check left/right based on previous scans if (random(0, 2) == 0) { currentState = LEFT_OF_LINE; } else { currentState = RIGHT_OF_LINE; } } } scanPosition = (scanPosition + 1) % 3; } // Execute motor control based on state controlMotors(); // Display status static unsigned long lastDisplay = 0; if (millis() - lastDisplay > 500) { lastDisplay = millis(); Serial.print("State: "); switch (currentState) { case ON_LINE: Serial.print("ON_LINE"); break; case LEFT_OF_LINE: Serial.print("LEFT_OF_LINE"); break; case RIGHT_OF_LINE: Serial.print("RIGHT_OF_LINE"); break; case LOST_LINE: Serial.print("LOST_LINE"); break; } Serial.print(" | Line Detections: "); Serial.print(lineDetectionCount); Serial.print(" | Sensor: "); Serial.println(irValue == HIGH ? "BLACK" : "WHITE"); } delay(50); } void controlMotors() { switch (currentState) { case ON_LINE: // Both motors forward analogWrite(leftMotor, 200); analogWrite(rightMotor, 200); noTone(buzzerPin); break; case LEFT_OF_LINE: // Turn right analogWrite(leftMotor, 150); analogWrite(rightMotor, 50); tone(buzzerPin, 800, 100); break; case RIGHT_OF_LINE: // Turn left analogWrite(leftMotor, 50); analogWrite(rightMotor, 150); tone(buzzerPin, 600, 100); break; case LOST_LINE: // Stop and search analogWrite(leftMotor, 0); analogWrite(rightMotor, 0); tone(buzzerPin, 300, 500); break; } }
Line followers work by detecting contrast between the line and background:
- Black line on white surface: IR reflects from white, not from black
- White line on black surface: IR reflects from white line
- Sensor array: Multiple sensors (3-5) to determine line position
- PID Control: Advanced line followers use PID algorithm for smooth following
IR Remote Control System
Learn to use IR remote controls with your Vumbua Kit:
ir_remote_control.ino
/* Vumbua Synapse Kit - IR Remote Control Note: This requires IR receiver module connected to D2 For demonstration, we'll simulate remote commands */ #include <IRremote.h> // Install IRremote library from Library Manager const int RECV_PIN = 2; const int ledPin = 9; const int buzzerPin = 3; IRrecv irrecv(RECV_PIN); decode_results results; // Common IR remote codes (NEC protocol) #define BTN_POWER 0xFFA25D #define BTN_VOL_UP 0xFF629D #define BTN_VOL_DN 0xFFA857 #define BTN_PLAY 0xFFC23D #define BTN_UP 0xFF18E7 #define BTN_DOWN 0xFF4AB5 #define BTN_LEFT 0xFF10EF #define BTN_RIGHT 0xFF5AA5 #define BTN_OK 0xFF38C7 int ledBrightness = 0; bool powerOn = false; void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); pinMode(buzzerPin, OUTPUT); irrecv.enableIRIn(); Serial.println("IR Remote Control System Ready"); Serial.println("Point remote at sensor and press buttons"); } void loop() { if (irrecv.decode(&results)) { handleIRCommand(results.value); irrecv.resume(); } delay(100); } void handleIRCommand(unsigned long command) { // Acknowledge command with beep tone(buzzerPin, 1000, 50); Serial.print("IR Command Received: 0x"); Serial.println(command, HEX); switch (command) { case BTN_POWER: powerOn = !powerOn; if (powerOn) { ledBrightness = 100; Serial.println("Action: SYSTEM POWER ON"); } else { ledBrightness = 0; Serial.println("Action: SYSTEM POWER OFF"); } break; case BTN_VOL_UP: if (powerOn) { ledBrightness = min(255, ledBrightness + 25); Serial.println("Action: VOLUME UP - Brightness: " + String(ledBrightness)); } break; case BTN_VOL_DN: if (powerOn) { ledBrightness = max(0, ledBrightness - 25); Serial.println("Action: VOLUME DOWN - Brightness: " + String(ledBrightness)); } break; case BTN_PLAY: tone(buzzerPin, 523, 200); // C5 delay(200); tone(buzzerPin, 659, 200); // E5 delay(200); tone(buzzerPin, 784, 200); // G5 Serial.println("Action: PLAY SOUND SEQUENCE"); break; case BTN_UP: Serial.println("Action: NAVIGATE UP"); break; case BTN_DOWN: Serial.println("Action: NAVIGATE DOWN"); break; case BTN_OK: Serial.println("Action: OK/SELECT"); for (int i = 0; i < 3; i++) { digitalWrite(ledPin, HIGH); delay(100); digitalWrite(ledPin, LOW); delay(100); } break; default: Serial.println("Action: UNKNOWN COMMAND"); tone(buzzerPin, 200, 300); } // Update LED brightness analogWrite(ledPin, ledBrightness); }
Remote Control Layout
Practical Applications
Robotics
- Obstacle avoidance
- Line following robots
- Wall following robots
- Sumo robots
- Maze solvers
Home Automation
- Automatic door opening
- Light control (motion sensing)
- Security systems
- Visitor counters
- Garage door sensors
Industrial
- Object counting
- Quality control
- Conveyor belt systems
- Level detection
- Safety curtains
Project: Smart Trash Bin
/*
Smart Trash Bin using IR Sensor
Opens lid when hand is detected, closes after delay
*/
const int irSensorPin = 2;
const int servoPin = 9;
const int ledPin = 13;
#include <Servo.h>
Servo lidServo;
bool lidOpen = false;
unsigned long lidOpenTime = 0;
const unsigned long LID_CLOSE_DELAY = 3000; // 3 seconds
void setup() {
pinMode(irSensorPin, INPUT);
pinMode(ledPin, OUTPUT);
lidServo.attach(servoPin);
lidServo.write(0); // Closed position
Serial.begin(9600);
}
void loop() {
int irValue = digitalRead(irSensorPin);
if (irValue == LOW && !lidOpen) {
// Hand detected, open lid
openLid();
}
if (lidOpen && millis() - lidOpenTime > LID_CLOSE_DELAY) {
// Close lid after delay
closeLid();
}
delay(100);
}
void openLid() {
lidServo.write(90); // Open position
digitalWrite(ledPin, HIGH);
lidOpen = true;
lidOpenTime = millis();
Serial.println("Lid opened");
}
void closeLid() {
lidServo.write(0); // Closed position
digitalWrite(ledPin, LOW);
lidOpen = false;
Serial.println("Lid closed");
}
Troubleshooting Common Issues
- Check: Adjust potentiometer counter-clockwise
- Test: Different objects (dark vs light)
- Verify: Ambient light isn't causing false triggers
- Solution: Add ambient light shield around sensor
- Check: Adjust potentiometer clockwise
- Test: Use highly reflective object (white paper)
- Verify: Correct wiring to D2 pin
- Measure: 5V and GND connections with multimeter
- Cause: Object at edge of detection range
- Solution: Adjust sensitivity potentiometer
- Add: Software debouncing in code
- Try: Different object materials/colors
- Avoid: Direct sunlight on sensor
- Use: Physical shield or enclosure
- Try: 940nm IR sensors (less affected by sunlight)
- Consider: Using ultrasonic sensor for outdoor applications
Software Debouncing Example
// Improved IR reading with debouncing
const int irPin = 2;
const int DEBOUNCE_DELAY = 50; // milliseconds
int lastSteadyState = HIGH;
int lastFlickerableState = HIGH;
int currentState;
unsigned long lastDebounceTime = 0;
void setup() {
pinMode(irPin, INPUT);
Serial.begin(9600);
}
void loop() {
int reading = digitalRead(irPin);
// If the reading changed (due to noise or actual change)
if (reading != lastFlickerableState) {
lastDebounceTime = millis();
lastFlickerableState = reading;
}
// If enough time has passed, consider the reading stable
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
if (reading != currentState) {
currentState = reading;
// Only act if the state changed
if (currentState == LOW) {
Serial.println("Object reliably detected!");
} else {
Serial.println("Object reliably removed!");
}
}
}
lastSteadyState = currentState;
delay(10);
}
Video Demonstration
IR Sensor Tutorial
Watch our detailed video tutorial on IR sensors and their applications.
Learn about object detection, line following, and remote control systems.
Watch on YouTubeWhat's Next?
Congratulations! You've successfully mastered infrared sensing with your Vumbua Synapse Kit. Here's what you've accomplished:
- ✓ Understood infrared light and sensor operation
- ✓ Implemented basic object detection on pin D2
- ✓ Created distance estimation algorithms
- ✓ Built line follower robot logic
- ✓ Worked with IR remote controls
- ✓ Learned reflective vs transmissive modes
- ✓ Developed practical applications
- ✓ Mastered troubleshooting techniques
In the final lesson, you'll explore 3-axis accelerometers:
Lesson 12: 3-Axis Accelerometer
Measure acceleration, tilt, and orientation with the accelerometer. Create motion-controlled projects, tilt sensors, activity monitors, and gesture recognition systems that respond to movement in 3D space.
Resources & Downloads
Download Complete Code
Get all the code files for this lesson: