Line-following robot is one of the most popular, simple, and engaging projects for beginners in electronics and programming. This robot works using infrared (IR) sensors to detect black or white lines on the ground, and it controls the motors so the robot can automatically follow a predefined path. Through this project, you will learn how to solder components, assemble mechanical parts, wire the control circuit, and program the Arduino to operate the robot.
Components & Materials:
Tools:
Software application:
Step 1: Preparing the Motors
-----------------------------------------------------------------
Step 2: Fabricating the Chassis
-----------------------------------------------------------------
Step 3: Installing the Control Circuit
-----------------------------------------------------------------
Step 4: Attaching the IR Sensors
-----------------------------------------------------------------
Step 5: Power Supply
-----------------------------------------------------------------
Step 6: Wiring
The line-following robot works by using IR sensors to detect black lines on a white surface. When a sensor detects a line (returns 1), the Arduino adjusts the speed and rotation direction of the two motors, allowing the robot to go straight, turn left, or turn right, keeping it following the path.
// Line Follower Robot using Adafruit Motor Shield V1
// Adafruit Motor Shield V1 library (install from Library Manager)
#include <AFMotor.h>
// Declare IR sensor pins
#define leftSensor A0 // Left IR sensor connected to A0
#define rightSensor A1 // Right IR sensor connected to A1
// Initialize two motors
AF_DCMotor motor1(1, MOTOR12_1KHZ); // Left motor, M1 port
AF_DCMotor motor2(4, MOTOR12_1KHZ); // Right motor, M4 port
void setup() {
pinMode(leftSensor, INPUT); // Set left sensor as input
pinMode(rightSensor, INPUT); // Set right sensor as input
Serial.begin(9600); // Enable Serial Monitor for debugging
}
// Store previous sensor state to avoid sending duplicate motor commands
int prevLeft = -1;
int prevRight = -1;
void loop() {
int left = digitalRead(leftSensor); // Read left sensor (0 = white, 1 = black)
int right = digitalRead(rightSensor); // Read right sensor (0 = white, 1 = black)
// Only process when sensor state changes
if (left != prevLeft || right != prevRight) {
prevLeft = left;
prevRight = right;
// Case 1: Both sensors on white (0,0) → line in the middle → go straight
if (left == 0 && right == 0) {
motor1.setSpeed(110); // Left motor speed
motor1.run(FORWARD); // Left motor forward
motor2.setSpeed(110); // Right motor speed
motor2.run(FORWARD); // Right motor forward
}
// Case 2: Left=0, Right=1 → line touches right sensor → drift right → turn left
else if (left == 0 && right == 1) {
motor1.setSpeed(110);
motor1.run(FORWARD); // Left motor forward
motor2.setSpeed(110);
motor2.run(BACKWARD); // Right motor backward → rotate left
}
// Case 3: Left=1, Right=0 → line touches left sensor → drift left → turn right
else if (left == 1 && right == 0) {
motor1.setSpeed(110);
motor1.run(BACKWARD); // Left motor backward
motor2.setSpeed(110);
motor2.run(FORWARD); // Right motor forward → rotate right
}
}
delay(15); // Pause 15ms before next loop → reduce noise, allow motor reaction
}