Introduction: Arduino PID Code for Line Following Robot

Picture of Arduino PID Code for Line Following Robot

In this instructable, you will learn how to make Arduino PID codes for any task, some mathematics, and design robots that are more efficient than others.

To start, watch the video above to see how the PID line following operates, in this example, we used a sophisticated infrared sensor to read the line, however, with this code, you can control it using a simple LDR (Light Dependent Resistor).

Step 1: P: the Proportional Term

The proportional term makes the current Error Multiplied with a gain (Kp), the result will be the output signal.

So OUTPUT-SIGNAL=Kp*Error-signal

Most 8-bit Microcontrollers can do this mathematical problem, it wouldn't be necessary to look further from an Arduino or a PIC.

Step 2: I: the Integral Term

Picture of I: the Integral Term

The Integral Term makes the current Error signal value and duration multiplied with a gain, the result will be the output signal.

(the math is shown above);

where K1 is the integral gain, t is the instantanious time, e(t) is the error signal.

The integral of a signal is the sum of all the instantaneous values that the signal has been, from whenever you started counting until you finished counting.

The Integral Term when added to the proportional term accelerates the movement of the process towards set-point.

Step 3: D: the Derivative Term

Picture of D: the Derivative Term

The derivative term makes the rate of change of the error signal multiplied with a gain (Kd). The result will be the output signal value.

Where Kd is the derivative gain, and e(T) is the error signal.

The derivative term slows the rate of change of the controller output and this effect is most noticeable close to the controller's setpoint.


BivekG (author)2017-01-09

please inbox me the code at

BivekG (author)2017-01-09

i need code of this line following robot please

poncos (author)2015-11-26

hey how you use sensor line ? why in ur sketch only read analogread(A0) ? but in ur video use a lot of sensor?

Josaiphe (author)poncos2015-11-26

In the video it was a first virgin that i designed 6 months ago, but I wanted to make simpler for everybody, so I used just the A0 pin of the arduino, and it works like a charm, also if you like it don't forget to vote for it in the robotics contest.

poncos (author)Josaiphe2015-11-27

u only use 1 sensor line (1 photodiode ) ? or how ?

Josaiphe (author)poncos2015-11-27

yes, you can use an LDR but a phototransistor is better, however make sure that the output wire is connected to the arduino, and make sure that the sensor gives a bigger signal in the white range and a lower signal in the black range.

poncos (author)Josaiphe2015-11-28

wow i just now know line followeronly using 1 sensor ._. btw where i took my sensor? in the black or white ?

Josaiphe (author)poncos2015-11-28

In the PID control you set the robot to see the thin line between black and White, but you need to set constants to work with your lighting. Also, Please vote for this if you like it in the robotics contest, to vote just press the "Vote" button in the Up right corner, and choose the contest.

poncos (author)Josaiphe2015-11-28

hmmm i still dont get it, because as i know i need min 2 sensor for line follower where the line between my right and left sensor

Josaiphe (author)poncos2015-11-29

yes but here we used one for simplicity, and it works like a charm

About This Instructable




Bio: Hi, I am 17 years old and I love making new things and learn something new along the way, so E-Mail me at: josephabulail@yahoo ...
More by Josaiphe:Arduino PID code for Line Following RobotThe Simplest Pan Cake RecipeAutomatic Light System
Add instructable to: