Header Ads Widget

How to make Arduino line following robot

 In this tutorial, we will make a line following robot that is controlled by an Arduino. This robot will be able to follow a line on the floor or ceiling.

The first step is to take the breadboard and solder wires to each row of holes. It is important to note that some rows are connected together so make sure that you are not accidentally soldering two rows together.

Next, you need to place your sensors on the breadboard so they are aligned with the lines on the floor or ceiling. The sensors should be placed in such a way that when they detect a black line, they are touching both lines of the sensor's row (one black and one white). When they detect white lines, they should only be touching one row of the sensor's column (either black or white).

Finally, connect your Arduino and upload your code!

How to make the Arduino line following the robot circuit:



How to make the Arduino line following the robot code:

#define in1 9
#define in2 8
#define in3 7
#define in4 6
#define enA 10
#define enB 5


int M1_Speed = 120; // speed of motor 1
int M2_Speed = 120; // speed of motor 2
int LeftRotationSpeed = 250;  // Left Rotation Speed
int RightRotationSpeed = 250; // Right Rotation Speed


void setup() {

  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);

  pinMode(A1, INPUT); // initialize Left sensor as an input
  pinMode(A0, INPUT); // initialize Right sensor as an input
  Serial.begin(9600);

  //analogWrite(enA, M1_Speed); // Write The Duty Cycle 0 to 255 Enable Pin A for Motor1 Speed
  //analogWrite(enB, M2_Speed); // Write The Duty Cycle 0 to 255 Enable Pin B for Motor2 Speed
  delay(1000);

}

void loop() {

  int LEFT_SENSOR = digitalRead(A0);
  int RIGHT_SENSOR = digitalRead(A1);

  if (RIGHT_SENSOR == 0 && LEFT_SENSOR == 0) {
    forward(); //FORWARD
    Serial.print("l:");
    Serial.println(LEFT_SENSOR);
    Serial.print("r:");
    Serial.println(RIGHT_SENSOR);
    Serial.println("direction:forward");
   // delay(1000);
  }

  else if (RIGHT_SENSOR == 1 && LEFT_SENSOR == 0) {
    right(); //Move Right
    Serial.print("l:");
    Serial.println(LEFT_SENSOR);
    Serial.print("r:");
    Serial.println(RIGHT_SENSOR);
    Serial.println("direction:r");
   // delay(1000);
  }

  else if (RIGHT_SENSOR == 0 && LEFT_SENSOR == 1) {
    left(); //Move Left
    Serial.print("l:");
    Serial.println(LEFT_SENSOR);
    Serial.print("r:");
    Serial.println(RIGHT_SENSOR);
    Serial.println("direction:l");
   // delay(1000);
  }

  else if (RIGHT_SENSOR == 1 && LEFT_SENSOR == 1) {
    Stop();  //STOP
    Serial.print("l:");
    Serial.println(LEFT_SENSOR);
    Serial.print("r:");
    Serial.println(RIGHT_SENSOR);
    Serial.println("direction:s");
   // delay(1000);
  }
}



void forward()
{
  digitalWrite(in1, LOW); //Right Motor forword Pin
digitalWrite(in2, HIGH);  //Right Motor backword Pin
digitalWrite(in3, HIGH);  //Left Motor backword Pin
digitalWrite(in4, LOW); //Left Motor forword Pin


  analogWrite(enA, M1_Speed);
  analogWrite(enB, M2_Speed);
}

void backward()
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);

  analogWrite(enA, M1_Speed);
  analogWrite(enB, M2_Speed);
}

void right()
{
 digitalWrite(in1, LOW);  //Right Motor forword Pin
digitalWrite(in2, HIGH); //Right Motor backword Pin  
digitalWrite(in3, LOW);  //Left Motor backword Pin
digitalWrite(in4, HIGH); //Left Motor forword Pin



  analogWrite(enA, LeftRotationSpeed);
  analogWrite(enB, RightRotationSpeed);
}

void left()
{
  digitalWrite(in1, HIGH); //Right Motor forword Pin
  digitalWrite(in2, LOW);  //Right Motor backword Pin
  digitalWrite(in3, HIGH); //Left Motor backword Pin
  digitalWrite(in4, LOW);  //Left Motor forword Pin


  analogWrite(enA, LeftRotationSpeed);
  analogWrite(enB, RightRotationSpeed);
}

void Stop()
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

Post a Comment

0 Comments