• Control Servomotor For Making Robotic arm with Leapmotion and Arduino
  • Control Leap Motion Technology with Arduino
  • SureFly, a New Air Taxi That Runs On Electricity—and Gasoline
  • Navigate Like a Rat
  • Arduino Line following Robot


PID algorithm implementation (PID Controlled Line Following Robot)

PID line following robot using pic complete code


  1. Introduction to PIC Microcontroller (PIC 18f4550)
  2. Introduction to PID algorithm 
  3. Sensor selection
  4. PID Controller Algorithms
  5. Code & Tuning
  6. SCHEMETIC and simulations
  7. Code
The basic purpose of this project is to develope the understanding of PID algorithm.Some of the basic things about making robot will not be covered here.For this you have to see the robotics section about making robot and other related stuff lilke motor selection motor driver etc.the core objective is to discuss the implementation of PID control system.


Components that we have use in building a PID base Line following Robots:

  1. ·       PIC microcontroller 18f4550
  2. ·       IR sensor
  3. ·       Chesses and Motor
  4. ·       LM 324
  5. ·       PID algorithm

  1. Introduction to PIC Microcontroller (PIC 18f4550)

      There were two main alternatives regarding the choice of microcontroller: PIC18F family (specifically the PIC18F4550, due to familiarity, and it having the needed features and number of ports), or Arduino (most likely the Nano or UNO, since both have enough processing power and I/O pins for it to be unnecessary to consider the more powerful models). 
                  PIC 18f4550:
            The PIC18F4550 is the highest performance USB-equipped model in Microchip, INC. popular PIC18 8-bit microcontroller line. It features (Microchip, Inc. 2009b, Microchip Inc. 2009a):

      32 KB Flash Program Memories.
      A 12 MIPS CPU
      2KB RAM.
      256 byte data EEPROM
   1 x A/E/USART Port
     1 x MSSP (SPI/I2C) Port
      1 x CCP & 1 x ECCP
      1 x 8-bit & 3 x 32-bit Timers
      13 x 10-bit Analog Inputs
       2 x Comparators.
      1 x USB 2.0.
      35 x Digital I/O Pins

This microcontroller has been used extensively. It can be programmed natively in Microchip PIC assembly, or in C18, Microchip’s implementation of the C programming language for use with their PIC18 line. Due to their popularity, there exist a great number of open-source projects developed for the PIC18F line, along with libraries, tutorials, and many articles on coding and developing using these microcontrollers. They are also used to great extent professionally.

2-Sensor selection

IR sensor:

 Depending on the accuracy we have choose IR sensor. The basic concept of IR(infrared) is  obstacle detection by transmitting a IR signal(radiation) in a direction and  a signal is received  at the IR receiver when the IR radiation bounces back  from a surface of the object. For Ir object can be anything the IR LED transmits the IR signal on to the object and the signal is reflected back from the surface of the object. The reflected signals are received by an IR receiver. The IR receiver can be a photodiode / phototransistor or a readymade module which decodes the signal.

 ir sensor working

 IR Transmitter:
 IR transmitter is modulation of the information signal with carrier signal, because the receiver modules which are available off-the-shelf are made for a particular carrier frequency. So it is clear that when you chose a particular IR receiver module, you also need to transmit the modulated wave with the same carrier frequency of that of a IR receiver module. Modulating a 38 Khz carrier signal.
IR Receiver:
A IR receiver with readily available off-the-shelf modules.the receiver is designed for 38 kHz carrier signal; hence the IC selected should work for the same frequency. The IC TSOP4838 will serve as a receiver module, which is compatible with both TTL and CMOS logic. This means that we can directly get digital signal from the receiver module and then connect it to the microcontroller. The Implementation of IR receiver is explained using an LED as an indicator.

3- Introduction to PID algorithm: 

Proportional-Integral-Derivative (PID) control is the most common control algorithm used in industry and has been universally accepted in industrial control. The popularity of PID controllers can be attributed partly to their robust performance in a wide range of operating conditions and partly to their functional simplicity, which allows engineers to operate them in a simple, straightforward manner. 
As the name suggests, PID algorithm consists of three basic coefficients; proportional, integral and derivative which are varied to get optimal response.

Proportional Response:
The proportional component depends only on the difference between the set point and the process variable. This difference is referred to as the Error term. The proportional gain (Kc) determines the ratio of output response to the error signal.

Integral Response
The integral component sums the error term over time. The result is that even a small error term will cause the integral component to increase slowly. The integral response will continually increase over time unless the error is zero, so the effect is to drive the Steady-State error to zero. Steady-State error is the final difference between the process variable and set point. A phenomenon called integral windup results when integral action saturates a controller without the controller driving the error signal toward zero.
Derivative Response:
 The derivative component causes the output to decrease if the process variable is increasing rapidly. The derivative response is proportional to the rate of change of the process variable. Increasing the derivative time (Td) parameter will cause the control system to react more strongly to changes in the error term and will increase the speed of the overall control system response. Most practical control systems use very small derivative time (Td), because the Derivative Response is highly sensitive to noise in the process variable signal. If the sensor feedback signal is noisy or if the control loop rate is too slow, the derivative response can make the control system unstable
 PID algorithm

