Build an Arduino Line Follower Robot With a PID Controller

13 May 2023 Balmiki Mandal 0 µC - µP

A Line Follower Robot with PID Controller using Arduino

Line Following Robot is a robotics technology for an autonomous robot involving the tracking of a line. This type of robot is commonly used in factories and warehouses. However, it can also be used for educational purposes such as in STEM projects. Usually, these robots are guided by a black line painted on a light surface, and they use sensors to detect the line and adjust the robot’s direction accordingly. PID controllers are often used in line following robots to maintain a constant speed and position along a path.

How to Make a Line Follower Robot with PID Controller using Arduino?

The following tutorial will show you how to make your own line follower robot with PID controller using Arduino.

Step 1: Gather Your Parts and Tools

For this project, you will need the following parts and tools:

  • Arduino Uno - You can purchase this from any local electronics store or online.
  • Chassis for the Robot - This can be anything from a plastic chassis to an aluminum frame. These are typically available at hobby stores.
  • Motors and Motor Driver - You will need two DC motors and a motor driver to control them. A motor driver such as the L293D or TB6612FNG would work.
  • Wheels - Any wheels with rubber tires will do.
  • Line Sensors - These are used to detect the line. You will need two infrared line sensors.
  • Power Supply - You will need a power source such as a battery pack or a regulated DC power supply.
  • Wires and Connectors - You will need various wires and connectors to connect the components.
  • Soldering Iron and Solder - This is used to make permanent electrical connections.

Step 2: Assemble the Chassis

Once you have all of the necessary parts and tools, you can begin assembling the chassis. Start by attaching the motors to the chassis and then mount the line sensors. Once the motors and line sensors are in place, you can connect the wires and motors to the Arduino.

Step 3: Write the Code

Now that the hardware is assembled, you can write the code to control the robot. The code should include instructions for the PID controller, which will make sure the robot maintains a constant speed and follows the line. You can make use of existing PID controller libraries or write your own code.

Step 4: Test Your Robot

Once the code is written, you can test the robot by running it on a black line. If the robot follows the line correctly and maintains a constant speed, it is ready to go!

Here is an example Arduino source code for a line follower robot using PID control:

// Line Follower Robot with PID Control

// Pin definitions
#define LEFT_SENSOR A0
#define MIDDLE_SENSOR A1
#define RIGHT_SENSOR A2
#define LEFT_MOTOR 5
#define RIGHT_MOTOR 6

// PID constants
#define KP 0.5
#define KD 0.2
#define KI 0.1

// Variables
int leftSensorValue, middleSensorValue, rightSensorValue;
int setpoint = 500;
int error = 0, lastError = 0;
int integral = 0, derivative = 0;
int pidValue = 0;
int leftMotorSpeed = 0, rightMotorSpeed = 0;

void setup() {
  pinMode(LEFT_SENSOR, INPUT);
  pinMode(MIDDLE_SENSOR, INPUT);
  pinMode(RIGHT_SENSOR, INPUT);
  pinMode(LEFT_MOTOR, OUTPUT);
  pinMode(RIGHT_MOTOR, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // Read sensor values
  leftSensorValue = analogRead(LEFT_SENSOR);
  middleSensorValue = analogRead(MIDDLE_SENSOR);
  rightSensorValue = analogRead(RIGHT_SENSOR);
  
  // Calculate error
  error = setpoint - middleSensorValue;
  
  // Calculate derivative
  derivative = error - lastError;
  
  // Calculate integral
  integral += error;
  
  // Calculate PID value
  pidValue = KP * error + KD * derivative + KI * integral;
  
  // Update last error
  lastError = error;
  
  // Set motor speeds based on PID value
  leftMotorSpeed = 150 - pidValue;
  rightMotorSpeed = 150 + pidValue;
  
  // Limit motor speeds to between 0 and 255
  leftMotorSpeed = constrain(leftMotorSpeed, 0, 255);
  rightMotorSpeed = constrain(rightMotorSpeed, 0, 255);
  
  // Set motor speeds
  analogWrite(LEFT_MOTOR, leftMotorSpeed);
  analogWrite(RIGHT_MOTOR, rightMotorSpeed);
  
  // Print sensor and motor values to serial monitor
  Serial.print(leftSensorValue);
  Serial.print(",");
  Serial.print(middleSensorValue);
  Serial.print(",");
  Serial.print(rightSensorValue);
  Serial.print(",");
  Serial.print(leftMotorSpeed);
  Serial.print(",");
  Serial.println(rightMotorSpeed);
}

This code assumes that the line follower robot has three analog sensors for detecting the line and two motors for controlling the movement. The PID constants KP, KD, and KI can be adjusted based on the specific requirements of the robot. The setpoint variable represents the desired sensor value for the middle sensor when the robot is following the line. The code reads the sensor values, calculates the error, derivative, and integral terms for the PID controller, and adjusts the motor speeds accordingly. The code also prints the sensor and motor values to the serial monitor for debugging purposes.

Conclusion

Line following robots are a great way to learn about robotics and engineering principles. With the right parts and a bit of coding knowledge, you can have your own working line follower robot using Arduino and PID controller in no time.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.