Arduino Line-Follower Won’t Restart at Low Speed—How to Boost Starting Torque Without Sacrificing Line Detection?

My Arduino-based line-tracer outputs its motor speed so low that the wheels don’t turn on their own—I have to push it from behind for it to start moving. However, if I raise the speed, it drives fine but then struggles to detect the line accurately. Is there a way to run it more slowly so that it still recognizes the line well, or to improve line detection with the HW-870 sensor at higher speeds? I’d prefer the robot to operate at a reduced speed for better line tracking

// Sensor pin definitions
const int leftSensorPin     = A0;
const int rightSensorPin    = A1;

// Motor control pin definitions
const int leftMotorForward  = 3;
const int leftMotorBackward = 11;
const int rightMotorForward = 5;
const int rightMotorBackward= 6;

// Speed settings
const int motorSpeed        = 60;  // Base forward speed
const int turnSpeed         = 60;  // Turning speed

// Black line encounter counter and previous state
int blackCount      = 0;
bool prevBothOn     = false;

void setup() {
  // Configure sensor input pins
  pinMode(leftSensorPin, INPUT);
  pinMode(rightSensorPin, INPUT);

  // Configure motor output pins
  pinMode(leftMotorForward, OUTPUT);
  pinMode(leftMotorBackward, OUTPUT);
  pinMode(rightMotorForward, OUTPUT);
  pinMode(rightMotorBackward, OUTPUT);

  // Ensure motors are stopped initially
  stopMotors();
}

void loop() {
  int leftSensor  = analogRead(leftSensorPin);
  int rightSensor = analogRead(rightSensorPin);

  bool leftOnLine  = leftSensor  > 400;
  bool rightOnLine = rightSensor > 400;
  bool bothOn      = leftOnLine && rightOnLine;

  if (bothOn && !prevBothOn) {
    // Edge: both sensors detect the line simultaneously
    blackCount++;
    stopMotors();
    if (blackCount >= 6) {
      delay(10000);  // 6th encounter: stop for 10 seconds
      blackCount = 0;
    } else {
      delay(1000);   // 1st–5th encounter: stop for 1 second
    }
    // After stopping, resume forward movement immediately
    moveForward();
  }
  else if (!leftOnLine && !rightOnLine) {
    // No line detected: move forward
    moveForward();
  }
  else if (leftOnLine && !rightOnLine) {
    // Left sensor only: turn left
    turnLeft();
  }
  else if (!leftOnLine && rightOnLine) {
    // Right sensor only: turn right
    turnRight();
  }
  else {
    // Otherwise: maintain forward motion
    moveForward();
  }

  prevBothOn = bothOn;
  delay(50);
}

// === Motor control functions ===

// Move forward at base speed
void moveForward() {
  analogWrite(leftMotorForward,  motorSpeed);
  analogWrite(leftMotorBackward, 0);
  analogWrite(rightMotorForward, motorSpeed);
  analogWrite(rightMotorBackward,0);
}

// Turn left (left wheel stopped, right wheel forward)
void turnLeft() {
  analogWrite(leftMotorForward,  0);
  analogWrite(leftMotorBackward, 0);
  analogWrite(rightMotorForward, turnSpeed);
  analogWrite(rightMotorBackward,0);
}

// Turn right (right wheel stopped, left wheel forward)
void turnRight() {
  analogWrite(leftMotorForward,  turnSpeed);
  analogWrite(leftMotorBackward, 0);
  analogWrite(rightMotorForward, 0);
  analogWrite(rightMotorBackward,0);
}

// Stop all motors
void stopMotors() {
  analogWrite(leftMotorForward,  0);
  analogWrite(leftMotorBackward, 0);
  analogWrite(rightMotorForward, 0);
  analogWrite(rightMotorBackward,0);
}

I tried configuring

TCCR0B = TCCR1B & 0b11111000 | 0x01;
TCCR2B = TCCR2B & 0b11111000 | 0x01;

but it made no difference.

Posting schematics would help us.

@dkssudgk1211 ,

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Instead of pushing it to start, you could briefly set the motor speed to a value higher than 60 to get the device moving, and then reduce it to 60 once moving.

What exact motor are you using?

I guess I've had better luck, or something else may be wrong. Using PWM as you are shoukd allow the vehicle to crawl at extreme speeds.

Perhaps you are using motors too small for the job.

Also, you might adjust your control algorithm to have a minimum PWM setting. Some motors I use do not care to turn until I am up on the line between zero and full power. Obvsly you still want to be able to say zero, don't move, but the 0.001 to 1 turning to full speed should be lifted to, say, provide 0.1 to 1.0 full power. An immediate jump in that example to 10 percent.

a7