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.
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.
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.
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.
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.
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.
BULIDING THE ROBOT
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.
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.
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).
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.
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.
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).
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.
LOOK TO THE FUTURE
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 | www.adafruit.com
EasyEDA | www.easyeda.com
FTDI Chip | www.ftdichip.com
Microchip Technology | www.microchip.com
Pololu | www.pololu.com
SketchUp | www.sketchup.com
Solarbotics | www.solarbotics.com
SparkFun | www.sparkfun.com
PUBLISHED IN CIRCUIT CELLAR MAGAZINE • DECEMBER 2020 #365 – Get a PDF of the issueSponsor this Article