📖 Scenario: You are building a simple robot that can detect obstacles using an IR sensor. The sensor will tell the robot if something is close by, so it can stop or change direction.
🎯 Goal: Create a program that reads the IR sensor value and prints Obstacle detected when an obstacle is close, or No obstacle when the path is clear.
📋 What You'll Learn
Use a variable called irSensorPin to store the IR sensor input pin number.
Use a variable called threshold to set the distance limit for obstacle detection.
Read the sensor value using digitalRead(irSensorPin).
Use an if statement to check if the sensor detects an obstacle.
Print Obstacle detected or No obstacle accordingly.
💡 Why This Matters
🌍 Real World
IR sensors are used in robots and devices to detect objects and avoid collisions.
💼 Career
Understanding sensor input and conditional logic is important for embedded systems and robotics jobs.
Progress0 / 4 steps
1
Set up the IR sensor pin
Create a variable called irSensorPin and set it to 7. Then set the pin mode of irSensorPin to INPUT inside the setup() function.
Arduino
Hint
Use pinMode(irSensorPin, INPUT); inside setup() to prepare the sensor pin.
2
Add a threshold variable
Create a variable called threshold and set it to 1. This will be used to decide if an obstacle is detected.
Arduino
Hint
The threshold variable helps decide when the sensor detects an obstacle.
3
Read sensor and check for obstacle
Inside the loop() function, read the sensor value into a variable called sensorValue using digitalRead(irSensorPin). Then use an if statement to check if sensorValue equals threshold.
Arduino
Hint
Use digitalRead(irSensorPin) to get the sensor value and compare it with threshold.
4
Print obstacle detection result
Inside the if block, print Obstacle detected using Serial.println(). Add an else block to print No obstacle when the sensor value does not equal threshold.
Arduino
Hint
Use Serial.println() to show messages on the serial monitor. Add a small delay to avoid flooding the output.
Practice
(1/5)
1. What does an IR sensor detect in an obstacle detection system?
easy
A. Visible light intensity
B. Magnetic fields around objects
C. Sound waves bouncing off objects
D. Reflected infrared light from nearby objects
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 D
Quick Check:
IR sensor = reflected infrared light [OK]
Hint: IR sensors detect reflected infrared light, not visible or sound [OK]
Common Mistakes:
Confusing IR sensor with light or sound sensors
Thinking it detects visible light
Assuming it detects magnetic fields
2. Which Arduino code snippet correctly reads an analog IR sensor value from pin A0?
easy
A. int sensorValue = analogRead(A0);
B. int sensorValue = digitalRead(A0);
C. int sensorValue = analogWrite(A0);
D. int sensorValue = digitalWrite(A0);
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 A
Quick Check:
Use analogRead for analog sensors [OK]
Hint: Use analogRead() for analog sensor pins like A0 [OK]
Common Mistakes:
Using digitalRead() for analog sensors
Using analogWrite() which is for output
Using digitalWrite() which is for digital output
3. What will be printed if the IR sensor value is 350 and the threshold is 300 in this code?
int sensorValue = 350;
int threshold = 300;
if(sensorValue > threshold) {
Serial.println("No obstacle");
} else {
Serial.println("Obstacle detected");
}
medium
A. Error
B. No obstacle
C. 350
D. 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 B
Quick Check:
350 > 300 = true, prints "No obstacle" [OK]
Hint: Check if sensor value is above threshold for output [OK]
Common Mistakes:
Confusing greater than with less than
Expecting sensor value to print
Thinking else block runs when condition is true
4. Identify the error in this Arduino code for obstacle detection:
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);
}
medium
A. Using digitalRead instead of analogRead for analog sensor
B. Missing Serial.begin in setup()
C. Incorrect delay value
D. Threshold comparison operator is wrong
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 A
Quick Check:
Analog sensor needs analogRead() [OK]
Hint: Use analogRead() for analog sensors, not digitalRead() [OK]
Common Mistakes:
Using digitalRead for analog sensor pins
Forgetting Serial.begin() (not the case here)
Misusing delay() values
5. You want to detect obstacles only when the IR sensor value is between 200 and 400. Which code snippet correctly implements this?