Line follower robot. Build a line follower for less than $20.

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

The end result.

The final result.

If you need help, please ask.

If these instructions are unclear, please contact us using the contact form in the sidebar and we’ll help you get you back on track.

Warning and Liability.

The Brisbane Robotics Club takes absolutely no Liability….

Be sure to read the instructions carefully and do your own research if you’re unsure.

Build instructions disclosure and recommendations.

If this is your first time building Robots, please follow the instructions.

If your Robot doesn’t work, the main cause is loose wires then incorrect assembly.

There is only one way to build any robot. The mechanical, electrical and code all work together.

If any system is not built correctly the robot will not work.

As you build the robot, we will show you how to test that your assembly is done correctly.

Please test your robot as you build it. This makes fault finding much easier.

I receive no commission for any parts you order from your nominated retailer. Therefore feel free to buy from any retailer you prefer.

With regards to the line sensor, do not buy any variations to the one we have suggested. We have experimented with several sensors and the sensor we’ve chosen works the best.

When we designed this Robot our aim was to build the most affordable line follower to get people started in Robotics.

There are many variations for each part we’ve chosen. We suggest if this is your first Robot that you build this robot exactly as we have instructed.

Once you get it working we hope that you have understood the process and are able to upgrade our design and create your own line following robot.

We look forward to see what you can improve and make a faster line following robot.

Batteries

Batteries are the most expensive part of any robot.

This design can either be powered by a 9 volt battery or 2s (7.4 Volts) LiPo battery.

A 2s LiPo battery is fairly expensive and you’ll also need to buy a LiPo battery charger.

If you’re just starting in Robotics and seeing if you like it, I recommend using 9 volt batteries.

If you enjoy robotics and see yourself playing with them a lot, then consider LiPo batteries as a better long term solution.

LiPo batteries and chargers are available from your local hobby shop.

LiPo batteries are great, but it’s best to understand their risks and proper operation to prevent damage to LiPo batteries and fires. Join any online hobby group to see what happens when LiPo’s go bad.

If you decide to use a LiPo battery, we absolutely recommend using a battery alarm to show the battery voltage and warn you when the batteries go below the recommended level. We’ve killed a few batteries and learnt the hard way.

The LiPo battery option will also require you to solder the XT60 plugs to the wiring. If you don’t have access to a soldering iron or the skills, it’s best to use the 9 volt battery setup.

Tools Required.

The design was chosen to minimise the tools required.

You will need a wire cutter to cut the wires to the required length.

You may need a wire stripper, otherwise you can use the wire cutters and a little bit of skill.

You will need a small Phillips head screwdriver.

You will need tweasers to remove the shunts from the motor controller.

No soldering is required, all the wired connections are twisted together and covered with electrical tape to prevent shorting.

You may need scissors to cut the electrical tape.

Parts Required.

PartPrice
($US)
Smart Robot Car Chassis Kit Speed Encoder Battery Box 2WD For Uno $2.24
Arduino Uno R3$3.91
5 Channel TCRT5000 IR sensor$1.07
L298N motor driver board$1.50
M3 Nylon standoffs$2.52
40 Pin Jumper leads Male-Female$3.75
9V Battery snap connector (9 volt users only)$0.56
XT60 Male Female Bullet connectors. (LiPo users only)$1.49
Electrical Tape 18mm wide, 18mtr$0.36
Total$17.40
Arduino Uno R3.
Smart Robot Car Chassis Kit Speed Encoder Battery Box 2WD For Uno.
Smart Robot Car Chassis Kit Speed Encoder Battery Box 2WD For Uno.
5 Channel TCRT5000 IR sensor.
L298N motor driver board.
M3 Nylon standoffs.
40 Pin Jumper leads Male-Female.
9V Battery snap connector.
XT60 Male Female Bullet connectors.
Electrical Tape 18mm wide, 18mtr.

Print The Track Tiles.

Download the Track tiles shown below to build a Track.

Print several tiles of each on A4 paper..

At a minimum, print the start/finish tile to calibrate the line sensor height.

