Projects Research & Design Hub

Build a Line Follower Robot

With Arduino and PID Control

Since the start of the 21st century, robotics technology has advanced dramatically and robots have come to play important roles in industry, as well as other fields. The line follower style of robot has emerged in recent years, able to replace people in tiring and dangerous activities.

  • How to build a line follower robot
  • What is the general concept of a line follower robot?
  • What are the hardware components?
  • How to implement the motor control
  • How to create the PCB
  • How to understand the PID control
  • What is the future of PID control?
  • Arduino Pro Mini 328 – 5V

  • FTDI Chip’s FT232RL USB-to-TTL serial converter
  • USB cable
  • 5V step-up/step-down voltage regulator S7V7F5 from Pololu
  • DRV8835 dual motor driver carrier from Pololu
  • 7.4V LiPo battery + charger
  • Ball caster with 3/8″ metal ball
  • 2x Solarbotics RW2i wheels
  • QTR-8RC reflectance sensor array from Polulu
  • HC-05 Bluetooth module
  • A switch
  • 2x small buttons
  • 4x 1kΩ resistors

A line follower robot has a simple algorithm, although it has evolved in recent years. A line follower robot is designed to detect and follow a line drawn on the floor. This line follower is a concept can be used in many situations, because of its simplicity of design and implementation. The advantages of the concept are its flexibility and the fact that it is autonomous—in other words, it can be used for long periods without monitoring.


There are many applications for the line follower in our world today. It is used especially in industrial settings, performing tasks such as transportation, manipulation of different objects and as a replacement for conveyer belts. They are also well suited for domestic applications, such as floor cleaning or as service robots that follow you. In the future, the concept of the line follower is expected to be applied in several more markets, including medical, transportation and construction.

In the medical field, a line follower can be used for automated stretchers that carry patients or as a tool for optimizing the patient reception system. Another utility in this domain is bringing food and drinks to patients in infectious disease wards, where medical staff aren’t able to safely interact with patients.

I designed this line follower robot for a competition (Figure 1), where the robot that finishes the racetrack in the shortest amount of time wins. I struggled to find a tutorial that exactly explains the line following process, or one that explains the principles by which it works. After months of research, I managed to assemble an acceptable model that can take curves at a relatively high speed without getting off the track.

FIGURE 1 – This is the final version on my Line Follower unit. You can see both the dynamic shape and all the components that have been used, including the PCB and 3D printed parts.

In the end, the project cost me $154 (€130), including the PCB, 3D printing and all the components. As I previously mentioned, speed and control are the most important factors in building a line follower robot. But there are also some other key requirements that had to be met for the contest. For instance, the robot needed to have dimensions smaller than 30cm × 30cm and the curve radius of the track had to at least 7.5cm. With that in mind, I designed it as a “racing car”—with two motors in the back and a ball caster in the front. I put a Polulu QTR-8RC sensor array in the front of the robot, so that the ball caster will always be on the line.

The concept of how a line follower works is rather simple. As shown in Figure 2, the sensor array reads the position of the line and gives the value to the Arduino. The Arduino processes the value and it is transformed—with the help of PID control—into a value that controls the motor speed. In other words, the Arduino compares the given position value to a setpoint (the ideal line position—when the line is on the center of the sensor array). If the error is positive or negative, the robot will turn left or right, respectively.


Advertise Here

FIGURE 2 – This diagram details the concept of how the PID control system works. It shows how PID control influences the speed and control of the robot’s motors.

I made a list (Table 1) of all the hardware components that I used in this robot. They can be found in any electronic store. Aside from what’s shown in Table 1, M3 and M2 screws and nuts are also required, as well as a connector for the battery. Also, a screw terminal block with two pins may help in connecting the battery to the circuit. A soldering iron is required to connect the components on the PCB board. The schematics of the circuit are shown in Figure 3.

TABLE 1 – Shown here is a list of all the hardware components that I used in the robot.
FIGURE 3 – This schematic shows how wiring can be done, as well as the pins of the parts that need to be connected. It is important that the wiring is accurate because different components need certain special pins on the Arduino board (Tx and Rx, PWM and so forth).
(Click to enlarge)

