Arduino projects: Line follower robot


We’ve built an obstacle avoiding robot and a Bluetooth controlled robot in the past.

What we didn’t do yet is one of the most popular robots every beginner do when starting with robotics.

A line follower!

A line follower is a satisfying robot especially when it follows the line the way you want it to.

I would first want to point out that we will go with the simplest code possible without making the use of a PID controller which most of the advanced robots of such use.

If you want some more advanced algorithm, you need to read about PID controllers. Every robot on competitions has one or more PID controllers and 10 or more line sensors so that it can track the line really fast and precisely.

Parts needed

  • 1 x Arduino Uno board
  • 3 x line tracking sensors
  • 1 x Motor controller
  • 1 x Robot chassis
  • jumper wires
  • 1 x breadboard (check the image)

We will also need a 3d printed part to attach our line tracking sensors to the chassis at a proper height above the ground, you can download it here: https://www.thingiverse.com/thing:2594606

This robot is quite similar to our last Bluetooth controlled robot. We have to connect 4 cables to 5V but there is only one 5V pin on Arduino. That’s why we needed to use a breadboard (or just a piece of it). I had a medium size breadboard with detachable power lines. So, I used one of them to connect all 5V and GND pins without soldering.

The 3d printed holder should be fixed at the front of the robot and you should try fixing the sensors on it as seen in the image below.

The LEDs on the sensor indicate whether it detects a line or not, you might need to calibrate it before it works perfectly on your set-up. There is a potentiometer on the sensor which you can use to adjust its accuracy.

You should also keep in mind that the line detection accuracy depends on the light conditions as well.

The logic here is the following:

  • When the left and right sensors detect no line and at the same time the middle sensor detects a line, the robot should move forward.
  • When the right sensor detects the line, the robot should turn right
  • When the left sensor detects the line, the robot should turn left

That’s all it does. Incomplete right! But not a bad algorithm to start with. There is plenty room for improvement here.

The motor functions are exactly the same as there were in our Bluetooth controlled robot example at the only difference that the PWM values here are low. This robot needs to move slowly in order to stay on track. Feel free to experiment with faster speeds to check its limitation.

//speed of motors betwen 0 and 255, if you like you can change it
int pwm_speedA = 150;
int pwm_speedB = 145;
char command;

void setup() {
Serial.begin(9600);
//pins for motor controller
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(3, OUTPUT);

pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(7, INPUT);

}

void loop() {

if(digitalRead(2) == LOW && digitalRead(7) == LOW && digitalRead(4) == HIGH){
forward();
}else if(digitalRead(2) == HIGH && digitalRead(7) == LOW && digitalRead(4) == LOW){
right();
delay(100);
}else if(digitalRead(2) == LOW && digitalRead(7) == HIGH && digitalRead(4) == LOW){
left();
delay(100);
}
}

// function for driving straight
void forward(){
digitalWrite(10, HIGH);
digitalWrite(11, LOW);

digitalWrite(9, HIGH);
digitalWrite(6, LOW);

analogWrite(5, 80);
analogWrite(3, 80);
}

//function for reversing
void backward(){

digitalWrite(10, LOW);
digitalWrite(11, HIGH);

digitalWrite(9, LOW);
digitalWrite(6, HIGH);

analogWrite(5, 80);
analogWrite(3, 80);
}

//function for turning left
void left(){
digitalWrite(11, LOW);
digitalWrite(10, LOW);

digitalWrite(9, HIGH);
digitalWrite(6, LOW);

analogWrite(3, 0);
analogWrite(5, 100);
}

//function for turning right
void right(){
digitalWrite(10, HIGH);
digitalWrite(11, LOW);

digitalWrite(9, LOW);
digitalWrite(6, LOW);

analogWrite(3, 100);
analogWrite(5, 0);

}

//function for stopping motors
void motors_stop(){

digitalWrite(11, LOW);
digitalWrite(10, LOW);

digitalWrite(9,LOW);
digitalWrite(6, LOW);

analogWrite(5, 0);
analogWrite(3, 0);
}

Here is the robot working on a dark floor, not too bad. Quite inefficient, but the good news is, it stays on track.

And that’s it for today. One task at hand right now for you is to go and find ways to improve the performance of this robot.

tchouken2

Related Posts