How to make an Obstacle avoiding robot using a 4-way line tracking sensor module

How to make an Obstacle avoiding robot using a 4-way line tracking sensor module

Hello, welcome back. In this tutorial, we will learn how to make an obstacle avoiding robot using a 4-way line tracking sensor module. Also, this robot is mainly based on Arduino UNO and L293D motor driver shield. If you have no idea about this 4-way line tracking sensor module, click this link. Also, this project can be made using foam board or cardboard. So this can be created at a low cost.

The structure and process of this robot

This robot is made of foam board and uses the L293D motor shield to drive the motors and controls everything via the Arduino UNO board. Also, a 4-way line tracking sensor module is used to detect obstacles. It includes four IR sensors, two on the front side and others are either sides.

When powering on this robot, it moves forward. At that point, the IR sensors emit IR rays. Then, when the sensors are close to the obstacles, the IR rays are reflected and a signal is sent through the sensors to the Arduino board. So, when the two front sensors close to an obstacle, the robot car turns right. Also, when the sensor on the right is close to an obstacle, the robot car turns left and moves forward. When the sensor on the left is close to an obstacle, the robot car turns right and moves forward. Also, this process continues.

OK, let’s do this project step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, cut a foam board piece using the following sizes.

How to make an Obstacle avoiding robot using a 4-way line tracking sensor module

Step 3

Thirdly, glue the gear motor and IR sensors as follows.

Step 4

Then, cut a piece of foam board of the following size and dig a hole to put the sensor wires.

Step 5

Next, connect the IR sensors to the sensor module through the hole drilled above. To do this, use the circuit diagram and pictures.

How to make an Obstacle avoiding robot using a 4-way line tracking sensor module

Step 6

Then, glue it to the top of the motors.

Step 7

Now, connect the motor driver shield to the Arduino board. Afterward, glue the Arduino board and sensor module to the robot chassis.

Step 8

Ok, now connect motors and sensor module to the Arduino UNO board. For that, use the circuit diagram above.

Step 9

Then, connect the li-ion battery holder as follows.

Step 10

Now, connect this robot to the computer. OK, let’s creates the program. It is as follows.

  • AFmotor library — Download
  • The complete program of this project – Download
/*Obstacle avoidance robot with Arduino.
  created by the SriTu Hobby team.
  Read the code below and use it for any of your creations.
  https://srituhobby.com
*/

#include <AFMotor.h>
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

int Speed = 150;

#define sensor1 A0
#define sensor2 A1
#define sensor3 A2
#define sensor4 A3

void setup() {
  Serial.begin(9600);
  motor1.setSpeed(Speed);
  motor2.setSpeed(Speed);
  motor3.setSpeed(Speed);
  motor4.setSpeed(Speed);
  pinMode(sensor1, INPUT);
  pinMode(sensor2, INPUT);
  pinMode(sensor3, INPUT);
  pinMode(sensor4, INPUT);
}

void loop() {
  bool value1 = digitalRead(sensor1);
  bool value2 = digitalRead(sensor2);
  bool value3 = digitalRead(sensor3);
  bool value4 = digitalRead(sensor4);
  //  Serial.println(value2);
  //  Serial.println(value1);

  if (value1 == 1 && value2 == 1 && value3 == 1 && value4 == 1 ) {
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);
  } else if (value1 == 0 && value2 == 0 && value3 == 1 && value4 == 1 ) {
    motor1.run(RELEASE);
    motor2.run(RELEASE);
    motor3.run(RELEASE);
    motor4.run(RELEASE);
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);
    delay(200);
    motor1.run(RELEASE);
    motor2.run(RELEASE);
    motor3.run(RELEASE);
    motor4.run(RELEASE);
    delay(1000);
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);
    delay(600);

  } else if (value1 == 1 && value2 == 1 && value3 == 0 && value4 == 1) {
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);
  } else if (value1 == 1 && value2 == 1 && value3 == 1 && value4 == 0) {
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);
  }
}

Code explanation

Firstly, library file is included.

#include <AFMotor.h>

Secondly, objects are created for motors.

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

Thirdly, sensor pins are defined.

#define sensor1 A0
#define sensor2 A1
#define sensor3 A2
#define sensor4 A3

In the setup function,

void setup() {
//The serial monitor is enabled
  Serial.begin(9600);
//The motor speed is set
  motor1.setSpeed(Speed);
  motor2.setSpeed(Speed);
  motor3.setSpeed(Speed);
  motor4.setSpeed(Speed);
//Sensor pins are set as input pins
  pinMode(sensor1, INPUT);
  pinMode(sensor2, INPUT);
  pinMode(sensor3, INPUT);
  pinMode(sensor4, INPUT);
}

In the loop function,

void loop() {
//Gets the sensor values
  bool value1 = digitalRead(sensor1);
  bool value2 = digitalRead(sensor2);
  bool value3 = digitalRead(sensor3);
  bool value4 = digitalRead(sensor4);
  //  Serial.println(value2);
  //  Serial.println(value1);
//These values are checked using the IF condition. If the value of all four sensors is 1, the robot moves forward
  if (value1 == 1 && value2 == 1 && value3 == 1 && value4 == 1 ) {
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);
//If the front sensor values are 0, the robot will stop and turn right.
  } else if (value1 == 0 && value2 == 0 && value3 == 1 && value4 == 1 ) {
    motor1.run(RELEASE);
    motor2.run(RELEASE);
    motor3.run(RELEASE);
    motor4.run(RELEASE);
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);
    delay(200);
    motor1.run(RELEASE);
    motor2.run(RELEASE);
    motor3.run(RELEASE);
    motor4.run(RELEASE);
    delay(1000);
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);
    delay(600);
//If the sensor 3 value is 0, the robot turns left.
  } else if (value1 == 1 && value2 == 1 && value3 == 0 && value4 == 1) {
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);
//If the sensor 4 value is 0, the robot turns right.
  } else if (value1 == 1 && value2 == 1 && value3 == 1 && value4 == 0) {
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);
  }
}

Step 11

Ok, now select board and port. Afterward, upload this code to the Arduino board.

Step 12

Lastly, connect the robot wheels and put the batteries.

OK, power up this robot and enjoy it. The full video guide is given below. So, we will meet in the next tutorial.

How to make an Obstacle avoiding robot using a 4-way line tracking sensor module

How to make an Obstacle avoiding robot using a 4-way line tracking sensor module

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *