How to make a 4 way line tracking sensor module robot using Arduino

How to make a 4 way line tracking sensor module robot using Arduino

Hello guys, welcome back. In this tutorial, we will learn how to make a 4-way line tracking sensor module robot using Arduino. This tutorial will show you how to make a robot step by step at a low cost. For that. I have mainly used the Arduino UNO board and L298N motor driver board.

 How to make a 4-way line tracking sensor module robot using Arduino

This module also belongs to the IR sensor type. Also, the special feature here is that four IR sensors can be connected. This module can be used to identify obstacles as well as black/white. It can also be used to create line-following robots. In this tutorial, this module is used only to identify obstacles. Also, the four sensors connected to this module include four potentiometers to control the sensor distance separately. As with other sensor modules, this requires a 5v potential.

The PIN structure of this 4-way infrared line tracking module.

 How to make a 4-way line tracking sensor module robot using Arduino

OK, now let’s make this robot step by step. The required components are as follows.

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 5 * 3.5-inch piece of cardboard and attach the gear motors to it as follows. For that, use the glue gun.

Step 3

Thirdly, install the motor drive board and connect the gear motors to it. For that, use the pictures and circuit diagram below.

How to make a 4 way line tracking sensor module robot using Arduino

Step 4

Next, cut a 5 * 1.5-inch piece of cardboard and adjust it to fit the Arduino board. Then, glue it to the base cardboard.

Step 5

Afterward, connect the motor driver board to the Arduino board. For that, use the circuit diagram above. Next, glue the Arduino board.

Step 6

Next, cut a 3 * 1.5-inch piece of cardboard and connect the sensor to the Arduino board through it. Then, glue that cardboard piece to the base cardboard piece.

Step 7

Now cut a 5 * 1.5-inch piece of cardboard and a 3 * 1.5-inch piece of cardboard. Then, glue these pieces of cardboard as follows.

Step 8

Next, cut a 3 * 5-inch piece of cardboard and glue the sensor module to the piece of cardboard. Then, glue this piece of cardboard to the top of the robot.

Step 9

OK, now glue the IR sensors as follows. After, connect these sensors to the module.

Step 10

Next, connect the robot wheels and connect this robot to the computer. For that, use a USB cable.

Step 11

OK, now let’s create a program for this robot. It is as follows.

  • The complete program of this project – Download
/*obstacle avoiding robot.
  created by the SriTu Tech team.
  Read the code below and use it for any of your creations.
*/
 
//motor one
#define ENA 5
#define IN1 2
#define IN2 3
 
//motor two
#define ENB 6
#define IN3 7
#define IN4 8
 
//sensor pins
#define SEN1 9
#define SEN2 10
#define SEN3 11
#define SEN4 12
 
bool S1;
bool S2;
bool S3;
bool S4;
bool start = false;
 
void setup() {
  Serial.begin(9600);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
 
  pinMode(SEN1, INPUT);
  pinMode(SEN2, INPUT);
  pinMode(SEN3, INPUT);
  pinMode(SEN4, INPUT);
}
 
void loop() {
  S1 = digitalRead(SEN1);//sensor one
  S2 = digitalRead(SEN2);//sensor one
  S3 = digitalRead(SEN3);//sensor one
  S4 = digitalRead(SEN4);//sensor one
 
  if (S2 == 0 && S3 == 0) {
    start = true;
  }
  if (start) {
    if (S1 == 1 && S2 == 1 && S3 == 1 && S4 == 1) {
      motor(‘F’, 180);
      Serial.println(“Forward”);
    } else if (S1 == 0 && S2 == 0 && S3 == 0 && S4 == 0) {
      motor(‘B’, 180);
      Serial.println(“Backward”);
    } else if (S4 == 0) {
      motor(‘R’, 180);
      Serial.println(“TurnRight”);
    } else if (S2 == 0) {
      motor(‘R’, 180);
      Serial.println(“TurnRight”);
    } else if (S1 == 0) {
      motor(‘L’, 180);
      Serial.println(“TurnLeft”);
    } else if (S3 == 0) {
      motor(‘L’, 180);
      Serial.println(“TurnLeft”);
    }
  }
}
//motor control
void motor(char action, int Speed) {
  if (action == ‘F’) {//forward
    analogWrite(ENA, Speed);
    analogWrite(ENB, Speed);
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
  } else if (action == ‘B’) {//backward
    analogWrite(ENA, Speed);
    analogWrite(ENB, Speed);
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
  } else if (action == ‘L’) {//turnleft
    analogWrite(ENA, Speed);
    analogWrite(ENB, Speed);
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
  } else if (action == ‘R’) {//turnright
    analogWrite(ENA, Speed);
    analogWrite(ENB, Speed);
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
  }
  else if (action == ‘S’) {//stop
    analogWrite(ENA, 0);
    analogWrite(ENB, 0);
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
  }
}

