Bird
0
0
Arduinoprogramming~5 mins

IR sensor for obstacle detection in Arduino

Choose your learning style9 modes available
Introduction

An IR sensor helps a robot or device know if something is close by. It tells the device to stop or turn to avoid hitting things.

When building a robot that should not bump into walls.
To detect if a hand or object is near a device for touchless control.
In automatic doors to know when someone is approaching.
To count objects passing on a conveyor belt.
For simple security alarms that detect movement nearby.
Syntax
Arduino
const int irSensorPin = A0;  // IR sensor connected to analog pin A0
int sensorValue = 0;          // Variable to store sensor reading
int threshold = 300;          // Threshold for obstacle detection

void setup() {
  Serial.begin(9600);        // Start serial communication
  pinMode(irSensorPin, INPUT); // Set sensor pin as input
}

void loop() {
  sensorValue = analogRead(irSensorPin);  // Read sensor value
  if(sensorValue < threshold) {            // Check if obstacle is close
    Serial.println("Obstacle detected!");
  } else {
    Serial.println("No obstacle.");
  }
  delay(500);  // Wait half a second before next reading
}

The IR sensor usually outputs an analog voltage that changes with distance.

You set a threshold value to decide when an obstacle is close enough.

Examples
Basic example: detects obstacle if sensor value is less than 300.
Arduino
const int irSensorPin = A0;
int sensorValue = 0;
int threshold = 300;

void setup() {
  Serial.begin(9600);
  pinMode(irSensorPin, INPUT);
}

void loop() {
  sensorValue = analogRead(irSensorPin);
  if(sensorValue < threshold) {
    Serial.println("Obstacle detected!");
  } else {
    Serial.println("No obstacle.");
  }
  delay(500);
}
Lower threshold means sensor only detects very close obstacles.
Arduino
const int irSensorPin = A0;
int sensorValue = 0;
int threshold = 100;

void setup() {
  Serial.begin(9600);
  pinMode(irSensorPin, INPUT);
}

void loop() {
  sensorValue = analogRead(irSensorPin);
  if(sensorValue < threshold) {
    Serial.println("Very close obstacle detected!");
  } else {
    Serial.println("No close obstacle.");
  }
  delay(500);
}
Higher threshold detects obstacles farther away, with slower checking every 1 second.
Arduino
const int irSensorPin = A0;
int sensorValue = 0;
int threshold = 400;

void setup() {
  Serial.begin(9600);
  pinMode(irSensorPin, INPUT);
}

void loop() {
  sensorValue = analogRead(irSensorPin);
  if(sensorValue < threshold) {
    Serial.println("Obstacle detected!");
  } else {
    Serial.println("No obstacle.");
  }
  delay(1000);
}
Sample Program

This program reads the IR sensor value every half second. It prints the sensor value and tells if an obstacle is detected based on the threshold.

Arduino
const int irSensorPin = A0;  // IR sensor connected to analog pin A0
int sensorValue = 0;          // Variable to store sensor reading
int threshold = 300;          // Threshold for obstacle detection

void setup() {
  Serial.begin(9600);        // Start serial communication
  pinMode(irSensorPin, INPUT); // Set sensor pin as input
  Serial.println("Starting IR sensor obstacle detection...");
}

void loop() {
  sensorValue = analogRead(irSensorPin);  // Read sensor value
  Serial.print("Sensor value: ");
  Serial.println(sensorValue);

  if(sensorValue < threshold) {            // Check if obstacle is close
    Serial.println("Obstacle detected!");
  } else {
    Serial.println("No obstacle.");
  }
  delay(500);  // Wait half a second before next reading
}
OutputSuccess
Important Notes

Time complexity: Reading sensor and checking is very fast, almost constant time.

Space complexity: Uses only a few variables, very low memory.

Common mistake: Not setting the correct threshold can cause false detection or no detection.

Use this operation when you want simple obstacle detection. For more accuracy, combine with other sensors.

Summary

IR sensors detect obstacles by measuring reflected infrared light.

Set a threshold value to decide when an obstacle is close.

Use serial print to see sensor values and debug your detection.