4-PID Controller Algorithms:

1 In a case when robot is deviated to left it then pushes to move towards right
2. In ac case of deviation towards right it is forced to move towards left

       The accomplishment of this task gives our robot is following the zigzag path
       Using this method the speed we were giving was much less as error getting by using pid. And this technique was also not reliable for the turns. To apply this algorithm it was better to use the error calculation method after calculating error giving motor its proportional value and them it changes speed and in turn it gets back to the line.
e.g.-the deviation ranges between -3 to +3.

         Kp is the proportionality constant it will give negative value when robot is be towards left of the line then the right motors gets slows down.
This is a good practical approach for many cases. But not suitable because of these reasons

1. It affects accuracy of the robot when the robot is not much effective it gets back to mean position very slowly.

2. To solve this problem we have to increase its proportionality contact but it will then overshoot when robot comes to mean position due to inertia-cross to other side then again overshoot results in deviating result.

PID Controller Algorithms:
PID is a feedback mechanism.
It has three components on which it works that are

1: error
2: time of deviation
3: overshoots

Efficiency of the PID algorithm is based on these three parameters these helps to reach at mean position much faster

 The three constants are set by hit and trial method experimentally.
  1. Kp
  2. Ki
  3. Kd

Using only kp parameter the robot will be applied and give the results as the classical ine following algorithm discussed earlier.
Ki: because of ki robot is forced to follow mean position.
Kd: kd does not allow the robot to perform any deviation


Given below is the code for measuring the deviation
‘00000001’ this means robot is towards rightmost this code works in a manner that is robot is at right the deviation will be recorded negative
 PID algorithm implementation

Getting these results PID then sums all the previous deviations and generates an integral called total error. The difference of the deviation is measured by derivative. The error calculated is then send to this code it will give the correction
These lines should run in each iteration.


PID tuning is greatly affected by the floor where it is going to be use and the track where robot is going be run and these physical parameters cannot be calculated mathematically. For example motor inductance, friction centre of mass etc. these quantities can be just guessed and applied by hit and trial method.

 Sensor circuit:
Sensors are the array of 7 IR sensors which are connect with operational amplifier LM324 which converting the analog output of sensor into digital by comparing the threshold level of line then the digital output of opamp are given to microcontroller.

 Main circuit:
The sensor array that we have made in last lab will be connected to PORT B. the motor driver IRFZ44N is a Mosfet  we have us Mosfet due to the fact that it can drive high voltage and current is per requirement we need to have a motor driver that has capability 
to handle high voltage and current. For the accuracy of the robot and requirement of the algorithm we selected IRFZ44N. To control the motion of the motors and to control the speed of the motors a PWM waveform with variable duty cycle is applied on the gate of the IC. The main circuit of the robot is on 18f4550 which is most popular micro controller of pic micro’s having 12 bit ADC resolution 8 analog to digital pins which are used for tuning PID occupied by potentiometers 2 CCP modules which controls PWM given to motors which ultimately controls the speed of robot having 33 I/O pins in which 8 is used for reading data from sensors and 8 are used in 16x2 LCD display interface circuit. Microcontroller take decision on the base is of sensor outputs and after applying PID it controls the motion of robot by changing speed of motors accordingly.

Motor driver:
Our motors are derived by IRFZ44N.
IRFZ44N is a motor driver made up of mosfets. Only code is not sufficient for desired results.
By continuously hit and trials it will effect more. It is not necessary to use all three parameters P,I and D.
If only PI is enough it is not necessary to use derivative part.
PID also requires settling time that is why it will also consume this time in showing accurate results.


PID line following robot circuit and complete code


The robot is successfully following the given arena.
Output is very much accurate and deviations are much reduced to very low level.

In this project our main focus was to cover the PID algorithm implementation.some of the sections about making robot are not included here.

Below is the Code of PIC micro-controller. Click on the download button to get complete code file and practice it.


All rights reserved www.makeitmech.com
PID line following Robot Code:

#include <18f4550.h>
#device adc=10
#use delay(clock=20M)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#include <lcd.c>
\\Variable Initialization 
char c;
float value,pwm1,temp,pwm2,error,preverror,totalerror,kp,ki,kd,filter0;

void main(void)
enable_interrupts(INT_RDA);  //Serial Interrupt Enable 
enable_interrupts(GLOBAL);  //Global Interrupt Enable
setup_ccp1(CCP_PWM);   // CCP1 Initialization 
setup_ccp2(CCP_PWM);   // CCP2 Initialization
totalerror=0;    // PID LOOP Variables

Download Complete Code:

Next Post »

Video Gallery


click here