For a high-speed robot, there are a wide variety of motors that could be used. The two important factors are the RPM (the higher the value, the faster the motor will spin), and the torque (the smaller the value, the motors will accelerate harder and the robot may not start without a push and they may even slip off the track). In other words, the RPM determines how fast will the robot go—how many rotations it makes around its axis in a minute—and the torque represents the force with which the motor rotates around its axis (influences the acceleration of the motors). Each motor has a gear ratio that proportionally controls the relationship between the RPM and the torque. The higher the RPM, the smaller the torque is and vice versa. For my robot, I used a high-power 6V brushed DC motor with a 10:1 metal gearbox.

The Polulu DRV8835 is used to control two DC motors and features two possible control modes: IN/IN (Coast mode + Brake mode) and PHASE/ENABLE (only brake mode). In Brake mode, letting go of the sticks causes the robot to stop fairly quickly. Meanwhile, in Coast mode, the robot rolls to a stop over a sometimes-considerable distance. It is recommended that you use the second mode because it provides a more linear relationship between PWM duty cycle and motor speed.


The control of the motors in brake mode can be made in two ways: They can spin in only one direction (forward) or in two directions (forward and backward). It’s recommended that the motors spin in only one direction, because switching the direction can damage them. However, if the motors spin in two directions, the robot can take tighter curves—the robot spins around its base.

The Arduino Pro Mini is a microcontroller (MCU) board designed for projects where space is limited and where projects are made as permanent setups. It’s available from Adafruit, SparkFun and other sources. It is based on the Microchip Technology (formerly Atmel) ATmage328P MCU and has 14 digital I/O pins (which can be used as PWM outputs), 6 analog inputs (which, in this project, are used as digital inputs) and two serial pins (TX and RX). There are enough pins to wire all the components, as you can see in Figure 3. There are eight digital I/O pins for QTR-8RC sensor, four PWM outputs for the motor driver and one I/O pin for LEDON from the QTR-8RC. The QTR-8RC controls a MOSFET gate, two serial pins for the HC–05 module and two digital I/O pins for the two buttons.

The QTR-8RC reflectance sensor array (Figure 4) has 8 IR LED/phototransistor pairs, making it a great detector for a line follower robot. The sensor provides 8 different digital I/O-measurable outputs. To use the sensor array in an Arduino project, a specific library must be downloaded, found in the Arduino IDE. Go to Sketch > Import Library > Manage Libraries > and search for “QTRSensors.” To access all the QTRSensors methods (read a line, calibrate the sensors), #include <QTRSensors.h> must be typed at the top of the code. In File > Examples > QTRSensors you can find more documentation on how the sensor works.

FIGURE 4 – This is the QTR-8RC sensor array. You can see that each sensor is labeled, so it can provide a variety of line position values from 0 to 7000.

The sensor array gets readings from all 8 sensors and transforms them into a value from 0 to 7,000. Each value that is divisible by 1000 corresponds to a sensor (0 to the first sensor, 1,000 to the second sensor and so on.). The setpoint (the ideal value), where the robot will be on the center of the line, will have a value of 3,500—between the 4th and the 5th sensor.


To design the robot, I chose to use a PCB and printed 3D parts instead of the classic breadboard with wires. The advantage of PCBs is that they save space and there is no problem that the wires can come off (as with the breadboard) causing confusion and weak connections. The advantage of the 3D Printing is that the parts can be modeled and printed according to the user’s taste. Also, the price isn’t very high. For the PCBs, I paid $6.90 for five pieces and $25 for the 3D printed parts. Some of my local electronic Arduino shops have a 3D printer.

Designing the PCB isn’t too difficult. To start, the circuit schematic (Figure 3) must be created in order to implement the PCB. I used the web-based EDA tool EasyEDA. To create the schematic, models for the electrical components (Arduino Pro mini, S7V7F5 and so on) must be either downloaded or self-made. They must be provided with a PCB model, as shown in Figure 5.


Advertise Here

FIGURE 5 – This is the PCB design layout, showing where I placed all the components and wired them. You can see how I used both the front (blue traces) and the back (yellow traces) of the board to implement the wiring.

