0
0
AutocadHow-ToBeginner · 4 min read

Arduino Project for Line Follower Robot: Simple Guide & Code

A line follower robot with Arduino uses infrared sensors to detect a line and motors to follow it. The Arduino reads sensor values and controls motor speed to keep the robot on track.
📐

Syntax

The basic syntax for a line follower robot program includes reading sensor inputs, processing the data, and controlling motors accordingly.

  • pinMode(pin, mode): Sets sensor or motor pins as input or output.
  • digitalRead(pin): Reads sensor digital value (line detected or not).
  • analogWrite(pin, value): Controls motor speed using PWM.
  • if-else statements: Decide motor actions based on sensor readings.
arduino
const int sensorLeft = 2;
const int sensorRight = 3;
const int motorLeft = 9;
const int motorRight = 10;

void setup() {
  pinMode(sensorLeft, INPUT);
  pinMode(sensorRight, INPUT);
  pinMode(motorLeft, OUTPUT);
  pinMode(motorRight, OUTPUT);
}

void loop() {
  int leftValue = digitalRead(sensorLeft);
  int rightValue = digitalRead(sensorRight);

  if (leftValue == LOW && rightValue == LOW) {
    // Both sensors on line
    moveForward();
  } else if (leftValue == HIGH) {
    // Left sensor off line
    turnRight();
  } else if (rightValue == HIGH) {
    // Right sensor off line
    turnLeft();
  }
}
💻

Example

This example shows a simple line follower robot code using two infrared sensors and two motors. It reads sensors and adjusts motor speeds to follow a black line on a white surface.

arduino
const int sensorLeft = 2;
const int sensorRight = 3;
const int motorLeftForward = 9;
const int motorRightForward = 10;

void setup() {
  pinMode(sensorLeft, INPUT);
  pinMode(sensorRight, INPUT);
  pinMode(motorLeftForward, OUTPUT);
  pinMode(motorRightForward, OUTPUT);
}

void loop() {
  int leftSensor = digitalRead(sensorLeft);
  int rightSensor = digitalRead(sensorRight);

  if (leftSensor == LOW && rightSensor == LOW) {
    // Both sensors detect line - move forward
    analogWrite(motorLeftForward, 255);
    analogWrite(motorRightForward, 255);
  } else if (leftSensor == HIGH) {
    // Left sensor off line - turn right
    analogWrite(motorLeftForward, 0);
    analogWrite(motorRightForward, 255);
  } else if (rightSensor == HIGH) {
    // Right sensor off line - turn left
    analogWrite(motorLeftForward, 255);
    analogWrite(motorRightForward, 0);
  } else {
    // No line detected - stop
    analogWrite(motorLeftForward, 0);
    analogWrite(motorRightForward, 0);
  }
}
Output
The robot moves forward when both sensors detect the line, turns right if left sensor loses the line, turns left if right sensor loses the line, and stops if no line is detected.
⚠️

Common Pitfalls

Common mistakes include:

  • Incorrect sensor wiring causing wrong readings.
  • Not calibrating sensors for the surface color.
  • Using digitalRead instead of analogRead when sensors output analog values.
  • Motors running at full speed without smooth turning logic.
  • Ignoring motor driver requirements leading to hardware damage.

Always test sensors separately and verify motor connections before running the full program.

arduino
/* Wrong way: Using digitalRead on analog sensor pins without calibration */
int sensorValue = digitalRead(A0); // Incorrect if sensor outputs analog

/* Right way: Use analogRead and set threshold */
int sensorValue = analogRead(A0);
if (sensorValue < 500) {
  // On line
} else {
  // Off line
}
📊

Quick Reference

Tips for building a line follower robot:

  • Use infrared sensors to detect line contrast.
  • Calibrate sensors for your surface colors.
  • Control motors with PWM for smooth turns.
  • Test sensor readings before integrating motor control.
  • Use a motor driver shield/module to protect Arduino.

Key Takeaways

Use infrared sensors to detect the line and read their values correctly.
Control motor speed with PWM to adjust robot direction smoothly.
Calibrate sensors for your specific surface to get accurate readings.
Test sensors and motors separately before combining in the main program.
Avoid hardware damage by using proper motor drivers and wiring.