Well so fundamentally what does a Line-Following robot do?

sense the line converts that sensor data into motor speed sends this signal to motor

to first code, we need to install the library of the component and make sure the connections are alright

sample motor_test.ino (forked from a page-online using TB6612 module)

#include <SparkFun_TB6612.h>
 
#define AIN1 4
#define BIN1 6
#define AIN2 3
#define BIN2 7
#define PWMA 9
#define PWMB 10
#define STBY 5
 
// these constants are useful for modifying in accordance to our setup
// line up with function names like forward.  Value can be 1 or -1
const int offsetA = 1;
const int offsetB = 1;
 
// Initializing motors.  The library will allow to initialize as many
// motors as much as we have memory for.  If we are using functions like forward
// that take 2 motors as arguements you can either write new functions or
// call the function more than once.
 
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);
 
 
int P, D, I, previousError, PIDvalue, error;
int lsp, rsp;
int lfspeed = 200;
 
float Kp = 0;
float Kd = 0;
float Ki = 0 ;
 
 
int minValues[6], maxValues[6], threshold[6];
 
void setup()
{
  Serial.begin(9600);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
}
 
 
void loop()
{
  while (digitalRead(11)) {}
  delay(1000);
  calibrate();
  while (digitalRead(12)) {}
  delay(1000);
 
  while (1)
  {
    if (analogRead(1) > threshold[1] && analogRead(5) < threshold[5] )
    {
      lsp = 0; rsp = lfspeed;
      motor1.drive(0);
      motor2.drive(lfspeed);
    }
 
    else if (analogRead(5) > threshold[5] && analogRead(1) < threshold[1])
    { lsp = lfspeed; rsp = 0;
      motor1.drive(lfspeed);
      motor2.drive(0);
    }
    else if (analogRead(3) > threshold[3])
    {
      Kp = 0.0006 * (1000 - analogRead(3));
      Kd = 10 * Kp;
      //Ki = 0.0001;
      linefollow();
    }
  }
}
 
void linefollow()
{
  int error = (analogRead(2) - analogRead(4));
 
  P = error;
  I = I + error;
  D = error - previousError;
 
  PIDvalue = (Kp * P) + (Ki * I) + (Kd * D);
  previousError = error;
 
  lsp = lfspeed - PIDvalue;
  rsp = lfspeed + PIDvalue;
 
  if (lsp > 255) {
    lsp = 255;
  }
  if (lsp < 0) {
    lsp = 0;
  }
  if (rsp > 255) {
    rsp = 255;
  }
  if (rsp < 0) {
    rsp = 0;
  }
  motor1.drive(lsp);
  motor2.drive(rsp);
 
}
 
void calibrate()
{
  for ( int i = 1; i < 6; i++)
  {
    minValues[i] = analogRead(i);
    maxValues[i] = analogRead(i);
  }
  
  for (int i = 0; i < 3000; i++)
  {
    motor1.drive(50);
    motor2.drive(-50);
 
    for ( int i = 1; i < 6; i++)
    {
      if (analogRead(i) < minValues[i])
      {
        minValues[i] = analogRead(i);
 
      }
      if (analogRead(i) > maxValues[i])
      {
        maxValues[i] = analogRead(i);
      }
    }
  }
 
  for ( int i = 1; i < 6; i++)
  {
    threshold[i] = (minValues[i] + maxValues[i]) / 2;
    Serial.print(threshold[i]);
    Serial.print("   ");
  }
  Serial.println();
  
  motor1.drive(0);
  motor2.drive(0);
}

troubleshooting tips:

try reducing kp value ensure sensors are attached properly make sure battery used is not very heavy

other useful resources:

(1) writing a custom pid line following bot from scratch: https://robotresearchlab.com/2019/03/13/build-a-custom-pid-line-following-robot-from-scratch/ (2) arduino pid controller from scratch: https://www.youtube.com/watch?v=RZW1PsfgVEI&list=PLL4KRtNNj3-SZd7SdhVDUg7oHpgVxqZJw&index=5 (3) sensors review: https://www.youtube.com/watch?v=xysz2xh69rQ&list=PLL4KRtNNj3-SZd7SdhVDUg7oHpgVxqZJw&index=6 (4) more PID-control-systems theory: https://www.youtube.com/watch?v=UR0hOmjaHp0&list=PLL4KRtNNj3-SZd7SdhVDUg7oHpgVxqZJw&index=9 (5) MATLAB’s take on PID: https://www.youtube.com/watch?v=wkfEZmsQqiA&list=PLL4KRtNNj3-SZd7SdhVDUg7oHpgVxqZJw&index=18 (6) might be useful (might remove): https://www.youtube.com/watch?v=S4urA1Fv6Kw&list=PLL4KRtNNj3-SZd7SdhVDUg7oHpgVxqZJw&index=10 (7) looks useful: https://www.youtube.com/watch?v=LhVHGth-apM&list=PLL4KRtNNj3-SZd7SdhVDUg7oHpgVxqZJw&index=13 (8) pretty good: https://www.youtube.com/watch?v=LhVHGth-apM&list=PLL4KRtNNj3-SZd7SdhVDUg7oHpgVxqZJw&index=13