LINE FOLLOWER ROBOT USING ARDUINO
AIM: Design a line Following Robot Using Arduino Uno .
APPARATUS:
• Arduino UNO
• DC Motor
• IR sensor *2
• Motor Driver L298N
• 12 Volt Battery
• Battery holder
• Robot Chasis
• Connecting wires
CIRCUIT DIAGRAM:
PROCEDURE:-
• Go to https://www.arduino.cc/en/Main/Software - select windows 7 and newer & select
just Download.
• Double click the arduino-1.8.19-windows setup file & proceed with installation by simply
clicking ‘agree & next ‘ in installation pop-up window.
• Double click Arduino icon. Go to Menu – File → New.
• Write c program for Object Following robot.
• Go to menu bar &click on verify ✅ option.(clear if any errors are there) Go to menu –
Tools – Board – Arduino Uno R3.
• Connect your Arduino board to laptop using it’s usb cable
• Go to Tools → Port – COM3(Arduino uno)
• Go to Menu – select the right arrow icon ➡ (Upload).
• Then remove the usb cable from Arduino board
• Connect the circuit as per circuit diagram
• Make sure all the connections are made properly & avoiding loose connections. Switch
on the power supply and verify its functionality with theoretical.
PIN CONNECTIONS:-
• RIGHT IR sensor is connected to PIN2
• LEFT IR sensor is connected to PIN3
• RIGHTMOTOR Enable pin is connected to PIN10
• RIGHT MOTOR PIN1 is connected to PIN9
• RIGHT MOTOR PIN2 is connected to PIN8
• LEFT MOTOR Enable pin is connected to PIN5 LEFT MOTOR PIN1 is connected to
PIN7
• LEFT MOTOR PIN2 is connected to PIN6.
CODE:
#define in1 9
#define in2 8
#define in3 7
#define in4 6
#define enA 10
#define enB 5
int M1_Speed = 100; // speed of motor 1
int M2_Speed = 100; // speed of motor 2
int LeftRotationSpeed = 150; // Left Rotation Speed
int RightRotationSpeed = 150; // Right Rotation Speed
void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(A1, INPUT); // initialize Left sensor as an input
pinMode(A0, INPUT); // initialize Right sensor as an input
Serial.begin(9600);
//analogWrite(enA, M1_Speed); // Write The Duty Cycle 0 to 255 Enable
Pin A for Motor1 Speed
//analogWrite(enB, M2_Speed); // Write The Duty Cycle 0 to 255 Enable
Pin B for Motor2 Speed
delay(1000);
void loop() {
int LEFT_SENSOR = digitalRead(A0);
int RIGHT_SENSOR = digitalRead(A1);
if (RIGHT_SENSOR == 0 && LEFT_SENSOR == 0) {
forward(); //FORWARD
Serial.print("l:");
Serial.println(LEFT_SENSOR);
Serial.print("r:");
Serial.println(RIGHT_SENSOR);
Serial.println("direction:forward");
// delay(1000);
}
else if (RIGHT_SENSOR == 1 && LEFT_SENSOR == 0) {
right(); //Move Right
Serial.print("l:");
Serial.println(LEFT_SENSOR);
Serial.print("r:");
Serial.println(RIGHT_SENSOR);
Serial.println("direction:r");
// delay(1000);
}
else if (RIGHT_SENSOR == 0 && LEFT_SENSOR == 1) {
left(); //Move Left
Serial.print("l:");
Serial.println(LEFT_SENSOR);
Serial.print("r:");
Serial.println(RIGHT_SENSOR);
Serial.println("direction:l");
// delay(1000);
}
else if (RIGHT_SENSOR == 1 && LEFT_SENSOR == 1) {
Stop(); //STOP
Serial.print("l:");
Serial.println(LEFT_SENSOR);
Serial.print("r:");
Serial.println(RIGHT_SENSOR);
Serial.println("direction:s");
// delay(1000);
}
}
void forward()
{
digitalWrite(in1, LOW); //Right Motor forword Pin
digitalWrite(in2, HIGH); //Right Motor backword Pin
digitalWrite(in3, HIGH); //Left Motor backword Pin
digitalWrite(in4, LOW); //Left Motor forword Pin
analogWrite(enA, M1_Speed);
analogWrite(enB, M2_Speed);
}
void backward()
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enA, M1_Speed);
analogWrite(enB, M2_Speed);
}
void right()
{
digitalWrite(in1, LOW); //Right Motor forword Pin
digitalWrite(in2, HIGH); //Right Motor backword Pin
digitalWrite(in3, LOW); //Left Motor backword Pin
digitalWrite(in4, HIGH); //Left Motor forword Pin
analogWrite(enA, LeftRotationSpeed);
analogWrite(enB, RightRotationSpeed);
}
void left()
{
digitalWrite(in1, HIGH); //Right Motor forword Pin
digitalWrite(in2, LOW); //Right Motor backword Pin
digitalWrite(in3, HIGH); //Left Motor backword Pin
digitalWrite(in4, LOW); //Left Motor forword Pin
analogWrite(enA, LeftRotationSpeed);
analogWrite(enB, RightRotationSpeed);
}
void Stop()
{
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
Observations:-
• When object is present in front of ultrasonic sensor then the robo will move forward.
• When the object is nearer to the right IR sensor,it turns right.
• When the object is near left IR sensor, it turns left .
• When the object covers both IR sensors,then the robo will stop
• Result:-
Thus designed line Following Robot on Arduino Uno & verified its functionality.