Line Follower Robot using Arduino
2024-12-11 | By Rachana Jain
License: Apache License, Version 2.0 Arduino
A Line Follower Robot is an autonomous machine that follows a specific path marked by a line, typically a black line on a white surface or vice versa. This project demonstrates how to create a Line Follower Robot using an Arduino.
Working of a Line Follower Robot
The robot uses line sensors to detect the line on the surface. Line sensors typically consist of an IR emitter (LED) and an IR receiver (photodiode). The IR emitter continuously projects infrared light, while the receiver detects the reflected light.
When the emitted light strikes a white surface, it reflects back to the receiver. However, when it encounters a black line, the light is absorbed rather than reflected, preventing it from reaching the receiver. This fundamental principle enables the robot to distinguish between the line and the background. The illustration below demonstrates how the line sensor operates.
How Does a Line Follower Robot Navigate?
To enable navigation, the robot requires two motors: one on the left side and the other on the right side of the chassis. Signals from line sensors control these motors. The robot’s movement depends on the input from these sensors, leading to four possible scenarios:
Case 1: Robot Moves Forward
When both sensors detect the white surface (indicating the black line is between them), the robot moves forward. In this scenario, both motors rotate in the forward direction, propelling the robot straight ahead.
Case 2: Robot Turns Left
If the left sensor detects the black line while the right sensor remains on the white surface, the microcontroller signals the robot to turn left. To achieve this, the left motor rotates backward, and the right motor continues to move forward, causing the robot to turn left.
Case 3: Robot Turns Right
When the right sensor detects the black line while the left sensor stays on the white surface, the microcontroller instructs the robot to turn right. This happens as the left motor moves forward and the right motor rotates backward, turning the robot to the right.
Case 4: Robot Stops
If both sensors simultaneously detect the black line, the robot stops moving. This is accomplished by halting both motors, ensuring the robot comes to a complete stop.
These cases illustrate the fundamental navigation logic of a Line Follower Robot, allowing it to adapt to the line's path effectively.
Hardware Required:
Arduino Uno: The microcontroller.
L293D Motor Driver Shield: For controlling the DC motors and managing motor direction.
Chassis with two geared motors and wheels: Forms the base structure of the robot.
Line sensor module (2): Detects the line.
Connecting wires: To connect components.
12V Li-ion Battery
Why Do We Need an L293D Motor Driver Shield?
The L293D Motor Driver Shield is essential because:
Bidirectional Control: It allows the robot to move both forward and backward by reversing the motor direction.
Voltage and Current Management: It provides sufficient voltage and current to drive DC motors.
Circuit Diagram of Line Follower Robot
The circuit consists of four key components: two line sensors, an L293D motor driver, four 12V BO motors, and an Arduino board. A 12V power supply (not shown in the diagram) is connected to the Arduino to provide power. The motor driver shield is placed directly on top of the Arduino board for seamless integration.
The VCC and GND pins of the Line sensors are connected to the corresponding VCC and GND pins on the Arduino.
The analog output pin of the left Line sensor is connected to the Arduino’s analog input pin A0, while the analog output pin of the right Line sensor is connected to the analog input pin A1.
The left-side motors are connected in parallel to the M3 port of the motor driver shield, and the right-side motors are connected in parallel to the M4 port of the shield.
Assembling the Arduino-based Line Follower Robot
Watch the following video:
Arduino Code for Line Follower Robot
/* Library used: Adafruit Motor Shield library V1 version: 1.0.1 For this code to run as expected: 1.The centre to centre distance between the Line sensors should be 11 to 11.5 cm 2. The width of black tape should be 4.8 to 5 cm 3. The distance of the sensor LED from the flat ground surface should be 2 cm. */ #include <AFMotor.h> // MACROS for Debug print, while calibrating set its value to 1 else keep it 0 #define DEBUG_PRINT 0 // MACROS for Analog Input #define LEFT_IR A0 #define RIGHT_IR A1 // MACROS to control the Robot #define DETECT_LIMIT 300 #define FORWARD_SPEED 60 #define TURN_SHARP_SPEED 150 #define TURN_SLIGHT_SPEED 120 #define DELAY_AFTER_TURN 140 #define BEFORE_TURN_DELAY 10 // BO Motor control related data here // Here motors are running using M3 and M4 of the shield and Left Motor is connected to M3 and Right Motor is connected to M4 using IC2 of the shield AF_DCMotor motorL(3); // Uses PWM0B pin of Arduino Pin 5 for Enable AF_DCMotor motorR(4); // Uses PWM0A pin of Arduino Pin 6 for Enable // variables to store the analog values int left_value; int right_value; // Set the last direction to Stop char lastDirection = 'S'; void setup() { #if DEBUG_PRINT Serial.begin(9600); #endif // Set the current speed of Left Motor to 0 motorL.setSpeed(0); // turn on motor motorL.run(RELEASE); // Set the current speed of Right Motor to 0 motorR.setSpeed(0); // turn off motor motorR.run(RELEASE); // To provide starting push to Robot these values are set motorR.run(FORWARD); motorL.run(FORWARD); motorL.setSpeed(255); motorR.setSpeed(255); delay(40); // delay of 40 ms } void loop() { left_value = analogRead(LEFT_IR); right_value = analogRead(RIGHT_IR); #if DEBUG_PRINT // This is for debugging. To check the analog inputs the DETECT_LIMIT MACRO value 300 is set by analysing the debug prints Serial.print(left_value); Serial.print(","); Serial.print(right_value); Serial.print(","); Serial.print(lastDirection); Serial.write(10); #endif // Right Sensor detects black line and left does not detect if (right_value >= DETECT_LIMIT && !(left_value >= DETECT_LIMIT)) { turnRight(); } // Left Sensor detects black line and right does not detect else if ((left_value >= DETECT_LIMIT) && !(right_value >= DETECT_LIMIT)) { turnLeft(); } // both sensors doesn't detect black line else if (!(left_value >= DETECT_LIMIT) && !(right_value >= DETECT_LIMIT)) { moveForward(); } // both sensors detect black line else if ((left_value >= DETECT_LIMIT) && (right_value >= DETECT_LIMIT)) { stop(); } } void moveForward() { if (lastDirection != 'F') { // To provide starting push to Robot when last direction was not forward motorR.run(FORWARD); motorL.run(FORWARD); motorL.setSpeed(255); motorR.setSpeed(255); lastDirection = 'F'; delay(20); } else { // If the last direction was forward motorR.run(FORWARD); motorL.run(FORWARD); motorL.setSpeed(FORWARD_SPEED); motorR.setSpeed(FORWARD_SPEED); } } void stop() { if (lastDirection != 'S') { // When stop is detected move further one time to check if its actual stop or not, needed when the robot turns motorR.run(FORWARD); motorL.run(FORWARD); motorL.setSpeed(255); motorR.setSpeed(255); lastDirection = 'S'; delay(40); } else { // When stop is detected next time then stop the Robot motorL.setSpeed(0); motorR.setSpeed(0); motorL.run(RELEASE); motorR.run(RELEASE); lastDirection = 'S'; } } void turnRight(void) { // If first time Right Turn is taken if (lastDirection != 'R') { lastDirection = 'R'; // Stop the motor for some time motorL.setSpeed(0); motorR.setSpeed(0); delay(BEFORE_TURN_DELAY); // take Slight Right turn motorL.run(FORWARD); motorR.run(BACKWARD); motorL.setSpeed(TURN_SLIGHT_SPEED); motorR.setSpeed(TURN_SLIGHT_SPEED); } else { // take sharp Right turn motorL.run(FORWARD); motorR.run(BACKWARD); motorL.setSpeed(TURN_SHARP_SPEED); motorR.setSpeed(TURN_SHARP_SPEED); } delay(DELAY_AFTER_TURN); } void turnLeft() { // If first time Left Turn is taken if (lastDirection != 'L') { lastDirection = 'L'; // Stop the motor for some time motorL.setSpeed(0); motorR.setSpeed(0); delay(BEFORE_TURN_DELAY); // take slight Left turn motorR.run(FORWARD); motorL.run(BACKWARD); motorL.setSpeed(TURN_SLIGHT_SPEED); motorR.setSpeed(TURN_SLIGHT_SPEED); } else { // take sharp Left turn motorR.run(FORWARD); motorL.run(BACKWARD); motorL.setSpeed(TURN_SHARP_SPEED); motorR.setSpeed(TURN_SHARP_SPEED); } delay(DELAY_AFTER_TURN); }
Testing the Line Follower Robot
Initial Setup: Place the robot on a surface with a clearly visible line (black on white or white on black).
Power On: Turn on the power switch and observe the robot’s movement.
Debugging: If the robot deviates from the line, check the sensor readings and ensure correct wiring.
Final Adjustments: Fine-tune the motor speeds in the code to optimize performance.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum