Obstacle Avoider Robot Using Arduino and IR Sensor
In this blog, we will learn to build an obstacle avoiding robot based on Infra-Red sensors. The primary purpose of this bot is to traverse forward autonomously and change directions if it encounters any significant obstacle in its path. The bot will avoid any obstacle big enough to be detected by the Infra-Red sensor mounted on the front.
Components required to make obstacle avoiding robot
- Arduino Uno (with cable)
- L298 Motor Driver
- Range adjustable IR sensor (x3)
- Chassis (x1)
- B.O. DC Motors (x2)
- Battery container (x1)
- Wheels (x2)
- Hammer caster (x1)
- Jumper wires: male-to-male|male-to-female|female-to-female (x10)
- Switch (x1)
- Spacers (x4)
- M3*30 screw (x4)
- M3*8 screw (x15)
- M3 nut (x15)
Step 1: Make the Robot itself
The first step is to make the bot itself, which comprises two driven wheels and a caster wheel at the back. The two front wheels are driven by 5V-12V B.O. Motors. The caster wheel is attached at the rear of the bot. This caster wheel helps the rear of the bot move freely as well as provides stability to the entire bot.
Step 2: Add the hardware required to power robot
The next step is to add the hardware required to power this bot and drive the wheels. We will be using an L298 motor driver to drive the wheels and the entire system will be powered by a 3S (11.1V) LiPo battery. This dual bidirectional motor driver is based on the very popular L298 Dual H-Bridge Motor Driver IC. This module will allow you to control the two motors in both directions easily and independently. We are also using a switch which will make turning on and off the bot easier.
Step 3: Add IR Sensor to detect obstacles
Now, moving on to the obstacle avoiding capabilities of the bot, we will be using a combination of 3 IR sensors to detect any obstacles and the sensor data is processed by an Arduino Uno R3 to drive the motors accordingly. The code is fairly simple and can be broken down into 3 sections.
- Declaration of all the variables and functions that we will be using in the program.
- Setup function: In this, we declare pins 6, 9, 10 and 11 as output pins, these pins have PWM functionality which means we can control the speed of the bot. These pins are connected to the input pins of the L298 motor driver. Additionally, two more GPIO pins have been made high to provide 5V to the IR sensors mounted on the front.
- In the loop function, we keep reading the analog sensor value from the IR sensors, a flag is raised if an obstacle is detected by the bot. The potentiometer can be used to tune to the sensor to detect an obstacle at a certain distance. Once an obstacle is detected by the IR sensors, a flag is raised and the program enters a certain loop, in this loop, the bot decides to turn left or right depending on which side the obstacle has been detected. In case an obstacle has been detected by the left IR sensor, the bot will move backwards briefly and them turn right. If the obstacle has been detected by the right IR sensor, the bot will briefly move backwards and then turn left. The bot moves back briefly and turns away from the obstacle and starts moving forward again. By default, the bot keeps moving forward until an obstacle is detected.