Arduino PID Code for Line Following Robot





Introduction: Arduino PID Code for Line Following Robot

About: Hi, I am 17 years old and I love making new things and learn something new along the way, so E-Mail me at:

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

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

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.



    • Creative Misuse Contest

      Creative Misuse Contest
    • Oil Contest

      Oil Contest
    • Clocks Contest

      Clocks Contest

    11 Discussions

    hi i need the code for the pid controlled line following robot using arduino uno for 4 ir sensors and its urgent


    1 year ago

    please inbox me the code at


    1 year ago

    i need code of this line following robot please


    2 years ago

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

    7 replies

    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.

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

    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.

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

    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.

    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

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