The line follower, as the name implies, is an automated self-driven vehicle (which follows line). In general, there are two types of line follower. In one type, the line follower follows the black line on the white surface, whereas, in the other type, it follows the white line on the black surface. These types of line followers are used in the industries, food restaurants for food delivery, the medical field for transmitting the medicine to the required patients, etc. Now, we will be building a line following robot by using mainly IR sensors, L293D, and a microcontroller. Here, we will be understanding the working of the IR sensor and L293D and see how it can be used in our line follower project.

 

Components Required

  1. Buy Arduino UNO with Programming Cable - 1
  2. Buy L293D module - 1
  3. Buy IR sensor module - 2
  4. Buy Metal Robot Chassis - 1
  5. Buy L-Shape Geared DC Motor - 2
  6. Buy Castor Wheel - 2
  7. Buy Geared DC Motor Wheel - 2
  8. Buy Male to Male and female to Male Wires
  9. 9V Battery with Connector - 2

 

IR Sensor 

So how does an IR sensor work? An IR sensor contains IR LED and a photodiode. The IR LED will be continuously emitting the IR rays and the photodiode will act as a receiver to the IR rays emitted by the IR LED. Whenever the IR rays emitted by the IR LED hits an object (Note: the surface shouldn’t be black color), the rays are reflected back. If the photodiode receives the IR rays emitted by the IR LED, then the output of the IR sensor is digital high. If the photodiode doesn’t receive any IR rays emitted by the IR LED, then the output will be digital low. When it comes to the operating voltage of the IR sensor, it operates on 5V and the output of the IR sensor module is either 5V (high) or 0V (low).

IR Line Follower Sensor

 

L293D Motor Driver Module 

The L293D module is a dual h-bridge motor driver circuit. By using the L293D module, we can control two motors at the same time. it can also drive the motor in either of the direction, which makes it perfect for any robot projects. The operating voltage of the module ranges from 4.5V to 12V. The output current of the module is 1A per channel. The module’s pinout is shown in the below picture.

L293D Motor Driver Module

 

Line follower robot circuit diagram

The connections for the line follower project is shown below. At first, let’s see the connections between the Arduino and IR sensors. The IR sensor contains 3 pins, in which the VCC and GND pins are connected to the 5V supply and GND pin of the Arduino. The output pin of the IR sensor, which is connected to the left side of the line follower (left sensor), is connected to pin 4 of the Arduino. The output pin of the IR sensor, which is connected to the right side of the line follower (right sensor)  is connected to the pin 3 of the Arduino. The below fig shows the sensor’s position on the line follower robot.

 

Now, let’s see the connections between the Arduino and L293D motor module. The L293D module has 3 pins, which deals with the power supply. The pin to the rightmost is connected to 5V of the Arduino, and the leftmost pin is given 9V from a battery. The center pin is grounded ( join the battery negative and GND pin of Arduino and connect it with the GND pin of the L293D module).

 

The M1 and M2 terminal blocks are used to connect the positive and negative terminals of the motors M1 and M2. The pins EN1, C1-A, C1-B, EN2, C2-A, C2-B are connected to the pins 9, 7, 6, 11, 10, 8 of the Arduino. To power the Arduino, we use a 9V battery. The positive terminal of the battery is connected to the Vin of the Arduino and the negative terminal of the battery is connected to the GND of the Arduino. The Arduino line follower robot circuit diagram is shown below.

Arduino Uno Line Follower Robot Circuit Diagram

Arduino Uno Line Follower Robot Code Explanation 

At first, we will define the pins, which will make our coding easy. The pins which are connected to enable pins of the L293D module is called en1 or en2. The pins connected to the input pins of the L293D are defined as input1, input2, input3, input4 pins.  The sensor, which is connected to the right side of the line follower is called R_sensor and the sensor which is connected to the left side is called the L_sensor.

#define en1 9

#define input1 6

#define input2 7

#define en2 11

#define input3 10

#define input4 8

#define L_sensor  4

#define R_sensor 3

 

In the void setup, we will set the pins en1, en2, input1, input2, input3, input4 as output pins, and pins L_sensor and R_sensor are set as input pins. We will be using the analog write command to write a value of 250 on the enable pins. The problem of using digital pins to connect with a enable pin is that by using digital pins, you can either output a voltage of 5V (high value) or a 0V (low value), but by using analog write command on analog pins, you can output, voltage ranging from 0 to 5V. If you write an analog value of 0 on the analog pin, then the output from that analog pin will be 0V. if you write an analog value of 255 on the analog pin, then the output from that analog pin will be 5V. You can use the below formula to find the value needed to write on the analog pin to get the desired voltage.

Value = (voltage needed/5) * 255.

The voltage needed will be the desired output voltage.

By using the above formula, you will get the value, which will be needed to write on the analog pin to get the desired voltage as output.

The reason for connecting the analog pins to the enable pin is, by changing the voltage at the enable pins, we can vary the speed of the motor. There may be situations in which you desire to slow down your line follower, in that case, you can just vary the value of that analog pin. And for the serial monitor’s initializing part, it is used for debugging purposes.

void setup() {

  pinMode(en1, OUTPUT);

  pinMode(en2, OUTPUT);

  pinMode(input1, OUTPUT);

  pinMode(input2, OUTPUT);

  pinMode(input3, OUTPUT);

  pinMode(input4, OUTPUT);

  pinMode(L_sensor, INPUT);

  pinMode(R_sensor, INPUT);

  Serial.begin(9600);

      analogWrite(en1,250);

    analogWrite(en2, 250);

}

