Build a Line Following Robot

Difficulty Level: Intermediate

A line-following robot uses infrared sensors to detect the line and navigate accordingly. This project involves basic robotics and programming concepts.

Components Required

Wiring Diagram

Connect the components according to the following configuration:

1. Connect the IR sensors to the Arduino's digital pins (e.g., pins 2-6).
2. Connect the motor driver to the Arduino and power supply.
3. Connect the DC motors to the motor driver.
        

Arduino Code

Below is a simple example code to control the line-following robot:

#define LEFT_MOTOR_FORWARD 5
#define LEFT_MOTOR_BACKWARD 4
#define RIGHT_MOTOR_FORWARD 7
#define RIGHT_MOTOR_BACKWARD 6

#define LEFT_SENSOR A0
#define CENTER_SENSOR A1
#define RIGHT_SENSOR A2

void setup() {
    pinMode(LEFT_MOTOR_FORWARD, OUTPUT);
    pinMode(LEFT_MOTOR_BACKWARD, OUTPUT);
    pinMode(RIGHT_MOTOR_FORWARD, OUTPUT);
    pinMode(RIGHT_MOTOR_BACKWARD, OUTPUT);
    
    pinMode(LEFT_SENSOR, INPUT);
    pinMode(CENTER_SENSOR, INPUT);
    pinMode(RIGHT_SENSOR, INPUT);
}

void loop() {
    int leftValue = digitalRead(LEFT_SENSOR);
    int centerValue = digitalRead(CENTER_SENSOR);
    int rightValue = digitalRead(RIGHT_SENSOR);
    
    if (centerValue == HIGH) {
        // Move forward
        moveForward();
    } else if (leftValue == HIGH) {
        // Turn right
        turnRight();
    } else if (rightValue == HIGH) {
        // Turn left
        turnLeft();
    } else {
        // Stop
        stop();
    }
}

void moveForward() {
    digitalWrite(LEFT_MOTOR_FORWARD, HIGH);
    digitalWrite(RIGHT_MOTOR_FORWARD, HIGH);
}

void turnRight() {
    digitalWrite(LEFT_MOTOR_FORWARD, LOW);
    digitalWrite(RIGHT_MOTOR_FORWARD, HIGH);
}

void turnLeft() {
    digitalWrite(LEFT_MOTOR_FORWARD, HIGH);
    digitalWrite(RIGHT_MOTOR_FORWARD, LOW);
}

void stop() {
    digitalWrite(LEFT_MOTOR_FORWARD, LOW);
    digitalWrite(RIGHT_MOTOR_FORWARD, LOW);
}
        

Calibration

After assembling your robot, you may need to calibrate the IR sensors for the specific line color and surface you are using. Adjust the threshold values in the code based on your testing.

Testing and Troubleshooting

Run the robot on a defined track with a black line on a white surface. If it does not follow the line correctly, check the wiring, ensure the sensors are positioned properly, and adjust the sensitivity.

Conclusion

Building a line-following robot is a fun and educational project that introduces key robotics concepts. You can further enhance your robot by adding features like speed control, obstacle avoidance, or wireless control.

Additional Resources