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 white0(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.