To construct a track overlap the tiles so there is a continuous line. You may need to trim the page.

Start/Finish line.
Straight section.
Turn Right.
Turn Left.

Mechanical Assembly

Line Sensors.

Install the line sensors last. The height will need to be calibrated while it is powered.

The instructions for calibrating the sensor height are in a section below.

It is important to get the height correct or the robot will not work.

We suggest connecting the line sensor to the Arduino and calibrate the line sensor with a tile of track.

Once you know the correct height, then mechanically fix it to your vehicle.

The height can be adjusted using the different standoff lengths.

Chassis

Peel the protective paper from both sides of the acrylic chassis.

Install the rear castor wheel using the gold standoffs provided.

Install the power switch in the center of the chassis. The rotation of the switch is not important.

Install both motors and acrylic motor mounts.

The encoders are optional. I’ve installed mine, but they’re not necessary.

IMPORTANT! Be sure to mount the motors so that the motor power terminals are facing to the right of the vehicle.

If you install the terminals on the left hand side, the robot motors will run backwards instead of forwards and the robot will not work.

The holes on the chassis do not line up properly with the Arduino and L298N motor controller, do the best you can.

Mount the Arduino on the front of the vehicle using the standoffs.

Mount the L298N motor controller to the rear of the vehicle. It is important to get the correct orientation. When we connect the wires, it will need to be exactly as we instruct on the motors may psin in the wrong direction and and robot will not work.

There is no need to install the AA battery box, we will not be using it.

When completed, your vehicle should look like this. (Without battery box and acrylic protective covering.)

Wiring Diagram/Fritzing

Most of the connections can be wired into the Arduino using the Male/Female jumper leads.

For all other connections you may need to cut the male/female connectors from the jumper wires and strip the cover off the wiring.

To join wiring, twist the wires together. When all the connections are made, cover the exposed wire with electrical tape to prevent shorting.

Arduino R3 Pinout

Each connection port on the Arduino has a specific and unique function to receive and send data using the Arduino Uno.

During this installation we will not elaborate on each ports function, just follow the instructions and connect the correct wires to the correct ports otherwise your robot will not work.

Arduino Uno pinout.

Connect Arduino to the Battery

ArduinoBattery/Switch
VIN 12VMain power switch out.
GNDBattery ground connection.

Connect the line sensor to the Arduino

Line SensorArduino
Out5Digital Pin 7
Out4Digital Pin 6
Out3Digital Pin 5
Out2Digital Pin 4
Out1Digital Pin 3
5V5V
GNDGND

Connect the L298N to the Arduino

First you will need to remove the shunts from the motor controller.

The shunts (black clips) are shown below and can be removed with tweasers.

Remove the shunts so we can connect wires.
L298NArduino/Motor
Out1Right motor, top terminal
Out2Right motor, lower terminal
Out3Left motor, top terminal
Out4Left motor, lower terminal
12VMain power switch out
GNDTo battery connection ground.
GNDArduino GND
5VEmpty.
EnADigital Pin 8
In1 Digital Pin 9
In2 Digital Pin 10
In3 Digital Pin 11
In3 Digital Pin 12
EnB Digital Pin 13

The L298N has only one Ground output. Run separate wires from single ground output to both ports listed above.

Do not connect the ground directly to the battery. We want to be able to disconnect the battery without interfering with the wiring.

Connect the ground wire to prior to the batteries XT60/snap connection.

Connect the Robot to the battery/switch

Whether you use a 9V battery or LiPo battery the positive terminal of the battery will be connected to the robots main power switch in terminal. By doing this, we can easily turn the power to the robot on and off.

The in/out main power switch terminal makes no difference which pin you select. Make sure you nominate one pin as ‘in’ and the other as ‘out’ and don’t mix them up.

All power supplied to the Arduino and L298N will come from the main power switch out terminal.

The ground wires do not need to go to the main power switch. The ground wires from the Arduino and L298N can go directly to the battery.

The battery connection below refers to the physical location prior to the battery connection point. By doing this, we can remove the battery easily.

If you are using a LiPo battery, make the connection prior to the male connection.

