Intermediate1 hourPID controlDead-bandOccupied/unoccupied schedulingAnalog scaling
HVAC Zone Temperature Control
Building HVAC control with PID modulation of a hot-water valve, dead-band hysteresis to prevent valve hunting, and scheduled occupied/unoccupied setpoints.
I/O list
| Tag | Type | Description |
|---|---|---|
| RoomTemp_RawDI | AI | RTD raw 4-20 mA from temperature sensor |
| RoomTemp_DegC | REAL | Scaled room temperature in °C |
| HeaterValve_Pct | AO | Hot water valve 0-100% open |
| OccupiedMode | DI | Schedule input from BMS |
| OccupiedSP | REAL | Setpoint when occupied (e.g., 21°C) |
| UnoccupiedSP | REAL | Setpoint when unoccupied (e.g., 17°C) |
Structured Text code
PROGRAM main
VAR
Setpoint : REAL;
DeadBand : REAL := 0.5; (* ±0.5°C around setpoint *)
HeatNeeded : BOOL;
PIDInst : PID; (* IEC 61131-3 PID FB *)
Kp : REAL := 5.0;
Ki : REAL := 0.1;
Kd : REAL := 0.0;
END_VAR
(* Scale 4-20 mA raw to 0-50°C *)
RoomTemp_DegC := (RoomTemp_RawDI - 4.0) / 16.0 * 50.0;
(* Choose setpoint based on schedule *)
IF OccupiedMode THEN
Setpoint := OccupiedSP;
ELSE
Setpoint := UnoccupiedSP;
END_IF;
(* Dead-band hysteresis: only call for heat when below SP - DB *)
IF RoomTemp_DegC < Setpoint - DeadBand THEN
HeatNeeded := TRUE;
ELSIF RoomTemp_DegC > Setpoint + DeadBand THEN
HeatNeeded := FALSE;
END_IF;
(* PID modulates valve when heat is needed *)
IF HeatNeeded THEN
PIDInst(SP := Setpoint, PV := RoomTemp_DegC, KP := Kp, KI := Ki, KD := Kd);
HeaterValve_Pct := PIDInst.OUT; (* Limit 0-100 *)
IF HeaterValve_Pct < 0.0 THEN HeaterValve_Pct := 0.0; END_IF;
IF HeaterValve_Pct > 100.0 THEN HeaterValve_Pct := 100.0; END_IF;
ELSE
HeaterValve_Pct := 0.0;
END_IF;How it works
Analog scaling: 4-20 mA raw signal is scaled to 0-50°C engineering units. The scaling formula is universal for 4-20 mA inputs.
Schedule logic: occupied/unoccupied mode (typically driven by BMS time-of-day schedule) selects different setpoints to save energy when the space is unoccupied.
Dead-band hysteresis: the heater turns on when temp drops 0.5°C below SP, off when it rises 0.5°C above. Without dead-band, the valve hunts continuously.
PID modulation: when heat is needed, PID modulates the valve from 0-100% based on the temperature error. Output is clamped to valid range.
More examples
Traffic Light PLC Program
Single-intersection traffic light controller with N-S and E-W phases plus all-red interval
Water Tank Level Control
Two-pump water tank with high/low level sensors, alternating pumps for even wear, high-high alarm trip
Conveyor with Photoeye Sort
Conveyor running at constant speed sorting parts by photoeye into two reject bins
Motor Reversing with Interlock
Bidirectional motor with start/stop buttons for forward and reverse, with software interlock preventing both contactors energising simultaneously