IR Obstacle Detector
Detect obstacles with an IR sensor and sound a buzzer.
- Beginner
- Sensors
- 5 parts
Opens in your browser with the parts, wiring and code already in place. Press Run to watch it work, then change anything you like.
How it works
An IR obstacle sensor shines infrared light and watches for the reflection. In this circuit its output goes high when something is in front of it, and that output goes to pin 4. The Arduino answers with two outputs: an LED on pin 13 and a buzzer on pin 8. The LED has no series resistor here to keep the circuit small; on real hardware put one of around 220 Ω in series with it.
loop() checks pin 4 ten times a second. With an obstacle in front, it lights the LED, plays an 880 Hz beep for 100 milliseconds with tone(8, 880, 100), and prints “Obstacle detected”. Otherwise it turns the LED off, calls noTone(8) to silence the buzzer, and prints “Clear”.
Parts
- Arduino Uno
- IR Sensor
- Buzzer
- LED
- GND
Wiring
- Arduino Uno 5V → IR Sensor VCC
- IR Sensor OUT → Arduino Uno D4
- IR Sensor GND → Arduino Uno GND
- Arduino Uno D13 → LED Anode (+)
- LED Cathode (-) → GND
- Arduino Uno D8 → Buzzer +
- Buzzer - → GND
Arduino code
// IR Obstacle Detector
void setup() {
pinMode(4, INPUT);
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
Serial.begin(9600);
}
void loop() {
int obstacle = digitalRead(4);
if (obstacle == HIGH) {
digitalWrite(13, HIGH);
tone(8, 880, 100);
Serial.println("Obstacle detected");
} else {
digitalWrite(13, LOW);
noTone(8);
Serial.println("Clear");
}
delay(100);
}
Try changing it
- Change the pitch: replace 880 with 440 for a lower beep or 1760 for a higher one.
- Many real sensor modules output LOW when they see an obstacle. If yours does, change obstacle == HIGH to obstacle == LOW.