SourceBattery Connection
L298N 12VMain power switch out
Arduino VINMain power switch out

Pre-Knowledge.

If you have never used an Arduino before you will need to click the link below and install the FREE arduino IDE software on your computer and configure your settings.

http://brisbaneroboticsclub.id.au/setup-your-arduino/

Code Required/Github.

Copy and paste the code below into your Arduino editor.

Save a copy of the code onto your computer.

To send the code to your robot please read the next section.

Test Code

Use this code to test the motors and line sensor.

/*
    This is the Test code for the line follower.

    For instructions, please click the link below.
*/

// Motor A - Left Motor
#define MCenA 13
#define MCin1 11
#define MCin2 12

// Motor B - Right Motor
#define MCenB 8
#define MCin3 9
#define MCin4 10

// Set inital speed for Motors
int motorSpeedA = 0;
int motorSpeedB = 0;

// Initialise light sensors

int in1 = 3;
int in2 = 4;
int in3 = 5;
int in4 = 6;
int in5 = 7;

int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
int s5 = 0;

void setup() {
  // Open serial connection to display values in Monitor if connected
  Serial.begin(9600);
  Serial.println("Light sensor test");

  // Initialise sensor inputs:
  pinMode(in1,INPUT);
  pinMode(in2,INPUT);
  pinMode(in3,INPUT);
  pinMode(in4,INPUT);
  pinMode(in5,INPUT);

  // Initialize the Arduino ports for the Motor Controller
  pinMode(MCenA, OUTPUT);
  pinMode(MCenB, OUTPUT);
  pinMode(MCin1, OUTPUT);
  pinMode(MCin2, OUTPUT);
  pinMode(MCin3, OUTPUT);
  pinMode(MCin4, OUTPUT);
  
}
void loop() {

  // Read each light sensor
  s1 = digitalRead(in1);
  s2 = digitalRead(in2);
  s3 = digitalRead(in3);
  s4 = digitalRead(in4);
  s5 = digitalRead(in5);

  // Print ligth sensor reading to Serial Monitor (Ctrl + Shift + M to view)
  Serial.print("Sensors");
  Serial.print("\t");
  Serial.print(s1);
  Serial.print("\t");
  Serial.print(s2);
  Serial.print("\t");
  Serial.print(s3);
  Serial.print("\t");
  Serial.print(s4);
  Serial.print("\t");
  Serial.print(s5);
  Serial.print("\t");

  // Set Motor A forward by default
  digitalWrite(MCin1, LOW);
  digitalWrite(MCin2, HIGH);
  // Set Motor B forward by default
  digitalWrite(MCin3, LOW);
  digitalWrite(MCin4, HIGH);
  
  TurnRight();
  delay(1000);

  Stop();
  delay(1000);

  TurnLeft();
  delay(1000);

  Stop();
  delay(1000);

  DriveStraight();
  delay(1000);
  
  Stop();
  delay(1000);
}

void DriveStraight() {
  Serial.print("Drive straight (IN LOOP.)");

  // Set motor speeds
  motorSpeedA = 150;
  motorSpeedB = 150;

  // Print motor speed to monitor.
  Serial.print("Motor Speeds.");
  Serial.print("\t");
  Serial.print(motorSpeedA);
  Serial.print("\t");
  Serial.println(motorSpeedB);

  // Send motor speeds to motor
  analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}

void TurnRight() {
  Serial.print("Turn Right (IN LOOP.)");

  // Set motor speeds
  motorSpeedA = 0;
  motorSpeedB = 150;

  // Print motor speed to montor.
  Serial.print("Motor Speeds.");
  Serial.print("\t");
  Serial.print(motorSpeedA);
  Serial.print("\t");
  Serial.println(motorSpeedB);

  // Send motor speeds to motor
  analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}

void TurnLeft() {
  Serial.print("Turn Left (IN LOOP.)");

  // Set motor speeds
  motorSpeedA = 150;
  motorSpeedB = 0;

  // Print motor speed to montor.
  Serial.print("Motor Speeds.");
  Serial.print("\t");
  Serial.print(motorSpeedA);
  Serial.print("\t");
  Serial.println(motorSpeedB);

  // Send motor speeds to motor
  analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}

