Building a Line Follower Robot: Arduino Guide
This guide will show you how to build a line follower robot using Arduino, an L293D motor driver, BO motors, and three IR sensors. The Arduino code provided enables the robot to detect a line and follow it autonomously, making real-time adjustments to its direction.
Components Needed
- Arduino Uno - Microcontroller for controlling the robot’s movements.
- IR Sensor Modules (3x) - Detects the line on the surface (left, center, right sensors).
- DC Motors (2x) - Drives the robot’s wheels.
- Motor Driver (L293D) - Interfaces Arduino with the motors.
- Chassis - The structure holding all components.
- Battery Pack - Powers the robot.
- Wheels and Wires - For movement and connections.
Circuit Diagram
Follow the circuit diagram below to connect the Arduino, motor driver (L293D), BO motors, and IR sensors:
Pin Connections
- Motor Driver (L293D):
- IN1 (Pin 6) to Arduino pin 6, IN2 (Pin 7) to Arduino pin 7.
- IN3 (Pin 8) to Arduino pin 8, IN4 (Pin 9) to Arduino pin 9.
- ENA to Arduino pin 5, ENB to Arduino pin 10.
- IR Sensors:
- Left (L_S) to A2, Center (S_S) to A1, Right (R_S) to A0.
Arduino Code
Below is the Arduino code for the line follower robot. Copy this code into your Arduino IDE and upload it to your Arduino Uno board:
int S_A = 5; // Speed for motor A
int M_A1 = 6; // Motor A forward
int M_A2 = 7; // Motor A backward
int M_B1 = 8; // Motor B backward
int M_B2 = 9; // Motor B forward
int S_B = 10; // Speed for motor B
int R_S = A0; // Right sensor
int S_S = A1; // Center sensor
int L_S = A2; // Left sensor
void setup() {
pinMode(M_B1, OUTPUT);
pinMode(M_B2, OUTPUT);
pinMode(M_A1, OUTPUT);
pinMode(M_A2, OUTPUT);
pinMode(S_B, OUTPUT);
pinMode(S_A, OUTPUT);
pinMode(L_S, INPUT);
pinMode(S_S, INPUT);
pinMode(R_S, INPUT);
analogWrite(S_A, 230);
analogWrite(S_B, 230);
delay(200);
}
void loop() {
if ((digitalRead(L_S) == 0) && (digitalRead(S_S) == 1) && (digitalRead(R_S) == 0)) {
forward();
}
if ((digitalRead(L_S) == 1) && (digitalRead(S_S) == 1) && (digitalRead(R_S) == 0)) {
SturnLeft();
}
if ((digitalRead(L_S) == 1) && (digitalRead(S_S) == 0) && (digitalRead(R_S) == 0)) {
turnLeft();
}
if ((digitalRead(L_S) == 0) && (digitalRead(S_S) == 1) && (digitalRead(R_S) == 1)) {
SturnRight();
}
if ((digitalRead(L_S) == 0) && (digitalRead(S_S) == 0) && (digitalRead(R_S) == 1)) {
turnRight();
}
if ((digitalRead(L_S) == 1) && (digitalRead(S_S) == 1) && (digitalRead(R_S) == 1)) {
Stop();
}
}
void forward() {
digitalWrite(M_A1, HIGH);
digitalWrite(M_A2, LOW);
digitalWrite(M_B1, HIGH);
digitalWrite(M_B2, LOW);
}
void SturnRight() {
digitalWrite(M_A1, LOW);
digitalWrite(M_A2, HIGH);
digitalWrite(M_B1, HIGH);
digitalWrite(M_B2, LOW);
}
void turnRight() {
digitalWrite(M_A1, LOW);
digitalWrite(M_A2, LOW);
digitalWrite(M_B1, HIGH);
digitalWrite(M_B2, LOW);
}
void SturnLeft() {
digitalWrite(M_A1, HIGH);
digitalWrite(M_A2, LOW);
digitalWrite(M_B1, LOW);
digitalWrite(M_B2, HIGH);
}
void turnLeft() {
digitalWrite(M_A1, HIGH);
digitalWrite(M_A2, LOW);
digitalWrite(M_B1, LOW);
digitalWrite(M_B2, LOW);
}
void Stop() {
digitalWrite(M_A1, LOW);
digitalWrite(M_A2, LOW);
digitalWrite(M_B1, LOW);
digitalWrite(M_B2, LOW);
}
Testing the Robot
Place your robot on a track with a dark line on a light surface. It should follow the line automatically, adjusting left or right as needed. If the robot doesn’t perform as expected, check sensor alignment and ensure the IR sensors are properly detecting the line.
Conclusion
With this guide, you've successfully built and programmed a line follower robot. This project is a fantastic way to understand autonomous movement and sensor-based navigation. Experiment with different line patterns, speeds, and turning angles to improve the robot’s performance. Enjoy your creation!
Tip:Dont buy the componnets from amazon or flipkart instead use makerbazzar , robu ,electronicscomp. but thet takes time upto 7 to 15 days