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.
Practice
Solution
Step 1: Understand IR sensor function
IR sensors emit infrared light and detect the light reflected back from objects.Step 2: Identify what is detected
The sensor measures reflected infrared light, not visible light, sound, or magnetic fields.Final Answer:
Reflected infrared light from nearby objects -> Option DQuick Check:
IR sensor = reflected infrared light [OK]
- Confusing IR sensor with light or sound sensors
- Thinking it detects visible light
- Assuming it detects magnetic fields
Solution
Step 1: Identify correct function for analog reading
analogRead(pin) reads analog sensor values from specified pin.Step 2: Check pin and function usage
Pin A0 is analog input; analogRead(A0) is correct syntax.Final Answer:
int sensorValue = analogRead(A0); -> Option AQuick Check:
Use analogRead for analog sensors [OK]
- Using digitalRead() for analog sensors
- Using analogWrite() which is for output
- Using digitalWrite() which is for digital output
int sensorValue = 350;
int threshold = 300;
if(sensorValue > threshold) {
Serial.println("No obstacle");
} else {
Serial.println("Obstacle detected");
}Solution
Step 1: Compare sensorValue with threshold
350 is greater than 300, so condition sensorValue > threshold is true.Step 2: Determine printed output
Since condition is true, code prints "No obstacle".Final Answer:
No obstacle -> Option BQuick Check:
350 > 300 = true, prints "No obstacle" [OK]
- Confusing greater than with less than
- Expecting sensor value to print
- Thinking else block runs when condition is true
int sensorPin = A0;
int threshold = 400;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = digitalRead(sensorPin);
if(sensorValue < threshold) {
Serial.println("Obstacle detected");
}
delay(100);
}Solution
Step 1: Check sensor reading function
IR sensor outputs analog values, so analogRead() must be used, not digitalRead().Step 2: Verify other code parts
Serial.begin is present, delay is reasonable, comparison operator is correct.Final Answer:
Using digitalRead instead of analogRead for analog sensor -> Option AQuick Check:
Analog sensor needs analogRead() [OK]
- Using digitalRead for analog sensor pins
- Forgetting Serial.begin() (not the case here)
- Misusing delay() values
Solution
Step 1: Understand range condition
To detect values between 200 and 400, sensorValue must be greater than 200 AND less than 400.Step 2: Analyze each option
if(sensorValue < 200 && sensorValue > 400) { Serial.println("Obstacle detected"); }: impossible condition (< 200 && > 400); if(sensorValue >= 200 || sensorValue <= 400) { Serial.println("Obstacle detected"); } uses || which allows all values; if(sensorValue > 200 && sensorValue < 400) { Serial.println("Obstacle detected"); }: > 200 && < 400 correct; if(sensorValue == 200 && sensorValue == 400) { Serial.println("Obstacle detected"); } checks equality to both values simultaneously, impossible.Final Answer:
if(sensorValue > 200 && sensorValue < 400) { Serial.println("Obstacle detected"); } -> Option CQuick Check:
Use AND (&&) for range checks [OK]
- Using || instead of && for range
- Checking impossible conditions
- Using equality instead of range
Ready to prove it?
Pass the test and this pattern moves unknown
