What We’re Doing
We'll connect an IR sensor to detect if an object is in front of it. Based on the sensor
output, the Arduino will:
• Use if to check the object presence
• Use digitalRead() to read sensor
• Print status using Serial.println()
modules output:
IR Sensor Pin Connect To
• LOW = Obstacle detected VCC 5V
• HIGH = No obstacle GND GND
OUT D8
Arduino Code
void setup() {
pinMode(8, INPUT); // IR sensor output connected to
pin 8
Serial.begin(9600); // Start serial monitor
}
void loop() {
int irState = digitalRead(8); // Read sensor
if (irState == LOW) {
Serial.println("Obstacle Detected!");
} else {
Serial.println("Path Clear.");
}
delay(500); // Half second delay
}
Serial Monitor Output Example
If something is in front of the sensor:
Obstacle Detected!
Obstacle Detected!
If nothing is detected:
Path Clear.
Path Clear.