void Stop() {
  Serial.print("Stop (IN LOOP.)");
  
  // Set motor speeds
  motorSpeedA = 0;
  motorSpeedB = 0;

  // Print motor speed to montor.
  Serial.print("Motor Speeds.");
  Serial.print("\t");
  Serial.print(motorSpeedA);
  Serial.print("\t");
  Serial.println(motorSpeedB);
  
  // Send motor speeds to motor
  analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}

Line Following Code

Use this code to run the line follower.

/*
    This is the run code for the line follower.

    For instructions, please click the link below.
*/

// Motor A - Left Motor
#define MCenA 13
#define MCin1 11
#define MCin2 12

// Motor B - Right Motor
#define MCenB 8
#define MCin3 9
#define MCin4 10

// Set inital speed for Motors
int motorSpeedA = 0;
int motorSpeedB = 0;

// Initialise light sensors

int in1 = 3;
int in2 = 4;
int in3 = 5;
int in4 = 6;
int in5 = 7;

int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
int s5 = 0;

void setup() {
  // Open serial connection to display values in Monitor if connected
  Serial.begin(9600);
  Serial.println("Light sensor test");

  // Initialize the Arduino ports for the Motor Controller
  pinMode(MCenA, OUTPUT);
  pinMode(MCenB, OUTPUT);
  pinMode(MCin1, OUTPUT);
  pinMode(MCin2, OUTPUT);
  pinMode(MCin3, OUTPUT);
  pinMode(MCin4, OUTPUT);

  // Initialise sensor inputs:
  pinMode(in1,INPUT);
  pinMode(in2,INPUT);
  pinMode(in3,INPUT);
  pinMode(in4,INPUT);
  pinMode(in5,INPUT);
  
}
void loop() {

  // Read each light sensor
  s1 = digitalRead(in1);
  s2 = digitalRead(in2);
  s3 = digitalRead(in3);
  s4 = digitalRead(in4);
  s5 = digitalRead(in5);

  Serial.print("Sensors");
  Serial.print("\t");
  Serial.print(s1);
  Serial.print("\t");
  Serial.print(s2);
  Serial.print("\t");
  Serial.print(s3);
  Serial.print("\t");
  Serial.print(s4);
  Serial.print("\t");
  Serial.print(s5);
  Serial.print("\t");

  // Depending on Joystick values, drive the vehicle.
  
  // Y-axis used for forward and backward control. (0 = UP, 512 = MID, 1023 = DOWN)

  // Set Motor A forward by default
  digitalWrite(MCin1, LOW);
  digitalWrite(MCin2, HIGH);
  // Set Motor B forward by default
  digitalWrite(MCin3, LOW);
  digitalWrite(MCin4, HIGH);

  // END OF MANUAL INPUT FROM JOYSTICK
  
  // START OF AUTONOMOUS CODE

  if ((s2 == 1) && (s4 == 1)) {
    // Serial.print("Drive straight.");
    DriveStraight();
  }
  else if ((s2 == 0) && (s4 == 1)) {
    // Serial.print("Turn Right.");
    TurnRight();
  }
  else if ((s2 == 1) && (s4 == 0)) {
    // Serial.print("Turn Left.");
    TurnLeft();
  }
  else if ((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 0) && (s5 == 0)) {
    // Serial.print("Drive straight.");
    DriveStraight();
  }
  else {
    // Serial.print("STOP.");
    Stop();
  }

  delay(10);

  motorSpeedA = 0;
  motorSpeedB = 0;
  
  // Send motor speeds to motor
  analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B

  delay(10);
  
}

void DriveStraight() {
  Serial.print("Drive straight (IN LOOP.)");

  // Set motor speeds
  motorSpeedA = 150;
  motorSpeedB = 150;

  // Print motor speed to montor.
  Serial.print("Motor Speeds.");
  Serial.print("\t");
  Serial.print(motorSpeedA);
  Serial.print("\t");
  Serial.println(motorSpeedB);

  // Send motor speeds to motor
  analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}

