Skip to Content


info@hub360.com.ng 07044715478

LINE FOLLOWING ROBOT CAR

February 18, 2025 by
LINE FOLLOWING ROBOT CAR
My Company (Chicago), Hub360
| No comments yet

The Line-Following Robot is a foundational project in robotics that brings to life the principles of autonomous navigation and sensor-based control. Designed to follow a predefined path—typically a black line on a white surface or vice versa—this robot uses infrared (IR) sensors to detect the line’s edges and an Arduino microcontroller to interpret sensor data and command its motors. By integrating an L298N motor driver and two DC motors, the robot dynamically adjusts its direction, moving forward, turning left or right, or stopping entirely based on real-time feedback from its environment.

This project is not just a fun DIY build but also an educational gateway into robotics, offering hands-on experience with sensor integration, motor control, and feedback loops. Line-following robots have real-world applications in automated warehouses, industrial transport systems, and even smart vacuum cleaners, making this project a practical introduction to technologies that shape modern automation. Whether you’re a student, hobbyist, or tech enthusiast, building this robot teaches critical problem-solving skills while showcasing the power of simple yet intelligent systems.

Components Required

  • Arduino Uno
  • 2WD Motor Chassis and accessories
  • L298N Motor Driver Module
  • IR Line Sensors
  • Power Supply
  • Breadboard 
  • Jumper Wires

Circuit Diagram

1. Connect the IR Sensors to Arduino:  

  • Left Sensor VCC → 5V on Arduino 
  • Left Sensor GND → GND on Arduino 
  • Left Sensor OUT → A0 on Arduino 
  • Right Sensor VCC → 5V on Arduino 
  • Right Sensor GND → GND on Arduino 
  • Right Sensor OUT → A1 on Arduino 

2. Connect the L298N Motor Driver to Arduino:  

  • IN1, IN2, IN3, IN4 → Arduino Digital Pins 6, 7, 8, 9 
  • Motor A Outputs → Left Motor Terminals 
  • Motor B Outputs → Right Motor Terminals 
  • 12V Power (from battery) → L298N 12V Input 
  • GND of Battery → L298N  GND and Arduino GND 

3. Connect the Power Supply:  

  • Use a 9V or 12V Battery 
  • Connect Battery + to Vin of Arduino and 12V of L298N 
  • Connect Battery - to GND of L298N and Arduino ​



CODE

#define LEFT_SENSOR A0  

#define RIGHT_SENSOR A1  

#define IN1 6  

#define IN2 7  

#define IN3 8  

#define IN4 9 


void setup() {  

​pinMode(LEFT_SENSOR, INPUT);  

​pinMode(RIGHT_SENSOR, INPUT);  

​pinMode(IN1, OUTPUT);  

​pinMode(IN2, OUTPUT); 

​pinMode(IN3, OUTPUT); 

​pinMode(IN4, OUTPUT);  

}

  

void loop() { 

​int leftValue = digitalRead(LEFT_SENSOR);  

​int rightValue = digitalRead(RIGHT_SENSOR);  

​if (leftValue == 0 && rightValue == 0) { // Both sensors on black (Move  Forward)  

​ digitalWrite(IN1, HIGH);  

​digitalWrite(IN2, LOW);

​digitalWrite(IN3, HIGH);

​digitalWrite(IN4, LOW);  

​ }

​else if (leftValue == 0 && rightValue == 1) { // Left sensor on black,  right on white (Turn Left)

​digitalWrite(IN1, LOW); 

​digitalWrite(IN2, HIGH);

​digitalWrite(IN3, HIGH);  

​digitalWrite(IN4, LOW);  

​ }

​ else if (leftValue == 1 && rightValue == 0) { // Right sensor on  black, left on white (Turn Right)

​digitalWrite(IN1, HIGH); 

​digitalWrite(IN2, LOW);

​digitalWrite(IN3, LOW);  

​digitalWrite(IN4, HIGH);  

​ }

​else { // Both sensors on white (Stop)  

​digitalWrite(IN1, LOW);

​digitalWrite(IN2, LOW);

​digitalWrite(IN3, LOW);  

​digitalWrite(IN4, LOW);  

​} 

}

CODE EXPLANATION

1. Pin Definitions

#define LEFT_SENSOR A0  
#define RIGHT_SENSOR A1  
#define IN1 6  
#define IN2 7  
#define IN3 8  
#define IN4 9  
  • Purpose: Defines the pins for the IR sensors and motor driver inputs.
    • LEFT_SENSOR and RIGHT_SENSOR are connected to analog pins A0 and A1.
    • IN1, IN2, IN3, and IN4 are connected to digital pins 6, 7, 8, and 9 to control the motors.

2. setup() Function

