Arduino UNO R4 - Obstacle Avoidance Sensor

In this guide, we will learn how to use the Arduino UNO R4 and the infrared obstacle avoidance sensor to detect obstacles.

Arduino UNO R4 Obstacle Avoidance Sensor

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×IR Obstacle Avoidance Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of IR Obstacle Avoidance Sensor

The infrared (IR) obstacle sensor can find obstacles in front of it using an infrared signal. It can detect objects from 2cm to 30cm away. You can change the detection distance with a built-in tool called a potentiometer.

Pinout

The IR obstacle avoidance sensor has three pins:

  • GND pin: Connect this pin to GND (0V).
  • VCC pin: Connect this pin to VCC (5V or 3.3V).
  • OUT pin: This is an output pin. It is LOW if there is an obstacle and HIGH if there is no obstacle. Connect this pin to the input pin of the Arduino UNO R4.
IR Obstacle Avoidance Sensor Pinout
image source: diyables.io

How It Works

An infrared obstacle sensor module includes an IR transmitter and an IR receiver. The IR transmitter sends out an IR signal, while the IR receiver detects this signal when it bounces back from an object to check if there is an obstacle. The detection of an obstacle is shown through the OUT pin.

  • When there is an obstacle in front of the sensor, the OUT pin of the sensor is LOW.
  • When there is no obstacle in front of the sensor, the OUT pin of the it is HIGH.

※ NOTE THAT:

During shipping, the sensor might get bent or damaged, which can cause it to not work properly. If the sensor is not working right, adjust the infrared transmitter and receiver so they are lined up parallel to each other.

Wiring Diagram

The wiring diagram between Arduino UNO R4 IR Obstacle Avoidance Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For IR Obstacle Avoidance Sensor

  • Set the Arduino UNO R4 pin as a digital input using the pinMode() function. For instance, configure pin 8.
pinMode(8, INPUT_PULLUP);
  • Reads the state of the Arduino UNO R4 pin by using digitalRead() function.
int state = digitalRead(8);

Arduino UNO R4 Code

There are use-cases for using the obstacle sensor:

  • Do something or do nothing, depending on whether there is an obstacle or not.
  • Do something or do nothing, when you detect an obstacle or when it is gone.

Arduino UNO R4 code for checking if the obstacle is present

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-obstacle-avoidance-sensor */ #define SENSOR_PIN 8 // The Arduino UNO R4 pin connected to OUT pin of IR obstacle avoidance sensor 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); }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the obstacle avoidance sensor to the Arduino Uno R4 according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the code and open the provided code with the Arduino IDE.
  • Click the Upload button in the Arduino IDE to upload the code to the Arduino UNO R4.
  • Place an obstacle in front of the sensor for some time, then take it away.
  • Check the results on the 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 UNO R4 code for detecting obstacle

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-obstacle-avoidance-sensor */ #define SENSOR_PIN 8 // The Arduino UNO R4 pin connected to OUT pin of IR obstacle avoidance sensor int prev_obstacle_state = HIGH; // the previous state from the input pin int obstacle_state; // 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: obstacle_state = digitalRead(SENSOR_PIN); if (prev_obstacle_state == HIGH && obstacle_state == LOW) Serial.println("The obstacle is detected"); else if (prev_obstacle_state == LOW && obstacle_state == HIGH) Serial.println("The obstacle is cleared"); delay(50); // save the the last state prev_obstacle_state = obstacle_state; }

Detailed Instructions

  • Copy the code and open it in Arduino IDE.
  • Click the Upload button in Arduino IDE to upload the code to the Arduino UNO R4.
  • Place an obstacle in front of the sensor for a moment, then take it away.
  • Check the results on the Serial Monitor.
COM6
Send
The obstacle is detected The obstacle is cleared
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Function References

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!