Project Banao Line Follower Robot Car Kit with Arduino UNO, IR Sensor & 4WD Motor Drive | DIY Automatic Line Tracking Robot | STEM Robotics & Electronics Project Kit for School Science Exhibition

–

by

in

const byte IR_PIN = 2;
const byte ENA = 5, ENB = 6;
const byte IN1 = 8, IN2 = 9, IN3 = 10, IN4 = 11;
const bool BLACK_LEVEL = LOW; // Confirm on your sensor
const byte CORRECT_SPEED = 180;

void motors(byte leftSpeed, byte rightSpeed) {
  digitalWrite(IN1, leftSpeed > 0);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, rightSpeed > 0);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, leftSpeed);
  analogWrite(ENB, rightSpeed);
}

void setup() {
  pinMode(IR_PIN, INPUT);
  pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT); pinMode(ENB, OUTPUT);
  motors(0, 0);
}

void loop() {
  bool onBlack = (digitalRead(IR_PIN) == BLACK_LEVEL);

  // Sensor is mounted over the LEFT edge of the line.
  // Black: turn left toward white; white: turn right toward line.
  if (onBlack) motors(0, CORRECT_SPEED);
  else         motors(CORRECT_SPEED, 0);
  delay(20);
}

Leave a Reply

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