Line Following Robot Using Arduino
Line Following Robot Using Arduino

Project Description
Creating a line follower robot using an Arduino is a popular robotics project. It uses infrared (IR) sensors to detect and follow a line on the ground, typically a black line on a white surface, or vice versa. Below is a step-by-step guide to help you build a simple line follower robot using an Arduino, IR sensors, and a motor driver (like L293D)
Components and supplies
Step 1

Now let's see how we can make this project. First of all, you will need a chassis to make this project. I've designed a chassis in Autodesk Fusion 360, and if you have a 3D printer, don't hesitate to download and print it. Let's assume that you have something to mount the motors. Now it's time to solder the electrical wire to the motors. The motor will have 2 terminals, you don't need to differentiate which is the positive and which is the negative because it is only used to reverse the motor.
After soldering 4 motors as the picture above. Let's fix the mounting bracket to the motors using 8 M3x40 mm:
Step 2

You will need to buy the mounting bracket to mount the motors to the 3D printed chassis.
Step 3

The next step is to attach the Arduino board to the chassis using M3 bolts and nuts. You can also use doubled side adhesive tape if you want to.
Attach the wheels to the motors. Everyone can do it by themselves.
Step 4

Next step is to connect the L293D motor driver to the Arduino Uno R3.
The L293D is a dual H-Bridge motor driver IC that can control the direction and speed of two DC motors. And it can be connected directly to the Arduino.
Step 5

Wiring setup for 4 motors. Follow the videos for more details. Connect the IR sensors for light detection You will need two IR sensors (left and right) placed at the front of the robot. These sensors will detect whether the robot is on the line or off the line.
IR sensor modules typically have:
- VCC: Power supply (usually 5V).
- GND: Ground.
- OUT: Output (HIGH or LOW depending on whether the sensor detects the line)
The OUT of the sensor on the left will be connected to the A0 and the other one will be connected to the A1.
GND and VCC will be fixed to the 0V and 5V of Arduino respectively.

That's all for the set up!
Code
//Line Follower Robot
//first , you need to insstall the motor driver for L293D motor driver
// Go to Sketch-> Include Librabry-> Manage Library-> Search for Adafruit Motor Shield V1
#include
#define left A0
#define right A1
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
void setup() {
pinMode(left,INPUT);
pinMode(right,INPUT);
Serial.begin(9600);
}
void loop(){
Serial.println(digitalRead(left));
Serial.println(digitalRead(right));
if(digitalRead(left)==0 && digitalRead(right)==0){
// Move forward
motor1.run(FORWARD);
motor1.setSpeed(150); // Run Speed from 0 to 255
motor2.run(FORWARD);
motor2.setSpeed(150);// Run Speed from 0 to 255
motor3.run(FORWARD);
motor3.setSpeed(150);// Run Speed from 0 to 255
motor4.run(FORWARD);
motor4.setSpeed(150);// Run Speed from 0 to 255
}
//Compare to sensors
else if(digitalRead(left)==0 && !analogRead(right)==0){
//turn right
motor1.run(FORWARD);
motor1.setSpeed(150);
motor2.run(FORWARD);
motor2.setSpeed(150);
motor3.run(BACKWARD);
motor3.setSpeed(150);
motor4.run(BACKWARD);
motor4.setSpeed(150);
}
//Compare to sensors
else if(!digitalRead(left)==0 && digitalRead(right)==0){
//Turn left
motor1.run(BACKWARD);
motor1.setSpeed(150);
motor2.run(BACKWARD);
motor2.setSpeed(150);
motor3.run(FORWARD);
motor3.setSpeed(150);
motor4.run(FORWARD);
motor4.setSpeed(150);
}
//No signals
else if(!digitalRead(left)==0 && !digitalRead(right)==0){
//Stop
motor1.run(RELEASE);
motor1.setSpeed(0);
motor2.run(RELEASE);
motor2.setSpeed(0);
motor3.run(RELEASE);
motor3.setSpeed(0);
motor4.run(RELEASE);
motor4.setSpeed(0);
}
}