ElectroMartBDElectroMartBD

Build a Line Follower Robot

Line follower robots are used in almost every school and university robotics competition. Two IR sensors look at the floor: a black line reflects less light than white paper, so the Arduino steers to keep the line between the sensors.

1. Wiring

  • Left IR OUT → D2, right IR OUT → D4 (VCC → 5V, GND → GND)
  • TB6612FNG: PWMA → D5, AIN1 → D7, AIN2 → D8, PWMB → D6, BIN1 → D9, BIN2 → D10, STBY → 5V
  • VM → battery +, VCC → 5V, all GNDs together

2. The Code

const int SPEED = 150;  // 0-255

void motors(int l, int r) {
  digitalWrite(7, l > 0); digitalWrite(8, l < 0); analogWrite(5, abs(l));
  digitalWrite(9, r > 0); digitalWrite(10, r < 0); analogWrite(6, abs(r));
}

void setup() {
  for (int p : {5, 6, 7, 8, 9, 10}) pinMode(p, OUTPUT);
  pinMode(2, INPUT); pinMode(4, INPUT);
}

void loop() {
  bool L = digitalRead(2);  // HIGH = sees black on most modules
  bool R = digitalRead(4);
  if (!L && !R) motors(SPEED, SPEED);     // line in the middle
  else if (L && !R) motors(0, SPEED);     // turn left
  else if (!L && R) motors(SPEED, 0);     // turn right
  else motors(0, 0);                      // stop / junction
}

3. Tips

  • Adjust the blue knob on each IR module so the LED switches exactly at the line edge.
  • Mount sensors 5–10 mm above the floor.
  • If it turns the wrong way, swap the L and R sensor wires.

Need help? Stuck on wiring or code? Message us on WhatsApp at +880 1577-098376 and we will help you with your project.