Line Following CAR
Have you ever wondered how robots can follow a path without human control? A line-following robot is one of the best beginner projects in robotics and embedded systems. It uses infrared (IR) sensors to detect a black line on a white surface (or vice versa) and automatically adjusts its movement to stay on the path.
In this project, I built a simple line-following robot using an Arduino Uno, two IR sensors, an L298N motor driver, and two DC motors. This project is ideal for students learning Arduino, robotics, and automation.
Supplies
Arduino Uno
L298N Motor Driver Module
2 × IR Line Tracking Sensors
2 × BO DC Motors
Robot Chassis
2 × Wheels
Caster Wheel
9V/12V Battery (or 2 × 18650 batteries)
Battery Holder
Jumper Wires
Switch
How It Works
The robot uses two IR sensors:
- Left IR Sensor
- Right IR Sensor
The sensors continuously detect the surface beneath them.
Black Surface
The sensor outputs LOW (0).
White Surface
The sensor outputs HIGH (1).
The Arduino reads these values and controls the motors.
Left SensorRight SensorRobot Action
Black
Black
Move Forward
Black
White
Turn Left
White
Black
Turn Right
White
White
Stop/Search
Circuit Diagram
IR Sensors
Left Sensor OUT → Arduino D2
Right Sensor OUT → Arduino D3
VCC → 5V
GND → GND
L298N Connections
IN1 → D8
IN2 → D9
IN3 → D10
IN4 → D11
ENA → PWM D5
ENB → PWM D6
Battery → L298N Power Input
Arduino GND → L298N GND
Assemble the Robot
Fix both DC motors to the chassis.
Attach the wheels.
Mount the caster wheel.
Install the Arduino.
Fix the L298N module.
Place the IR sensors at the front.
Keep both sensors about 5–10 mm above the ground.
Connect all wires.
Upload the Arduino Code
Upload the line-following program using the Arduino IDE.
code
// ===============================
// Line Following Robot
// Arduino UNO + L298N + 2 IR Sensors
// Black Line = LOW
// White Surface = HIGH
// ===============================
// -------- Motor Driver Pins --------
#define ENA 5
#define IN1 8
#define IN2 9
#define ENB 6
#define IN3 10
#define IN4 11
// -------- IR Sensor Pins --------
#define LEFT_SENSOR 2
#define RIGHT_SENSOR 3
// -------- Motor Speed --------
int speedA = 100;
int speedB = 100;
void setup()
{
pinMode(LEFT_SENSOR, INPUT);
pinMode(RIGHT_SENSOR, INPUT);
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int left = digitalRead(LEFT_SENSOR);
int right = digitalRead(RIGHT_SENSOR);
Serial.print("Left: ");
Serial.print(left);
Serial.print(" Right: ");
Serial.println(right);
// ===============================
// Black Line = LOW
// White = HIGH
// ===============================
if (left == LOW && right == LOW)
{
forward();
}
else if (left == LOW && right == HIGH)
{
turnLeft();
}
else if (left == HIGH && right == LOW)
{
turnRight();
}
else
{
stopRobot();
}
}
// ===============================
// Move Forward
// ===============================
void forward()
{
analogWrite(ENA, speedA);
analogWrite(ENB, speedB);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
// ===============================
// Turn Left
// ===============================
void turnLeft()
{
analogWrite(ENA, 70);
analogWrite(ENB, 110);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
// ===============================
// Turn Right
// ===============================
void turnRight()
{
analogWrite(ENA, 110);
analogWrite(ENB, 70);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
// ===============================
// Stop Robot
// ===============================
void stopRobot()
{
analogWrite(ENA, 0);
analogWrite(ENB, 0);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
Make sure to:
- Select the correct board.
- Select the correct COM port.
- Upload the code.
Test the Robot
Create a track using black electrical tape on a white chart paper.
Power on the robot.
The robot should:
- Move forward on the line
- Turn left at left curves
- Turn right at right curves
- Continue following the path automatically
Troubleshooting
Robot moves in the wrong direction
Swap the motor wires.
Robot doesn't detect the line
Adjust the IR sensor height.
Use the onboard potentiometer to calibrate the sensor.
Robot keeps spinning
Check sensor wiring.
Verify that the left and right motors are connected correctly.
Robot stops frequently
Increase motor speed slightly in the code.
Recharge the battery.
Applications
Warehouse automation
Industrial material transport
Hospital delivery robots
Automated guided vehicles (AGVs)
Educational robotics
Robotics competitions
Future Improvements
- Add PID control for smoother movement.
- Use more IR sensors for better accuracy.
- Add Bluetooth control.
- Integrate obstacle detection using an ultrasonic sensor.
- Implement speed control based on turns.
- Upgrade to ESP32 for IoT monitoring.