Robot That Follows Lines
​
Project:
The arduino-based robot specifically designed to detect and trace lines on a surface. Using sensors, it can autonomously navigate by staying on its path. This technology is commonly used in automation and educational robotics projects..
​
Parts and Materials:
Code:
int rightMotorA = 8; // Right motor pin A
int rightMotorB = 9; // Right motor pin B
int leftMotorA = 10; // Left motor pin A
int leftMotorB = 11; // Left motor pin B
int rightSensor = 6; // Right side sensor pin
int leftSensor = 7; // Left side sensor pin
int rightSensorVal = 0;
int leftSensorVal = 0;
int indicatorLED = 13;
int rightMotorSpeed = 3;
int leftMotorSpeed = 5;
int baseSpeed = 200;
int turnSpeed = 300;
int turnDelay = 10;
​
void setup()
{
pinMode(rightMotorA, OUTPUT);
pinMode(rightMotorB, OUTPUT);
pinMode(leftMotorA, OUTPUT);
pinMode(leftMotorB, OUTPUT);
pinMode(indicatorLED, OUTPUT);
pinMode(rightSensor, INPUT);
pinMode(leftSensor, INPUT);
delay(5000); // Initial delay after power up
}
void loop()
{
rightSensorVal = digitalRead(rightSensor);
leftSensorVal = digitalRead(leftSensor);
if (leftSensorVal == LOW && rightSensorVal == LOW)
{
moveForward();
}
else if (leftSensorVal == HIGH && rightSensorVal == LOW)
{
turnLeft();
}
else if (leftSensorVal == LOW && rightSensorVal == HIGH)
{
turnRight();
}
else
{
halt(); // Stop robot if both sensors detect a line
}
}
void moveForward()
{
digitalWrite(rightMotorA, HIGH);
digitalWrite(rightMotorB, LOW);
digitalWrite(leftMotorA, HIGH);
digitalWrite(leftMotorB, LOW);
analogWrite(rightMotorSpeed, baseSpeed);
analogWrite(leftMotorSpeed, baseSpeed);
}
void moveBackward()
{
digitalWrite(rightMotorA, LOW);
digitalWrite(rightMotorB, HIGH);
digitalWrite(leftMotorA, LOW);
digitalWrite(leftMotorB, HIGH);
analogWrite(rightMotorSpeed, baseSpeed);
analogWrite(leftMotorSpeed, baseSpeed);
}
void turnRight()
{
digitalWrite(rightMotorA, LOW);
digitalWrite(rightMotorB, HIGH);
digitalWrite(leftMotorA, HIGH);
digitalWrite(leftMotorB, LOW);
analogWrite(rightMotorSpeed, turnSpeed);
analogWrite(leftMotorSpeed, turnSpeed);
delay(turnDelay); // Short delay for turning
}
void turnLeft()
{
digitalWrite(rightMotorA, HIGH);
digitalWrite(rightMotorB, LOW);
digitalWrite(leftMotorA, LOW);
digitalWrite(leftMotorB, HIGH);
analogWrite(rightMotorSpeed, turnSpeed);
analogWrite(leftMotorSpeed, turnSpeed);
delay(turnDelay); // Short delay for turning
}
void halt()
{
analogWrite(rightMotorSpeed, 0);
analogWrite(leftMotorSpeed, 0);
}
​
Explanation:
​
Variable Declaration & Initialization :
- Motor and sensor pins on the microcontroller (like Arduino) are assigned:
- `rightMotorA`, `rightMotorB`: Control pins for the right side motor.
- `leftMotorA`, `leftMotorB`: Control pins for the left side motor.
- `rightSensor`, `leftSensor`: Pins for sensors that detect lines on the right and left, respectively.
- `indicatorLED`: Pin for a potential status LED.
- `rightMotorSpeed`, `leftMotorSpeed`: Pins to control the speed of the right and left motors, respectively.
- `baseSpeed` and `turnSpeed` represent motor speeds for normal forward movement and for turns.
- `turnDelay` represents a short delay when turning.
Setup Function (`setup`) :
- Initializes the motor pins and sensor pins to be either OUTPUT (for the motors and LED) or INPUT (for the sensors).
- Waits for 5 seconds after starting up. This is likely to give the user time to place the robot on the line before it starts following it.
Main Loop Function (`loop`):
- Reads the values of the right and left sensors.
- Depending on the sensor values, it decides which function to execute:
- Both sensors `LOW` (not detecting line): `moveForward()`
- Left sensor `HIGH` (detecting line) and right sensor `LOW`: `turnLeft()`
- Left sensor `LOW` and right sensor `HIGH`: `turnRight()`
- Both sensors `HIGH`: `halt()`
Movement Functions:
- `moveForward()`: Makes the robot move straight ahead by driving both left and right motors in the forward direction at `baseSpeed`.
- `moveBackward()`: Not used in the main loop, but it's designed to make the robot move backward. Both motors are driven in the reverse direction.
- `turnRight()`: The robot turns right by moving the left motor forward and the right motor backward for a short duration defined by `turnDelay`.
- `turnLeft()`: The robot turns left by moving the right motor forward and the left motor backward for a duration defined by `turnDelay`.
- `halt()`: Stops both motors by setting their speed to zero.
​
The logic behind the robot's movement is simple: if it's on the line, it corrects its path. If both sensors are on the line, it stops. If neither sensor is on the line, it proceeds forward.
This code offers a basic line-following mechanism. In practical applications, further refinements might be required for smoother and more accurate line tracking.
​
​
Circuit Design:
​