Code explanation

Firstly, the motor driver PINs and sensor module PIns are defined. Afterward, five Boolean variables were created to help the program.

//motor one
#define ENA 5
#define IN1 2
#define IN2 3
 
//motor two
#define ENB 6
#define IN3 7
#define IN4 8
 
//sensor pins
#define SIN1 9
#define SIN2 10
#define SIN3 11
#define SIN4 12
 
bool S1;
bool S2;
bool S3;
bool S4;
bool start = false;

In the void setup, motor driver pins and sensor pins are set as Output and Input pins.

void setup() {
  Serial.begin(9600);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
 
  pinMode(SEN1, INPUT);
  pinMode(SEN2, INPUT);
  pinMode(SEN3, INPUT);
  pinMode(SEN4, INPUT);
}

In the void loop, the sensor values are read and put into four boolean variables. After, these values were checked using the IF condition. Then, if the value of all four sensors is 1, the robot will move forward (use your hands for this). Also, if the value of all four sensors is 0, the robot will backward. Afterward, If the value of the first and third sensors is 0, the robot moves to the left, and if the value of the second and fourth sensors is 0, the robot moves to the right.

void loop() {
  S1 = digitalRead(SEN1);//sensor one
  S2 = digitalRead(SEN2);//sensor one
  S3 = digitalRead(SEN3);//sensor one
  S4 = digitalRead(SEN4);//sensor one
 
  if (S2 == 0 && S3 == 0) {
    start = true;
  }
  if (start) {
    if (S1 == 1 && S2 == 1 && S3 == 1 && S4 == 1) {
      motor(‘F’, 180);
      Serial.println(“Forward”);
    } else if (S1 == 0 && S2 == 0 && S3 == 0 && S4 == 0) {
      motor(‘B’, 180);
      Serial.println(“Backward”);
    } else if (S4 == 0) {
      motor(‘R’, 180);
      Serial.println(“TurnRight”);
    } else if (S2 == 0) {
      motor(‘R’, 180);
      Serial.println(“TurnRight”);
    } else if (S1 == 0) {
      motor(‘L’, 180);
      Serial.println(“TurnLeft”);
    } else if (S3 == 0) {
      motor(‘L’, 180);
      Serial.println(“TurnLeft”);
    }
  }
}

Finally, motor control functions are designed to support the main program.

//motor control
void motor(char action, int Speed) {
  if (action == ‘F’) {//forward
    analogWrite(ENA, Speed);
    analogWrite(ENB, Speed);
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
  } else if (action == ‘B’) {//backward
    analogWrite(ENA, Speed);
    analogWrite(ENB, Speed);
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
  } else if (action == ‘L’) {//turnleft
    analogWrite(ENA, Speed);
    analogWrite(ENB, Speed);
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
  } else if (action == ‘R’) {//turnright
    analogWrite(ENA, Speed);
    analogWrite(ENB, Speed);
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
  }
  else if (action == ‘S’) {//stop
    analogWrite(ENA, 0);
    analogWrite(ENB, 0);
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
  }
}

Step 12

Now, select the board and port. After, upload this program to the Arduino UNO board.

Step 13

Lastly, connect the power source to the robot. You can use any other power source for that. I use two phone batteries. But, remember to give potentials between 7v to 12v.

Step 14

OK, turn ON this robot and set the values of all four sensors to 1. To do this, place your hands in front of the four sensors at the same time. Then, the robot will then move forward. The full video guide is below. So, we will meet in the next tutorial. Have a good day.

How to make a 4 way line tracking sensor module robot using Arduino

Similar Posts

Leave a Reply

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