void setup() {

  pinMode(LEFT_SENSOR, INPUT); 

  pinMode(RIGHT_SENSOR, INPUT); 

  pinMode(IN1, OUTPUT); 

  pinMode(IN2, OUTPUT); 

  pinMode(IN3, OUTPUT); 

  pinMode(IN4, OUTPUT); 

}

  • Purpose: Initializes the pins for sensors and motor control.
    • Sets LEFT_SENSOR and RIGHT_SENSOR as inputs to read sensor data.
    • Sets IN1, IN2, IN3, and IN4

3. loop() Function

The loop() function continuously reads sensor data and adjusts motor behavior based on the line’s position.

a. Reading Sensor Values

int leftValue = digitalRead(LEFT_SENSOR);  
int rightValue = digitalRead(RIGHT_SENSOR); 
  • Purpose: Reads the digital values from the left and right IR sensors.
    • 0: Sensor detects a black line (low reflection).
    • 1: Sensor detects a white surface (high reflection).

b. Decision-Making Logic

The robot’s behavior is determined by the sensor readings:

  • Move Forward (Both sensors detect black):
if (leftValue == 0 && rightValue == 0) {  
  digitalWrite(IN1, HIGH); 
  digitalWrite(IN2, LOW); 
  digitalWrite(IN3, HIGH); 
  digitalWrite(IN4, LOW); 
}
    • Both motors rotate forward.
  • Turn Left (Left sensor detects black, right detects white):
else if (leftValue == 0 && rightValue == 1) {  
  digitalWrite(IN1, LOW); 
  digitalWrite(IN2, HIGH); 
  digitalWrite(IN3, HIGH); 
  digitalWrite(IN4, LOW); 
}
    • Right motor moves forward, left motor reverses or stops, causing a left turn.
  • Turn Right (Right sensor detects black, left detects white):
else if (leftValue == 1 && rightValue == 0) {  
  digitalWrite(IN1, HIGH); 
  digitalWrite(IN2, LOW); 
  digitalWrite(IN3, LOW); 
  digitalWrite(IN4, HIGH); 
}
    • Left motor moves forward, right motor reverses or stops, causing a right turn.
  • Stop (Both sensors detect white):
else {  
  digitalWrite(IN1, LOW); 
  digitalWrite(IN2, LOW); 
  digitalWrite(IN3, LOW); 
  digitalWrite(IN4, LOW); 
}
    • Both motors stop.

How It Works

  1. The IR sensors detect the line’s position by measuring reflected light.
  2. The Arduino reads the sensor values and decides the robot’s movement:
    • Forward: Both sensors on the line.
    • Left Turn: Left sensor on the line, right sensor off.
    • Right Turn: Right sensor on the line, left sensor off.
    • Stop: Both sensors off the line.
  3. The motor driver executes the Arduino’s commands, adjusting the robot’s direction.

OBSERVING FUNCTIONALITY

  1. Place the robot on a black line with a white background (or vice versa, depending  on sensor type). 
  2. The IR sensors detect the line and send signals to the Arduino.  
  3. The Arduino processes the sensor data and adjusts the motor driver to move the car:  
    • Forward → Both sensors detect black.  
    • Left Turn → Left sensor detects black, right sensor detects white. 
    • Right Turn → Right sensor detects black, left sensor  detects white. 
    • Stop → Both sensors detect white. 

COMMON PROBLEMS AND SOLUTIONS

  1. Robot Not Following the Line Properly: 
  • Adjust the sensor  placement to ensure they are correctly aligned with the track.  
  • Ensure the sensor output is correctly mapped (some sensors give  HIGH on black, others LOW).  

 2. Robot Moves Randomly or Stalls:

  • Check if the IR  sensors are receiving proper 5V power.  
  • Use a multimeter to verify sensor outputs when detecting black/white. 

3. Motors Not Running 

  • Ensure the motor driver (L298N) is  properly powered. 
  • Confirm IN1, IN2, IN3, and IN4  connections to Arduino.

Conclusion

The Line-Following Robot exemplifies how basic components like IR sensors, a motor driver, and an Arduino can come together to create a smart, responsive machine. By mastering this project, you’ve not only built a robot that navigates a path autonomously but also gained insight into key robotics concepts such as sensor calibration, closed-loop control, and real-time decision-making.

This robot is just the beginning. With a few upgrades—like adding more sensors for obstacle detection, integrating Bluetooth for remote control, or programming advanced algorithms for complex paths—you can transform it into a versatile platform for exploring AI and IoT. Imagine a robot that maps its environment, avoids obstacles, or even competes in line-following competitions!

The skills you’ve honed here—circuit design, coding logic, and troubleshooting—are the building blocks of robotics and automation. So, whether you’re refining this robot’s precision or diving into more ambitious projects, remember: every line followed is a step toward mastering the future of technology. Ready to take the next turn? Power up your Arduino, tweak the code, and let your creativity drive innovation! 


Comments

Share this post
Tags
Archive
Sign in to leave a comment