Skip to content
Sun, Oct 11, 2026 / Ideas for a clearer worldINDEPENDENT INSIGHTS Β· PRACTICAL TOOLS Β· EXPLAINERS
Sign in
Robot Building / May 26, 2026

How to Build a Line-Following Robot πŸš—πŸš¦

A line-following robot is a simple yet powerful project for robotics beginners. It follows a black line on a white surface (or vice versa) using infrared (IR) sensors. These robots are used in automated transport, warehouse automation, and robotic competitions!Contents1️⃣ How Does a Line-Following Robot Work? πŸ€–2️⃣ Components Required πŸ› οΈ3️⃣ Circuit Connections πŸ”§πŸ”Ή Connecting the […]

A line-following robot is a simple yet powerful project for robotics beginners. It follows a black line on a white surface (or vice versa) using infrared (IR) sensors. These robots are used in automated transport, warehouse automation, and robotic competitions!

In this guide, you’ll learn:

βœ… How a line-following robot works

βœ… Components required

βœ… Circuit connections

βœ… Writing the Arduino code

βœ… Testing & improving your robot

Let’s build one! πŸš€

Β 

1️⃣ How Does a Line-Following Robot Work? πŸ€–

A line-following robot uses IR sensors to detect a black line against a white background.

πŸ”Ή IR sensors emit infrared light and detect how much light is reflected.

πŸ”Ή White surfaces reflect more light (HIGH signal), while black surfaces absorb light (LOW signal).

πŸ”Ή The microcontroller (Arduino) reads sensor data and adjusts the motor direction accordingly.

πŸ“Œ Example:

  • Left sensor on white, right sensor on black β†’ Turn right
  • Both sensors on black β†’ Move forward
  • Both sensors on white β†’ Stop (line lost)

2️⃣ Components Required πŸ› οΈ

To build a line-following robot, you need:

πŸ”Ή Microcontroller – Arduino Uno (best for beginners)

πŸ”Ή Motor Driver Module – L298N (controls DC motors)

πŸ”Ή IR Sensors – TCRT5000 or IR sensor module (detects the line)

πŸ”Ή DC Motors (2x) – For movement

πŸ”Ή Chassis – The frame/body of the robot

πŸ”Ή Wheels (2x) & Caster Wheel (1x) – For stability

πŸ”Ή Battery Pack (9V or Li-ion) – Power supply

πŸ’‘ Pro Tip: Use rechargeable batteries for longer operation! πŸ”‹

Β 

3️⃣ Circuit Connections πŸ”§

πŸ”Ή Connecting the IR Sensors

IR Sensor PinArduino PinVCC5VGNDGNDLeft Sensor OUTA0Right Sensor OUTA1

πŸ“Œ IR sensors return:

  • 1 (HIGH) for white
  • 0 (LOW) for black

πŸ”Ή Connecting the Motor Driver (L298N) to Motors

L298N PinArduino PinIN18IN29IN310IN411ENA (PWM)6ENB (PWM)5

πŸ“Œ PWM pins (5,6) control speed; IN1-IN4 control direction.

Β 

4️⃣ Writing the Arduino Code πŸ’»

πŸ”Ή Basic Code for Line Following

cpp
----
#define leftSensor A0
#define rightSensor A1
#define leftMotor1 8
#define leftMotor2 9
#define rightMotor1 10
#define rightMotor2 11

void setup() {
  pinMode(leftSensor, INPUT);
  pinMode(rightSensor, INPUT);
  pinMode(leftMotor1, OUTPUT);
  pinMode(leftMotor2, OUTPUT);
  pinMode(rightMotor1, OUTPUT);
  pinMode(rightMotor2, OUTPUT);
}

void loop() {
  int leftValue = digitalRead(leftSensor);
  int rightValue = digitalRead(rightSensor);

  if (leftValue == 0 && rightValue == 0) { // Both sensors on black (move forward)
    moveForward();
  } 
  else if (leftValue == 1 && rightValue == 0) { // Left sensor on white, right sensor on black (turn right)
    turnRight();
  } 
  else if (leftValue == 0 && rightValue == 1) { // Right sensor on white, left sensor on black (turn left)
    turnLeft();
  } 
  else { // Both sensors on white (stop)
    stopRobot();
  }
}

void moveForward() {
  digitalWrite(leftMotor1, HIGH);
  digitalWrite(leftMotor2, LOW);
  digitalWrite(rightMotor1, HIGH);
  digitalWrite(rightMotor2, LOW);
}

void turnRight() {
  digitalWrite(leftMotor1, HIGH);
  digitalWrite(leftMotor2, LOW);
  digitalWrite(rightMotor1, LOW);
  digitalWrite(rightMotor2, LOW);
}

void turnLeft() {
  digitalWrite(leftMotor1, LOW);
  digitalWrite(leftMotor2, LOW);
  digitalWrite(rightMotor1, HIGH);
  digitalWrite(rightMotor2, LOW);
}

void stopRobot() {
  digitalWrite(leftMotor1, LOW);
  digitalWrite(leftMotor2, LOW);
  digitalWrite(rightMotor1, LOW);
  digitalWrite(rightMotor2, LOW);
}

5️⃣ Testing Your Line-Following Robot 🏎️

πŸ”Ή Step 1: Upload Code

  • Connect Arduino to your PC via USB.
  • Open Arduino IDE, select the correct board & port, and upload the code.

πŸ”Ή Step 2: Place the Robot on a Line

  • Draw a thick black line on a white surface (use black tape or a printed track).
  • Place the robot on the black line and power it ON.

πŸ”Ή Step 3: Observe the Movement

βœ… Moves forward when both sensors detect the line.

βœ… Turns left/right when the line is lost on one side.

βœ… Stops when both sensors lose the line.

πŸ“Œ Adjust IR sensor height for better accuracy.

Β 

6️⃣ How to Improve Your Robot πŸš€

πŸ”Ή Use More Sensors – A 3-5 sensor array improves precision.

πŸ”Ή Adjust Speed Using PWM – Modify analogWrite() for smoother turns.

πŸ”Ή Use PID Control – Implement Proportional-Integral-Derivative (PID) for better tracking.

πŸ”Ή Add Bluetooth – Control the robot via a smartphone.

πŸ”Ή Make It Faster! – Use lighter materials and stronger motors.

πŸ’‘ Advanced Upgrade: Add a camera (Raspberry Pi + OpenCV) for AI-based line tracking!

Β 

Final Thoughts πŸ’‘

Building a line-following robot is a great way to learn robotics! With IR sensors, motor control, and programming, you develop real-world automation skills.