How to make a line follower robot using a 3-way IR infrared sensor module
Hello, and welcome back to another tutorial from SriTu Hobby. This tutorial will discuss how to make a line follower robot using a 3-way IR infrared line tracking sensor module. The Arduino UNO Board and the L298N Motor Driver Board have also 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
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.
The PIN structure of this sensor
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.
- Arduino UNO board x 1 — Our Store / Amazon
- 3-way IR sensor module x 1 — Our Store / Amazon
- L298N motor driver board x 1 — Our Store / Amazon
- Gear motor x 2 — Our Store / Amazon
- Robot wheel x 2 — Our Store / Amazon
- Jumper wires — Our Store / Amazon
- Foamboard — Amazon /
- Nut and bolt x 1
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, 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
Thirdly, 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.
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
Next, attach the switch and make a hole in the back of the frame. Put a nut through this hole and tighten it.
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 create the program for this line follower robot. It is as follows.
- The complete program of this project – Download
/*line following robot.
This code created by the SriTu Hobby team.
Home Page
*/
#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 are 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);
Later, these values are tested using the IF condition. Also if all these three values are 0 or 1 then the robot stops. Also, if the value of the two sensors on both sides is 1 and the value of the middle sensor is 0, the robot moves forward. Next, if the value of the right sensor is 1, the robot will turn left, if the value of the left sensor 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.
Step 14
Finally, put the batteries into the battery holder and switch on this robot. Now, place the robot on the black track. Then you can see the robot’s movements.
OK, enjoy this project. The full video guide is given below. So, we will meet in the next tutorial. Have a good day.
How to make a line follower robot using a 3-way IR infrared sensor module