void loop() {

Input1 and input2 pins are related to motor 1, and input3 and input4 pins are related to the motor 2. If input1 and input2 are high and low value, it rotates in one direction, and if input1 and input2 are low and high value, the motor rotates in the direction opposite to the previous direction. When it comes to finding when to switch on/off the motors, it depends on the sensors.  If both the sensors are giving logical high value, it means that the black line is in between the sensor and we need to switch ON both the motors. This makes the line follower move forward. The below code does the same, it reads the sensor’s output and if both the sensor’s output is logical high value then both the motors are switched ON.

  if((digitalRead(L_sensor) == HIGH) && (digitalRead(R_sensor) == HIGH)){

    Serial.println(" Left and Right sensors are high");

    digitalWrite(input1, HIGH);

    digitalWrite(input2, LOW);

    digitalWrite(input3, HIGH);

    digitalWrite(input4, LOW);

  }

If the right sensor is giving digital low value, it means that the black track is not in between the IR sensors and the line follower is crossing the black track from the right side. Now, we need to turn the line follower towards the right. The below code stops the motor connected to the right side and switches ON the left side motor. This will be continued until both the sensors started giving logic high as output.

  else if((digitalRead(L_sensor) == HIGH) && (digitalRead(R_sensor) == LOW)){

    Serial.println(digitalRead(L_sensor));

    Serial.println(digitalRead(R_sensor));

    digitalWrite(input1, LOW);

    digitalWrite(input2, LOW);

    digitalWrite(input3, HIGH);

    digitalWrite(input4, LOW);

  }

If the left sensor is giving digital low value, it means that the black track is not in between the sensors and the line follower is crossing the black track from the left side. Now, we need to turn the line follower towards the left. The below code stops the motor connected to the left side and switches ON the right side motor. This will be continued until both the sensors started giving logic high as output.

  else if((digitalRead(L_sensor) == LOW) && (digitalRead(R_sensor) == HIGH)){

     Serial.println(" Left Low");

    Serial.println(digitalRead(" Right HIGH"));

    digitalWrite(input1, HIGH);

    digitalWrite(input2, LOW);

    digitalWrite(input3, LOW);

    digitalWrite(input4, LOW);    

  }

If both the sensors are giving digital low value as output, then the motor is switched off. This condition is the terminate condition.

  else {

    digitalWrite(input1, LOW);

    digitalWrite(input2, LOW);

    digitalWrite(input3, LOW);

    digitalWrite(input4, LOW);

  }

}

Working of Arduino Uno based Line Follower Robot

We will be using a black tape as a track. If the sensor comes on top of the black track, the output of the sensor will be digital low, and if the sensors are not facing the black track the output of the sensor will be digital high. So, we will position our sensors in such a way that each sensor should be placed on either side of the black track and must be in height sufficient to sense the variation of the track. When the right sensor is giving an output of digital low, it means that the line follower is crossing the black track from the right side. At this condition, we need to stop the righthand side motor. If the same condition arises for the left sensor, we need to stop the left side motor. If both the sensors are giving digital high, we need to switch on both the motors.

 

If you have connected the components correctly, the end product looks more or less like this.

 

Just place the line follower on the track and see how it follows the line. You could build this along with your friends, and keep competition among yourselves to check which robot will complete the track in a short time.

Arduino Line Follower Robot Project

 

Line Follower Robot Arduino Complete Code

#define en1 9
#define input1 6
#define input2 7
#define en2 11
#define input3 10
#define input4 8
#define L_sensor 4
#define R_sensor 3
void setup() {
pinMode(en1, OUTPUT);
pinMode(en2, OUTPUT);
pinMode(input1, OUTPUT);
pinMode(input2, OUTPUT);
pinMode(input3, OUTPUT);
pinMode(input4, OUTPUT);
pinMode(L_sensor, INPUT);
pinMode(R_sensor, INPUT);
// put your setup code here, to run once:
Serial.begin(9600);
analogWrite(en1,250);
analogWrite(en2, 250);
}
void loop() {
if((digitalRead(L_sensor) == HIGH) && (digitalRead(R_sensor) == HIGH)){
Serial.println(" Left and Right sensors are high");
digitalWrite(input1, HIGH);
digitalWrite(input2, LOW);
digitalWrite(input3, HIGH);
digitalWrite(input4, LOW);
}
else if((digitalRead(L_sensor) == HIGH) && (digitalRead(R_sensor) == LOW)){
Serial.println(digitalRead(L_sensor));
Serial.println(digitalRead(R_sensor));
digitalWrite(input1, LOW);
digitalWrite(input2, LOW);
digitalWrite(input3, HIGH);
digitalWrite(input4, LOW);
}
else if((digitalRead(L_sensor) == LOW) && (digitalRead(R_sensor) == HIGH)){
Serial.println(" Left Low");
Serial.println(Right HIGH);
digitalWrite(input1, HIGH);
digitalWrite(input2, LOW);
digitalWrite(input3, LOW);
digitalWrite(input4, LOW);
}
else {
digitalWrite(input1, LOW);
digitalWrite(input2, LOW);
digitalWrite(input3, LOW);
digitalWrite(input4, LOW);
}
}

 

Video - Working of Line Follower Robot 

Leave a comment

Please note, comments must be approved before they are published

Your cart

×