Arduino - Infrared Obstacle Avoidance Sensor

In this tutorial, we are going to learn how to use Arduino and the infrared obstacle avoidance sensor to detect the presence of the obstacle.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×IR Obstacle Avoidance Sensor
n×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About IR Obstacle Avoidance Sensor

The infrared (IR) obstacle sensor is used to detect the presence of any obstacle in front of the sensor module by using the infrared signal. The detection range is from 2cm to 30cm. The detection range can be adjusted by a built-in potentiometer.

Pinout

IR obstacle avoidance sensor includes three pins:

  • GND pin needs to be connected to GND (0V)
  • VCC pin needs to be connected to VCC (5V or 3.3v)
  • OUT pin is an output pin: LOW when there is an obstacle, HIGH when there is no obstacle. This pin needs to be connected to Arduino's input pin.
  • IR Obstacle Avoidance Sensor Pinout

How It Works

An infrared obstacle sensor module has a built-in IR transmitter and IR receiver. The IR transmitter emits the IR signal. The IR receiver looks for the reflected IR signal to determine if the object is present or not. The presence of obstacle is reflected in the OUT pin:

  • If the obstacle is present in front of the sensor, the sensor's OUT pin is LOW
  • If the obstacle is NOT present in front of the sensor, the sensor's OUT pin is HIGH

Wiring Diagram

Arduino IR Obstacle Avoidance Sensor Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

How To Program For IR Obstacle Avoidance Sensor

  • Initializes the Arduino pin to the digital input mode by using pinMode() function. For example, pin GIOP18
pinMode(18, INPUT_PULLUP);
  • Reads the state of the Arduino pin by using digitalRead() function.
int state = digitalRead(18);

Arduino Code

There are two ways to program for an obstacle avoidance application:

  • Do or don't do something while the obstacle is present or not present
  • Do or don't do something when the obstacle is detected or cleared

Arduino code for checking if the obstacle is present

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-infrared-obstacle-avoidance-sensor */ // Arduino's pin connected to OUT pin of IR obstacle avoidance sensor const int SENSOR_PIN = 8; void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the Arduino's pin as aninput pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: int state = digitalRead(SENSOR_PIN); if (state == LOW) Serial.println("The obstacle is present"); else Serial.println("The obstacle is NOT present"); delay(100); }

Quick Steps

  • Copy the above code and open it with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Put an obstacle in front of the sensor for a while, and then remove it.
  • See the result on Serial Monitor.
  • COM6
    Send
    The obstacle is NOT present The obstacle is NOT present The obstacle is NOT present The obstacle is NOT present The obstacle is NOT present The obstacle is present The obstacle is present The obstacle is present The obstacle is present The obstacle is NOT present The obstacle is NOT present
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Arduino code for detecting obstacle

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-infrared-obstacle-avoidance-sensor */ // Arduino's pin connected to OUT pin of IR obstacle avoidance sensor const int SENSOR_PIN = 8; int lastState = HIGH; // the previous state from the input pin int currentState; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the Arduino's pin as aninput pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: currentState = digitalRead(SENSOR_PIN); if (lastState == HIGH && currentState == LOW) Serial.println("The obstacle is detected"); else if (lastState == LOW && currentState == HIGH) Serial.println("The obstacle is cleared"); delay(50); // save the the last state lastState = currentState; }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Put an obstacle in front of the sensor awhile, and then remove it.
  • See the result on Serial Monitor.
  • COM6
    Send
    The obstacle is detected The obstacle is cleared
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Function References

※ OUR MESSAGES