Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 36 ยท Projects and Practice

Project: A PID Temperature Controller

Design a PID temperature controller in LabVIEW: a timed loop, setpoint, output limiting, anti-windup and tuning. Includes a runnable Python simulation of a heater and PID.

Control is where LabVIEW's DAQ and loop features meet. Here you will build a thermostat: measure a temperature, compare it with a setpoint, and drive a heater so the temperature settles at the setpoint without large overshoot. The diagram parts are described precisely; the heater and the controller are modelled in Python so you can tune the numbers here. LabVIEW and real hardware are not available on this site.

The control loop

flowchart LR
    SP["Setpoint"] --> SUM(("error = SP - PV"))
    SUM --> PID["PID<br/>Kp, Ki, Kd"]
    PID --> OUT["Output limit 0-100%"]
    OUT --> HEAT["Heater (PWM or analog)"]
    HEAT --> PLANT["Thermal mass"]
    PLANT --> SENS["Sensor + DAQ read"]
    SENS -- "PV" --> SUM

The process variable (PV) feeds back every cycle.

Hardware path on the diagram: DAQmx Read (1 sample, hardware timed) of the sensor; the PID VI; DAQmx Write of an analog output (or a PWM pulse train from a counter) to a heater driver (a MOSFET or solid-state relay). Add a hardware over-temperature cutout that does not depend on software.

When real heaters are involved, safety comes first: use independent thermal fuses or thermostats, set a software maximum output and maximum temperature, and drive the output to zero in the error path and when the VI stops. Do not rely on software alone.

PID in LabVIEW

The PID VIs come from the PID and Fuzzy Logic Toolkit (the PID Advanced.vi, PID.vi and autotuning VIs; available with the Control Design and Simulation or the toolkit). Inputs: setpoint, process variable, PID gains (Kc, Ti, Td), output range, and dt. Note the parallel versus ideal difference: LabVIEW's gains are Kc (proportional gain), Ti (integral time, minutes) and Td (derivative time, minutes), not Kp, Ki and Kd, though they are related (Ki = Kc / Ti, Kd = Kc * Td).

The toolkit PID has built-in anti-windup and output limiting. If you do not have it, you write the algorithm yourself with a shift register for the integral and previous error; it is only a few nodes.

The algorithm

Each sample the controller computes:

  • P (proportional): Kp * error: reacts to the present error.
  • I (integral): sum of error over time times Ki: removes steady-state offset.
  • D (derivative): Kd * d(error)/dt: damps fast changes (use the PV derivative to avoid a "kick" when the setpoint changes, and filter noise).
  • Output limiting: clamp to 0-100 %.
  • Anti-windup: stop integrating while the output is saturated, otherwise the integral term grows during a long warm-up and causes a large overshoot.

Simulation

The plant is a simple first-order thermal model: temperature rises with heater power and falls with losses to a 20 C ambient.

Python PID controller with a simulated heater

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare the lines. P only stops short of the setpoint (a steady-state error, so it never settles within 1 degree). Adding I removes the offset. In this simple model the integral guard changes the result only slightly, because the heater saturates briefly. With kd=8 the derivative is too large for this plant and the temperature keeps wobbling by about a degree, so the controller does not settle until the end of the run: derivative gain needs its own tuning. Try kd between 0.5 and 3, change the plant constants, and watch the numbers. They come from this toy model, not from a real heater.

Tuning

  1. Start with I and D at zero; raise Kp until the response is quick without large oscillation.
  2. Add I slowly until the offset disappears in a reasonable time.
  3. Add a little D if overshoot remains.
  4. Test with a setpoint change and a disturbance (open a door).

Ziegler-Nichols gives starting values: raise Kp until a sustained oscillation of period Tu appears at gain Ku, then use Kp = 0.6 Ku, Ti = Tu / 2, Td = Tu / 8. These are aggressive; scale them back.

LabVIEW offers PID Autotuning VIs that find gains from a step test, and the Control Design and Simulation Module to model the plant and simulate before touching hardware.

Python Ziegler-Nichols starting values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Timing the loop

The PID needs a constant dt. Use a Timed Loop (or a While Loop with Wait Until Next ms Multiple and measure dt) and wire the actual dt into the PID. Windows is not real-time: jitter of several milliseconds is normal. For a slow thermal process (1 second) that is fine; fast control needs an NI Real-Time target or FPGA.

Interface

  • A setpoint control and a PV/Setpoint/Output chart.
  • Auto/Manual switch: in manual, the operator sets the output; switching back should be bumpless (initialise the integral so the output stays the same).
  • Gains controls, behind a password if needed, saved to a configuration file.
  • Alarms and an enable/disable button with a safe default of off.

Build it as a state machine with Idle, Run, Manual and Fault states and keep the control calculation in a small tested subVI (unit tests).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the integral term remove?

    Integrating the error keeps increasing the output until the offset is gone.
  2. 2. What is integral windup?

    Anti-windup stops integrating during saturation.
  3. 3. Why take the derivative of the process variable rather than the error?

    A setpoint step would create a huge derivative of the error for one sample.
  4. 4. Which safety measure is essential on a real heater?

    Software can fail; an independent cutout and a zero-output error path protect people and equipment.

Summary

  • A PID loop reads the PV, computes P, I and D, limits the output and writes it with a constant dt.
  • Use anti-windup, derivative on PV and bumpless manual/auto transfer.
  • Tune step by step (P, then I, then D) or start from Ziegler-Nichols and back off.
  • Add independent hardware safety and a safe state on error.

Next: Project: A SCPI Instrument Simulator