CYTHEIA AIOpen the editor

IR Obstacle Detector

Detect obstacles with an IR sensor and sound a buzzer.

Open this circuit and run it

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

Wiring

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

Open this circuit and run it

Next challenge

More projects in Sensors