Skip to content

This page is archived material from a previous course. Please check for updated material.

    Lab 5

    Lab 5 – PID Control


    • Build and characterize a servo system based on PID control.
    • Learn to tune PID parameters to obtain optimum response and stability.


    1. Read a few primers on PID control:
      1. Wikipedia entry – focus on explanation of terms, manual tuning.  PID Control, Wikipedia »
      2. Articles on PID control including walk-throughs, and a few other subtleties:  PID Control, from »
      3. Writing PID control loops for a microcontroller: »
    2. For this lab, you will be using a single-turn potentiometer (included in your electronics kit, has 3 wires and is screwed to a piece of meccano).  The 3 wires may not necessarily be colour-coded in any way.  You need to figure out how to figure out how to use a Digital Multimeter to determine which leads are which on the potentiometer; incorrectly hooking up the potentiometer means possibly shorting your power supply to ground!    A hint can be found by reading up about potentimeters.  Potentiometers as Rheostats »
    3. Attempt to write software to implement the PID control loop as described in the lab below.

    Additional Readings

    PID control is an algorithm to provide a control signal based on an error signal, the integral of the error signal over time, and the time derivative of the error signal. The resulting equation is:

    This can be implemented in an analog circuit by using Op-amps and filters to perform the integration and differentiation functions. For this lab, you will be implementing PID in software.  In that case you will execute a loop (loop index i) where

    (i) = sensor voltage(i) – setpoint voltage

    (i) = ((i) – (i-1)) / T

    where T = the number of seconds spent in each loop (this can be omitted and lumped into the gain constant D)

    where error_sum is incremented each loop:

    error_sum = error_sum + T *(i);

    The gain variable T allows you to slow the rate at which the integral accumulates should it be desirable to do so (this is usually the case if your algorithm executes much faster than the rate at which you can perform the control).

    To avoid integral “wind-up” (where the value of the integral term becomes too large following a sustained error), software limits are applied to error_sum:

    if (error_sum > high_limit)

    error_sum = high_limit;

    if (error_sum < low_limit)

    error_sum = low_limit;

    this prevents error_sum from reaching absurdly high or low values which the P gain will not be able to overcome should the error suddenly decrease.

    Tuning P, I, and D gain variables is often done by trial and error.    Some more precise tuning methods are shown in the primer.

    Alternate algorithm

    An alternate form of the digital PID algorithm can be found in “Microprocessors in Instruments and Control” by R.J. Bibbero.    This algorithm is inherently “anti wind-up”:



    dOm =amount to be added to the output voltage each loop (Vout += dOm)

    Verror(i)= error voltage during ith iteration

    G = total loop gain

    I = Integral gain

    D = Derivative gain

    T = Time interval between each loop execution


    This is a relatively long lab, and you are allotted up to 2 weeks to do it, but please aim to do as much as possible (or possibly finish the lab) in the first week, so you’ll have more time to discuss and work on your robots sooner!


    1. Build a servo mechanical linkage. Build a linkage mechanism using Meccano gears or pulleys to connect a 12V motor through a gear train or pulley to a single-turn potentiometer.
      1. Use either the Geared Barber Coleman motor, or possibly one of the 6V solarbotics motors.  We recommend that you avoid using the ungeared Barber Coleman motor, they turn very fast and it is more difficult than necessary to build a servo-mechanical linkage, at least for this lab.
        1. There are small brass shaft adapters used to connect the Geared Barber-Coleman motors to meccano shafts.  There aren’t many of them, so ask your TA/Instructors for them.
      2. Set this up so that one full turn of the motor causes the pot to turn through its full range.  You may instead want to use a lower gear ratio (e.g. multiple motor turns to get the potentiometer to turn once), but avoid higher gear ratios, since you lose precision in your turning that way.
      3. Use Tygon tubing to connect the potentiometer shaft to Meccano shaft.  This will act not only to couple the two systems together, but also to protect the potentiometer from breaking (if turned very hard, the Tygon will slip on the shaft instead of breaking).
      4. You will have to ensure that your drive train is not set too loose or too tight.  Friction will play a big role in this lab, as well as proper alignment and support of your meccano shafts in 2 places (only 1 support means it bends out of alignment, more than 2 means the shaft is over-constrained and friction can cause problems).
    2. Power your motor. Wire up your motor driver circuit to connect the TINAH Board motor output to your 12V motor.   You do not necessarily need to hook up the motor through the h-bridge you constucted last week, for most groups going directly to the TINAH motor outputs will be fine.
    3. Get position information into the TINAH board. Wire the potentiometer as a 5V voltage divider (as done in Lab 3) and connect it to a TINAH board analog input.
    4. Write your PID software algorithm. Write a PID control algorithm to compare the potentiometer voltage to the TINAH Board’s knob value, and adjust the motor position to make the difference between these two voltages zero.
    5. Tune your PID control. As described in the lecture and in the pre-lab readings, tune your PID loop to provide zero steady state error, and optimum response time with minimum overshoot.    Initially you will want to set I=D=0 and use only proportional gain for tuning the system.   Qualitatively explore the response time and steady state error of your servo system, then increase gain, and add I,D gain to see what happens.
    6. Drive your system with an external signal.
      1. Set I=D=0 again.
      2. Connect the function generator to an analog input of the TINAH board through an op-amp as in lab 3.
      3. Modify your code to add this function generator signal to the knob value you are using as a set-point.
      4. Produce a low frequency (0.1  Hz) square wave with the frequency generator, and observe the response of your system (measure the pot voltage with the scope – this voltage is proportional to the rotation angle of the motor).
      5. Increase the frequency until the system can no longer keep up mechanically with the input signal.
    7. Change the inertia of the system. Add a large moment of inertia (e.g. a long Meccano girder) to the gear or pulley that is driven by the motor and see how this affects your servo performance. Try to re-tune your PID for optimal performance.


    • Demonstrate your tuned PID system to your TA.