How to make a line follower robot using a 3-way IR infrared sensor module

How to make a line follower robot using a 3-way IR infrared sensor module

Hello, welcome back to another tutorial from the SriTu Hobby. In this tutorial, we will talk about how to make a line follower robot using a 3-way IR infrared line tracking sensor module. Also, the Arduino UNO Board and the L298N Motor Driver Board have been used primarily for this project. The chassis of this robot car is made of foam board. You can even use cardboard for this.

3-way IR infrared line tracking sensor module

How to make a line follower robot using a 3-way IR infrared sensor module

This module is made using three IR sensors. Hence this is called 3-way. Also, we can get digital outputs separately as 1 or 0 through these three IR sensors. 5v potential should be provided for this.

PIN structure of this sensor

How to make a line follower robot using a 3-way IR infrared sensor module

Process of this line follower robot

When this robot is activated on the black and white track, if the left and right sensor values are 1, and if the center sensor value is 0, the robot will forward. Also, if the right sensor value is 1, the robot will turn left and if the left sensor value is 1, the robot will turn right.

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

Step 1

Firstly, let’s identify these components.

Arduino UNO board

3-way IR sensor module

L298N motor driver board

Gear motor

Robot wheel

Li-ion battery holder

Li-ion battery

Foamboard

Nut and bolt

Jumper wires

Step 2

Secondly. cut a 5.5 x 3-inch size foam board piece.

Step 3

Third, cut it out as follows and glue the 3-way IR sensor module to the front side. For that, use the pictures below.

Step 4

Now, dig a hole in the middle of this robot chassis. Then, put the jumper wire through this hole.

Step 5

And then. Glue the Arduino UNO board to the top of the chassis.

Step 6

Now, connect the 3-way IR sensor module to the Arduino board. To do this, use the circuit diagram below.

How to make a line follower robot using a 3-way IR infrared sensor module

How to make a line follower robot using a 3-way IR infrared sensor module

Step 7

Next, glue the gear motors and motor drive board to the robot chassis. After, connect the gear motors to the motor driver board.

Step 8

After, connect the motor driver board to the Arduino board. For that, use the circuit diagram above.

Step 9

Then, attach the Li-ion battery holder to the top of the Arduino UNO board. Afterward, connect it to the motor drive board.

Step 10

Now, glue the switch and dig a hole in the back of the chassis and insert a nut through this hole and tighten.

Step 11

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

Step 12

OK, now let’s creates the program for this line follower robot. It is as follows.

/*line following robot.
This code created by the SriTu Hobby team.
https://www.srituhobby.com
*/

#define left 6
#define center 7
#define right 8

//motor one
#define ENA 9
#define IN1 2
#define IN2 3

//motor two
#define ENB 10
#define IN3 4
#define IN4 5

int Speed = 120; // speed of this robot

void setup() {
Serial.begin(9600);
pinMode(left, INPUT);
pinMode(center, INPUT);
pinMode(right, INPUT);

pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENB, OUTPUT);
}

void loop() {
bool leftV = digitalRead(left);
bool centerV = digitalRead(center);
bool rightV = digitalRead(right);

Serial.println(rightV);

if (leftV == 1 && centerV == 0 && rightV == 1) {
carforward();
Serial.println(“forward”);
} else if (leftV == 0 && centerV == 0 && rightV == 0) {
carStop();
} else if (leftV == 1 && centerV == 1 && rightV == 1) {
carStop();
} else if (leftV == 0 && centerV == 0 && rightV == 1) {
carturnleft();
} else if (leftV == 1 && centerV == 0 && rightV == 0) {
carturnright();
} else if (leftV == 0 && centerV == 1 && rightV == 1) {
carturnleft();
} else if (leftV == 1 && centerV == 1 && rightV == 0) {
carturnright();
}

}

void carforward() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void carturnleft() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void carturnright() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void carStop() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}


Code explanation

Firstly, sensor pins and motor driver pins defined. Later, a variable is created for the speed of this robot.

#define left 6
#define center 7
#define right 8

//motor one
#define ENA 9
#define IN1 2
#define IN2 3

//motor two
#define ENB 10
#define IN3 4
#define IN4 5

int Speed = 120; // speed of this robot

In the setup function, all pins are set as input and output.

void setup() {
Serial.begin(9600);
pinMode(left, INPUT);
pinMode(center, INPUT);
pinMode(right, INPUT);

pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENB, OUTPUT);
}

In the loop function, the sensor values are taken and put into three bool variables.

bool leftV = digitalRead(left);
bool centerV = digitalRead(center);
bool rightV = digitalRead(right);

After, these values were checked using the IF condition. Also, if all three of these values are 0 or 1, the robot will stop and the value of the two sensors on either side will be 1 and if the value of the middle sensor is 0, the robot will move forward. Next, if the right sensor value is 1, the robot will turn left and if the left sensor value is 1, the robot will turn right.

if (leftV == 1 && centerV == 0 && rightV == 1) {
carforward();
Serial.println(“forward”);
} else if (leftV == 0 && centerV == 0 && rightV == 0) {
carStop();
} else if (leftV == 1 && centerV == 1 && rightV == 1) {
carStop();
} else if (leftV == 0 && centerV == 0 && rightV == 1) {
carturnleft();
} else if (leftV == 1 && centerV == 0 && rightV == 0) {
carturnright();
} else if (leftV == 0 && centerV == 1 && rightV == 1) {
carturnleft();
} else if (leftV == 1 && centerV == 1 && rightV == 0) {
carturnright();
}

Forward, left, right and stop functions are as follows.

void carforward() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void carturnleft() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void carturnright() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void carStop() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}

Step 13

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

How to make a line follower robot using a 3-way IR infrared sensor module code upload

Step 14

Lastly, put the batteries to the battery holder and switch on this robot. Now, move the robot using a black and white track.

How to make a line follower robot using a 3-way IR infrared sensor module

OK, enjoy this project. The full video guide is given below. So, we will meet in the next tutorial.

Leave a Reply

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