Arduino Nano - Obstacle Sensor
This tutorial instructs you how to use Arduino Nano with the infrared obstacle avoidance sensor. In detail, we will learn:
- How to connect the Arduino Nano to the infrared obstacle avoidance sensor.
- How to program the Arduino Nano to read the state of the infrared obstacle avoidance sensor.
- How to program the Arduino Nano to detect obstacles.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
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.
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
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:
- Uses the digitalRead() function to read the state of the Arduino Nano pin.
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
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.
Arduino Nano code for detecting obstacle
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.