When the wiring is done, the model can be converted into a PCB. In the PCB section, the outline of the PCB must be made, as well as the placement of components. After that, the wiring must be done again, to account for the component positioning. All that said, the wiring done in the schematic creation was not made in vain. It creates ratlines to mark the connections between the pads of the components. The wiring can be done manually or using the auto-router provided by the EasyEDA software. In the end, the PCB created can be converted to a GERBER file and sent to any PCB fabrication company.

The modeling part of the robot can be more difficult for those who have never used CAD software. For my part, I used SketchUp, because it is free and has an accessible user interface. It also features the ability to export the SketchUp file into a STL (Standard Triangle Language) file, ready to be 3D-printed. The CAD model must be made in such a way that all the parts are combined, as shown in Figure 6. This software is easy to use once you understand how it works, and you can create more complex shapes for the model. It has all the tools you need, such as measurements, different shapes (triangles, rectangles, angles), methods by which you can manipulate them (push/pull, scale, curve, and access to other people’s models on the web.

FIGURE 6 – In this image, I’ve deconstructed the model to show all the connecting parts, which, once merged, form the complete system.

For some of the components—such as the motors and the array sensor—the 3D file can be found on the web and can be imported into the model. But some of the parts that need to be printed have to be manually designed. There are a few tips in designing a robot. First, the parts must have a certain thickness (I used 2.4mm), in order to not bend or even break. Second, a distance between certain parts is recommended, in the case of some of the electronic parts (Arduino, motor driver) and the battery holder. This also applies for screws, because there are a lot of lengths for them and they could either enter certain printed parts or touch the track. To avoid this, the screws can be cut, or the design can be made in such way that it includes the screws and are therefore no longer a problem.

Finally, the dimensions of the robot may vary depending on what it is intended for. The line follower competitions have some restrictions, for example the robot’s maximum dimensions are 30cm width × 30cm length and the maximum weight is 3kg. The curve radius of the track also particularly influences the design. As is shown in Figure 7, the tangent of the triangle (the blue line), formed from the line between the two tires (red) and the line between the center of the PCB and the ball caster (green), will be the minimum curve radius the robot can take (in ideal conditions).

FIGURE 7 – This shows how both the length of the PCB and the length from the wheels to the sensor determine the minimum radius of the curve that the robot can take.

A line follower can work, even with the simplest code. Basically, it compares the setpoint (ideal position of the line) of the sensor array with the current position of the line. The obtained error will determine if the robot will turn right or left, respectively. Over time, the algorithm has been improved. The goal of improving the code is to make the line follower a robot that goes fast, on any track, without oscillating or getting off it. The best solution is the PID control (Figure 8), which helps keep the robot on track. It is a feedback-based control system that continuously calculates an error (between the setpoint and the actual value of the sensor array), and applies a correction, based on three control terms—proportional (P), integral (I) and derivative (D), hence the name PID.

FIGURE 8 – This diagram details how the PID control system works, in correlation with the sensor array and the motor speed.

These three control terms are summed to calculate the output of the PID controller—in this case the motor speed. Each term has its own purpose and influences differently how the robot will follow the line. They are accompanied by a constant, which are multiplied by each other. In the end, the line of code that assigns a value to the motor speed is:

int motorspeed = Kp*P + Ki * I + Kd * D;

To complete the follower line code, finding the constants of each control term is the most important part. They can have any value. For example, for my robot I took Kp as 0.07, Ki as 0.001 and Kd as 0.6. Changing the values can be done manually, in the Arduino IDE, or adding a Bluetooth module in which you can adjust these values directly from the phone.

The proportional term gives a value directly proportional to the error between the setpoint and the current position. It directly controls how the robot will take a curve or move on the line. The more the line is in the center, the more the robot will go straight. The closer the line is to the edges of the sensor, the faster it will turn. But using only this constant isn’t enough, because the robot will oscillate. The proportional constant (Kp), sets up the speed sensitivity of the motors. The following line of code is the set up for the proportional term, without it being multiplied by the respective constant:

int P = error;

The integral term is the sum of the error in a period of time (since the program started). The result is a value that indicates how much the robot had to be corrected on the track. When the error is 0, that means that there is no need for increase, and the robot is on the right path. After that, it is multiplied by the integral gain (Ki). The integral term helps the line follower robot to take the curves more smoothly and will help the robot center on the line faster.

int I = I + error;

The derivative term is the difference between the last value of the sensor array and the actual value. It makes the control system react more strongly to error changes and helps stabilize the robot in less time. It can also be multiplied by the derivative gain (Kd), and helps the robot to take sudden tight curves.

int D = lasterror – error;

But, if only one term is used in controlling the robot, then it will not work properly. It will stay on the track, but it will oscillate. Each term has its role and, once combined, they will combine harmoniously as shown in Figure 9—resulting in a robot that can follow a track at high speed without derailing.


Advertise Here

FIGURE 9 – Here, you can see how the speed of the two motors is not stable on a straight-line track. It oscillates when one (a) or two ((b) and (c)) control terms are used. Once all three terms (d) are used, the robot goes in a straight line. Y-axis units = PWM duty cycles.
(Click to enlarge)

As I said, the algorithm for tracking a line is simple with PID control because it provides real and fast feedback. The only difficult thing to do manually for this controller is setting the constants of each control term—Kp, Ki and Kd. Once the values are found, the robot should follow any type of track, which can be tracked within the physical limits of the robot (Figure 10).

FIGURE 10 – This is the track I used to test the robot. I made it with A3-sized papers and PVC black tape.

To find those constants very fast, instead of uploading the code every time, they can be set using serial communication between the receiving side (HC-05 to Arduino) and the transmitting side (phone or other device with Bluetooth in this case). All long-distance communication transmits data one bit at a time, so, to transmit all data in special low values with a lot of decimals, I made a second value for each constant that divides them by a power of 10. For each of the instruction, the app sends two bytes. The first one sends the type of the instruction (P, I or the buttons) and then the second byte sends the value of the instruction.


In the future, I believe that this type of robot will play an important role in everyday life. Although the PID control system is suitable for this type of robot, there is room for improvement. One such improvement would be an AI (perception type) of system that can process the track before the robot is there. That would be an interesting idea to implement and perfect. In addition to sensors, it would use a video camera that would be manipulated with the help of an object-detection AI. The disadvantage is that it would need a processor, taking up more space—an issue for small projects. All that said, for large platforms—like vehicles—this should not be a problem.

As I said earlier, a line follower robot has the advantage that it can be used without human help. It can perform tasks in certain fields where there aren’t enough staff (medical field), or where work is much too dangerous (industrial) or for simply replacing boring or long-term activities (driving). 

Today’s robots play important roles in many fields of activity. In this project article, Alexandru shares his Arduino Line Follower design—a simple robot that can follow a line. The robot uses PID control as a feedback control system in order to optimize both the robot’s speed and its accuracy following a line.

Adafruit |
EasyEDA |
FTDI Chip |
Microchip Technology |
Pololu |
SketchUp |
Solarbotics |
SparkFun |


Keep up-to-date with our FREE Weekly Newsletter!

Don't miss out on upcoming issues of Circuit Cellar.

Note: We’ve made the May 2020 issue of Circuit Cellar available as a free sample issue. In it, you’ll find a rich variety of the kinds of articles and information that exemplify a typical issue of the current magazine.

Would you like to write for Circuit Cellar? We are always accepting articles/posts from the technical community. Get in touch with us and let's discuss your ideas.

Sponsor this Article
+ posts

Alexandru Dumitrache is a student at the Tudor Vianu National College of Computer Science in Bucharest, Romania. His passions are technology robotics, electronics and he is keen on developing projects (with Arduino and Raspberry Pi) that can change people’s lives for the better. His focus is on changing society by reducing pollution, new ideas for utility robots and the implementation of eco energy and intelligent systems. In the future, using his PCB and CAD design skills, as well as my programming knowledge in C/C++ and Python, he wants to add AI into projects that develop beneficial devices. Contact him at [email protected]

Supporting Companies

Upcoming Events

Copyright © 2021 KCK Media Corp.

Build a Line Follower Robot

by Alexandru Dumitrache time to read: 14 min