How to make a line following robot using an Arduino Nano board

How to make a line following robot using an Arduino Nano board

Hello and welcome back. In this project, we will learn how to make a line-following robot using an Arduino Nano board. This robot is designed to follow a black line on a white surface using infrared sensors. For this project, I mainly used two N20 DC gear motors. Because of these motors, we can get smooth and stable movement.

I used two TCRT5000 infrared line-tracking sensor modules to detect the line. These sensors can easily detect the difference between black and white surfaces and send the signal to the Arduino. Also, I have added two LEDs to indicate the black line. For this project, I designed a custom PCB, which also works as the chassis of this robot. Because of this PCB design, we do not need extra wires or a separate chassis, and the robot looks clean and compact.

Also, I used two Li-ion batteries to power up this robot. However, you can also build this project using a 9V battery if needed.

Ok, let’s do this project step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, let’s order PCBs for this project.

  • Click the “Instant Quote” button and upload the Gerber file, which you can download from the link below.
  • Gerber file – Download
  • For this project, I ordered five Purple PCBs. Next, select the build time and shipping method. Finally, click “Save to Cart” and complete the payment.

Step 3

Next, carefully unbox the PCB package.

Step 4

Now, solder all the components onto the PCB.

Step 5

Afterward, solder the wires to the gear motors. Then, attach the wheels to the motors and mount them onto the chassis. For this step, I used N20 motor brackets.

Step 6

Next, connect the motors to the motor terminals. Then, attach the front caster wheel to the PCB.

Step 7

Now, connect the Arduino Nano board, the L293D IC, sensors, and battery holder to the robot properly. Afterward, connect the Arduino board to the computer.

Step 8

Next, copy and paste the following program into the Arduino IDE.

// ===== IR Sensors =====
#define Sleft  3
#define Sright 2

// ===== LEDs =====
#define LEDleft  12
#define LEDright 11

// ===== Motor Driver =====
#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 10

// ===== Speed Settings =====
#define MOTOR_SPEED 90  // PWM speed (0-255)

// =========================
void setup() {
  // Sensor pins
  pinMode(Sleft, INPUT);
  pinMode(Sright, INPUT);

  // LEDs
  pinMode(LEDleft, OUTPUT);
  pinMode(LEDright, OUTPUT);

  // Motor pins
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);

  // Set motor speed
  analogWrite(ENA, MOTOR_SPEED);
  analogWrite(ENB, MOTOR_SPEED);

  Stop(); // Safety stop
}

// =========================
void loop() {
  int L = digitalRead(Sleft);
  int R = digitalRead(Sright);

  // LEDs ON when BLACK line detected
  digitalWrite(LEDleft,  L);
  digitalWrite(LEDright, R);

  // ===== Line Following Logic =====
  if (L == LOW && R == LOW) {
    Forward();      // Both on line → straight
  }
  else if (L == HIGH && R == LOW) {
    Left();         // Line under LEFT sensor → turn RIGHT physically
  }
  else if (L == LOW && R == HIGH) {
    Right();        // Line under RIGHT sensor → turn LEFT physically
  }
  else if (L == HIGH && R == HIGH) {
    Stop();         // Both sensors off line → stop
  }
}

// ===== Motor Functions (SLOW + SWAPPED) =====
void Forward() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void Left() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);   // left motor forward
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);    // right motor stop
}

void Right() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);    // left motor stop
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);   // right motor forward
}

void Stop() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}
  • Now, select the board and port. After that, click the upload button.

Step 9

Finally, remove the USB cable and insert the batteries into the battery holder. Now you can test your robot on a black track on a white surface.

How to make a line following robot using an Arduino Nano board

Similar Posts

Leave a Reply

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