int sensorinput = 4; // Digital pin connected to IR sensor output
int ledoutput = 11; // Pin connected to LED
void setup() {
pinMode(ledoutput, OUTPUT); // Set LED pin as output
pinMode(sensorinput, INPUT); // Set sensor pin as input
Serial.begin(9600); // Initialize Serial Monitor
}
void loop() {
int value = digitalRead(sensorinput); // Read sensor output
Serial.println(value); // Print sensor value to Serial Monitor
if (value == LOW) { // If sensor detects object (LOW signal)
digitalWrite(ledoutput, HIGH); // Turn ON LED
delay(100); // Small delay for visibility
} else {
digitalWrite(ledoutput, LOW); // Turn OFF LED
}
}
Instruction Details
Wiring: Connect IR sensor VCC to 5V, GND to GND, OUT to Digital Pin 4. Connect LED anode to Digital Pin 11 via 220Ω, cathode to GND.
Library: Not required (built-in).
Upload Code: Tools → Board → Arduino Uno. Tools → Port → select your COM port. Click Upload button.
View Output: LED turns ON when obstacle detected. Open Serial Monitor at 9600 baud to see digital values. Place object in front of sensor to test.
How to Use
Connect the required components as per the Pin Configuration above
Open Arduino IDE and create a new sketch
Copy and paste the code above
Select your Arduino board and COM port from Tools menu
Click the Upload button to upload the code to your Arduino
Open Serial Monitor (if applicable) to see the output