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.
IR sensor for obstacle detection in 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.
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); }
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); }
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); }
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.
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 }
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.
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.
