Arduino Nano - Obstacle Sensor

This tutorial instructs you how to use Arduino Nano with the infrared obstacle avoidance sensor. In detail, we will learn:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×IR Obstacle Avoidance Sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of IR Obstacle Avoidance Sensor

The infrared obstacle sensor is utilized to identify the presence of any barrier in front of the module by means of the infrared signal. Its detection range lies between 2cm and 30cm, and can be adjusted with a built-in potentiometer.

The Infrared Obstacle Avoidance Sensor Pinout

The IR obstacle avoidance sensor has three pins:

  • GND pin: must be connected to GND (0V)
  • VCC pin: must be connected to VCC (5V or 3.3v)
  • OUT pin: is an output pin - it will be LOW when an obstacle is detected and HIGH when there is no obstacle. This pin needs to be connected to an Arduino Nano input pin.
IR Obstacle Avoidance Sensor pinout

How It Works

The infrared obstacle sensor module contains an IR transmitter and an IR receiver. The transmitter emits the IR signal. The receiver looks for the reflected IR signal to detect if an object is present or not. The OUT pin of the sensor will indicate the presence of an obstacle:

  • If there is an obstacle in front of the sensor, the OUT pin will be LOW
  • If there is no obstacle in front of the sensor, the OUT pin will be HIGH

※ NOTE THAT:

The sensor may get deformed during shipment, which can cause malfunctioning. If the sensor is not working properly, adjust the IR transmitter and receiver to ensure they are parallel to each other.

Wiring Diagram

The wiring diagram between Arduino Nano and IR Obstacle Avoidance Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For IR Obstacle Avoidance Sensor

  • Initializes the Arduino Nano pin to the digital input mode by utilizing the pinMode() function. For instance, D2:
pinMode(2, INPUT_PULLUP);
  • Uses the digitalRead() function to read the state of the Arduino Nano pin.
int state = digitalRead(2);

Arduino Nano Code

Two methods exist for programming an obstacle avoidance application:

  • Taking action or not when an obstacle is present or absent
  • Taking action or not when an obstacle is detected or removed

Arduino Nano code for checking if the obstacle is present

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-obstacle-sensor */ #define SENSOR_PIN 2 // The Arduino Nano pin connected to the out pin of obstacle avoidance sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the Arduino Nano pin as an input 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

  • Copy the code and open it with Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano.
  • Place an obstacle in front of the sensor for a period of time, then take it away.
  • Check out the result 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 Nano code for detecting obstacle

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-obstacle-sensor */ #define SENSOR_PIN 2 // The Arduino Nano pin connected to the out pin of obstacle avoidance sensor int prev_state = HIGH; // The previous state from the input pin int obstacle_state; // The current reading from the input pin void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the Arduino Nano pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: obstacle_state = digitalRead(SENSOR_PIN); if (prev_state == HIGH && obstacle_state == LOW) Serial.println("The obstacle is detected"); else if (prev_state == LOW && obstacle_state == HIGH) Serial.println("The obstacle is cleared"); delay(50); // save the the last state prev_state = obstacle_state; }

Detailed Instructions

  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Place an obstacle in front of the sensor for a while, then remove it.
  • Check out the result 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