Introduction: The Foundational Robotics Milestone
The autonomous line follower robot is the quintessential project bridging embedded C++ firmware, analog sensor acquisition, and electro-mechanical motor control.
Equipped with dual TCRT5000 infrared reflectance sensors and an H-bridge driver, the robot tracks high-contrast electrical tape lines in real time.
---
Operating Principle
- White Surface: High infrared reflectance triggers the phototransistor, outputting logic LOW (0).
- Black Line: Absorbs infrared radiation; no signal reflects back, outputting logic HIGH (1).
---
Complete Wiring Layout
- Left IR Sensor Output: Pin 2
- Right IR Sensor Output: Pin 3
- Motor Driver Left Forward (PWM): Pin 5
- Motor Driver Left Reverse: Pin 6
- Motor Driver Right Forward (PWM): Pin 9
- Motor Driver Right Reverse: Pin 10
- Driver Power (VCC): 7.4V (2x 18650 in series)
- Common Ground: Connect battery negative, motor driver GND, and Arduino GND together.
---
Arduino C++ Source Code
const int leftSensorPin = 2;
const int rightSensorPin = 3;
const int motorLeftFwd = 5;
const int motorLeftRev = 6;
const int motorRightFwd = 9;
const int motorRightRev = 10;
const int forwardSpeed = 160;
const int turnSpeed = 180;
void setup() {
pinMode(leftSensorPin, INPUT);
pinMode(rightSensorPin, INPUT);
pinMode(motorLeftFwd, OUTPUT);
pinMode(motorLeftRev, OUTPUT);
pinMode(motorRightFwd, OUTPUT);
pinMode(motorRightRev, OUTPUT);
stopMotors();
delay(1000);
}
void loop() {
int left = digitalRead(leftSensorPin);
int right = digitalRead(rightSensorPin);
if (left == LOW && right == LOW) {
moveForward();
} else if (left == HIGH && right == LOW) {
turnLeft();
} else if (left == LOW && right == HIGH) {
turnRight();
} else {
stopMotors();
}
}
void moveForward() {
analogWrite(motorLeftFwd, forwardSpeed);
digitalWrite(motorLeftRev, LOW);
analogWrite(motorRightFwd, forwardSpeed);
digitalWrite(motorRightRev, LOW);
}
void turnLeft() {
digitalWrite(motorLeftFwd, LOW);
digitalWrite(motorLeftRev, LOW);
analogWrite(motorRightFwd, turnSpeed);
digitalWrite(motorRightRev, LOW);
}
void turnRight() {
analogWrite(motorLeftFwd, turnSpeed);
digitalWrite(motorLeftRev, LOW);
digitalWrite(motorRightFwd, LOW);
digitalWrite(motorRightRev, LOW);
}
void stopMotors() {
digitalWrite(motorLeftFwd, LOW);
digitalWrite(motorLeftRev, LOW);
digitalWrite(motorRightFwd, LOW);
digitalWrite(motorRightRev, LOW);
}
---
Pro Troubleshooting Tips
- Reversed Steering: Swap the two motor lead wires on the terminal block to reverse rotation direction.
- Sensor Standoff Distance: Mount sensor heads strictly between 4mm and 7mm above the surface for optimal focal reception.
- Common Ground: Ensure the 18650 battery pack negative and the Arduino GND pin are firmly bridged together.