void TurnRight() {
  Serial.print("Turn Right (IN LOOP.)");

  // Set motor speeds
  motorSpeedA = 0;
  motorSpeedB = 150;

  // Print motor speed to montor.
  Serial.print("Motor Speeds.");
  Serial.print("\t");
  Serial.print(motorSpeedA);
  Serial.print("\t");
  Serial.println(motorSpeedB);

  // Send motor speeds to motor
  analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}

void TurnLeft() {
  Serial.print("Turn Left (IN LOOP.)");

  // Set motor speeds
  motorSpeedA = 150;
  motorSpeedB = 0;

  // Print motor speed to montor.
  Serial.print("Motor Speeds.");
  Serial.print("\t");
  Serial.print(motorSpeedA);
  Serial.print("\t");
  Serial.println(motorSpeedB);

  // Send motor speeds to motor
  analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}

void Stop() {
  Serial.print("Stop (IN LOOP.)");
  
  // Set motor speeds
  motorSpeedA = 0;
  motorSpeedB = 0;

  // Print motor speed to montor.
  Serial.print("Motor Speeds.");
  Serial.print("\t");
  Serial.print(motorSpeedA);
  Serial.print("\t");
  Serial.println(motorSpeedB);
  
  // Send motor speeds to motor
  analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}

Run the Code on your Robot

To run the code on your Robot you will need to connect your Robots Arduino to your Computer using the USB cable.

Make sure you have setup the correct ‘com’ ports and ‘device’ in the ‘tools’ menu. If your have not setup your Arduino, please click the link below….

http://brisbaneroboticsclub.id.au/setup-your-arduino/

To ‘Upload’ your code to the Arduino, click on the ‘upload’ icon.

Click ‘upload’ to upload code to your Arduino.

If your upload was successful, you should see the following…

Successful upload…

Once you have uploaded the code to your Arduino, it will be on the Arduino permanently and the code will automatically start when you power your robot from the battery. You can now disconnect the USB cable.

You can have the Arduino powered by a battery source and connect to your computer via USB. I have done it many times and have had no issues.

To overwrite your code, connect the USB cable and upload some new code using the Arduino editor.

View the Output

The code has been written to display feedback on your computer from the robot when the USB cable is connected.

If the USB cable is not connected, the feedback will now show.

This can be used when determining the sensor height.

To reveal the serial monitor output, select ‘tools’ then ‘Serial Monitor’.

Opening the Serial Monitor.

If your connection was successful, you should see a new window showing the output values.

Serial Monitor providing feedback.

Installing the light sensors.

The light sensors will need to be installed at the correct height to work properly.

By now you should have the line sensor connected to the Arduino.

If it is connected, you can connect your Arduino to your computer using the USB cable. It will provide power to your Arduino and will be sufficient to power the line sensor too.

If power is going to your line sensor, you should see at least one red led lit on the line sensor.

If you do not see any led’s lit, then check your wiring is not loose and is connected properly.

Now get a tile of track and see if the line sensor can detect the line.

Experiment by moving the sensor across the line at different heights.

WARNING: The line sensor detects infrared light. The sun is a source of Infrared light and will interfere with your sensor and cause false readings.

When using the sensor, try and block out all sunlight and other infrared light sources.

It’s best to operate at night when there is no sunlight.

As the line sensor moves across the line, you should see each sensors led turn on and off.

Keep experimenting with different heights off the tile.

Our sensor circuit board is approximately 20mm from the tile.

When you are satisfied with the senor height that works for you, use the standoffs to fix the height.

We used a small and medium standoff to achieve the required height.

Run The Line Follower.

By now you should have completed the line follower assembly.

To run the line follower, upload the line follower code from the previous section.

Create a track using the tile provided.

Now turn on the line follower using the main power switch.

If you have done everything correctly, the line follower should follow the line.

Instructables/Thingyverse.

Coming soon.

Contributors

Marty

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.