Webdroid Edutech
Published

Line Follower Robot Using Arduino

This project demonstrates how to build a simple line-following robot using an Arduino Uno. The robot detects a black line on a white surface

BeginnerProtip1 hour19
Line Follower Robot Using Arduino

Things used in this project

Hardware components

2 IR Sensor Modules
×1
Arduino Uno
×1
2WD Car Chassis Kit
×1
L298N Motor Driver Module
×1
Jumper wires
×1
7-12 V DC Battery (in our case lipo 2s battery)
×1

Story

Read more

Code

Arduino code to control the robot's movement based on the IR sensor readings.

Arduino
#define LEFT_SENSOR 11
#define RIGHT_SENSOR 12
#define MOTOR_LEFT_FORWARD 5
#define MOTOR_LEFT_BACKWARD 6
#define MOTOR_RIGHT_FORWARD 9
#define MOTOR_RIGHT_BACKWARD 10

void setup() {
    pinMode(LEFT_SENSOR, INPUT);
    pinMode(RIGHT_SENSOR, INPUT);
    pinMode(MOTOR_LEFT_FORWARD, OUTPUT);
    pinMode(MOTOR_LEFT_BACKWARD, OUTPUT);
    pinMode(MOTOR_RIGHT_FORWARD, OUTPUT);
    pinMode(MOTOR_RIGHT_BACKWARD, OUTPUT);
}

void loop() {
    int leftState = digitalRead(LEFT_SENSOR);
    int rightState = digitalRead(RIGHT_SENSOR);
    
    if (leftState == LOW && rightState == LOW) {
        moveForward();
    } else if (leftState == HIGH && rightState == LOW) {
        turnRight();
    } else if (leftState == LOW && rightState == HIGH) {
        turnLeft();
    } else {
        stopMotors();
    }
}

void moveForward() {
    digitalWrite(MOTOR_LEFT_FORWARD, HIGH);
    digitalWrite(MOTOR_LEFT_BACKWARD, LOW);
    digitalWrite(MOTOR_RIGHT_FORWARD, HIGH);
    digitalWrite(MOTOR_RIGHT_BACKWARD, LOW);
}

void turnRight() {
    digitalWrite(MOTOR_LEFT_FORWARD, HIGH);
    digitalWrite(MOTOR_LEFT_BACKWARD, LOW);
    digitalWrite(MOTOR_RIGHT_FORWARD, LOW);
    digitalWrite(MOTOR_RIGHT_BACKWARD, HIGH);
}

void turnLeft() {
    digitalWrite(MOTOR_LEFT_FORWARD, LOW);
    digitalWrite(MOTOR_LEFT_BACKWARD, HIGH);
    digitalWrite(MOTOR_RIGHT_FORWARD, HIGH);
    digitalWrite(MOTOR_RIGHT_BACKWARD, LOW);
}

void stopMotors() {
    digitalWrite(MOTOR_LEFT_FORWARD, LOW);
    digitalWrite(MOTOR_LEFT_BACKWARD, LOW);
    digitalWrite(MOTOR_RIGHT_FORWARD, LOW);
    digitalWrite(MOTOR_RIGHT_BACKWARD, LOW);
}

Credits

Webdroid Edutech
12 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.