Part 1:: Line following robot :: code guideline arduino using PID

The PID algorithm uses three constants, Kp, Ki and Kd to function. They are shorthand notations for proportionality, integral and differential constants respectively. These three constants have to be set by you after testing, and define how good your control works.
Now let’s look at how to develop a simple PID control algorithm.

Use the Arduino function analogRead() to retrieve sensor values. You’ll need an array to store these variables. Use Serial.print() to display these sensor values and observe them through the serial monitor in the Arduino IDE. If you’re using five sensors connected at analog pins 0-4, your code would look somewhat like this;

void setup(){
Serial.begin(9600);   //Necessary to set up Serial port
void loop()
Serial.print(' ');
Serial.print(' ');
Serial.print(' ');
Serial.print(' ');
Serial.println(' ');
delay(1500);              //Set the number to change frequency of readings.

Burn this code onto the Arduino and place your bot over a sample track you’ve made. Move it across the line and observe the sensor readings. The reading will be a number between 0 and 1023. Find out what reading comes up for white and black, and any other colors you may want.

The sensors connected from analog pins 0-4 should be in order from left to right. If you’re using 5 sensors, the leftmost should be connected to pin 0, the middle one connected to pin 2, and the rightmost one to pin 4.

Set Point
It is the position the robot is stable in, in our case in dead center of the line. This is the position the control algorithm strives to achieve. Familiarize yourself with the Arduino language.

We use two new quantities in our algorithm, an average of the sensor readings and sum of the sensor readings. You’ll understand why we need these two when you move a little further in the algorithm development.

The below code displays the sensor average, sensor sum and position via the serial port. Use the serial monitor to view the readings.

int position;
long sensors[] = {0, 0, 0, 0, 0};   // Array used to store 5 readings for 5sensors.

void setup(){

void loop(){
sensors_average = 0;
sensors_sum = 0;

for (int i = 0; i < 5; i++){
sensors[i] = analogRead(i);
sensors_average += sensors[i] * i * 1000;   //Calculating the weighted mean
sensors_sum += int(sensors[i]);}            //Calculating sum of sensor readings
position = int(sensors_average / sensors_sum);
Serial.print(' ');
Serial.print(' ');

You need little understanding  of mathematics and kinematics to understand the calculations of the weighted mean and position.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: