Type to search, in English or Thai

  1. Home
  2. English lessons
  3. PLC Automation
  4. Analog · PID · VFD

// Chapter 22.8 · PLC · Analog, PID & VFD

Analog, PID and Drive Control with a PLC: 4–20 mA Scaling, the PID Block, a Constant-Pressure Pump and Modbus RTU

The real world is not just 0 and 1: pressure, level, temperature and motor speed are continuous quantities. This page goes from the raw number in an analog module to a pressure-control loop that commands a drive — the most common job in buildings and factories.

By Ajarn ModUpdated
In 30 seconds
  • Scaling: y=ymin+(x−xmin)(ymax−ymin)/(xmax−xmin)y = y_{min} + (x - x_{min})(y_{max} - y_{min})/(x_{max} - x_{min}), computed in REAL
  • Below 3.6 mA or above 21 mA is a fault, not a measurement
  • PID: u=Kp(e+1Ti∫e dt+Tddedt)u = K_p\left(e + \frac{1}{T_i}\int e\,dt + T_d\frac{de}{dt}\right) · start tuning with PI
  • Commanding a VFD: DI + AI is simplest · Modbus returns data (current, faults) over a single pair

From raw counts to engineering units Basic

02.557.51006912138242073627648Raw ADC valuebar13824 → 5.0 bary = y_min + (x − x_min)· (y_max − y_min)/(x_max − x_min)4 mA00 bar12 mA138245 bar20 mA2764810 bar< 3.6 mAnegative/lowwire break!Other brands: Mitsubishi 0–4000 or 0–32000 · Omron 0–4000/0–8000 · check the module manual and match the transmitter range
Figure 1 · A 0–10 bar, 4–20 mA pressure transmitter into an analog module that returns raw counts of 0–27648

P1 Linear scaling

y=ymin+(x−xmin) ymax−yminxmax−xminy = y_{min} + (x - x_{min})\,\frac{y_{max} - y_{min}}{x_{max} - x_{min}}

P2 Resolution per raw count

Δy=ymax−yminxmax−xmine.g. 10 bar27648=0.36 mbar\Delta y = \frac{y_{max} - y_{min}}{x_{max} - x_{min}} \qquad \text{e.g. } \frac{10\ \text{bar}}{27648} = 0.36\ \text{mbar}

P3 Signal validity (NAMUR NE 43)

3.8≤I≤20.5 mA: valid measurementI≤3.6 or I≥21 mA → fault3.8 \le I \le 20.5\ \text{mA: valid measurement} \qquad I \le 3.6 \text{ or } I \ge 21\ \text{mA → fault}
Example P-1

Scaling a raw value of 13824 with INT gives zero

  1. Wrong: Bar := Raw / 27648 * 10; in INT → 13824/27648 = 0 (truncated) → 0 bar

  2. Also wrong: Raw * 10 / 27648 in INT → 27648 × 10 = 276480 overflows INT

  3. Right: Bar := INT_TO_REAL(Raw) * 10.0 / 27648.0; → 5.0 bar

  4. Check P3: Siemens modules return negative raw values when the current is below 4 mA (down to −4864 ≈ 1.185 mA), and with wire-break diagnostics enabled they report a special value (32767) — check both the raw range per P3 and the module's status bits.

The PID block in a PLC Intermediate

SPΣ+−PIDKp Ti TdVFD0–50 HzPump + pipepressurePT4–20 mAAO/ModbusPV (measured) via analog input + scalingPLC: scale → PID → limit → outputAlternatively use the VFD's built-in PID (PT into the VFD's AI) — the PLC only sends RUN and the setpoint
Figure 2 · Constant-pressure loop: SP − PV = e → PID → frequency → VFD → pump → pressure → transmitter

P4 PID (standard form)

u(t)=Kp(e+1Ti∫0te dτ+Td dedt)e=SP−PVu(t) = K_p\left(e + \frac{1}{T_i}\int_0^t e\,d\tau + T_d\,\frac{de}{dt}\right) \qquad e = SP - PV

P5 Discrete PID (sample time TsT_s)

uk=Kp(ek+TsTi∑j=0kej−TdTs(PVk−PVk−1))u_k = K_p\left(e_k + \frac{T_s}{T_i}\sum_{j=0}^{k} e_j - \frac{T_d}{T_s}(PV_k - PV_{k-1})\right)

The derivative term uses PV instead of e (derivative on measurement) so the output does not kick when SP changes, and anti-windup is needed: when the output hits its limit (e.g. 50 Hz), stop accumulating the integral. Most pump and fan loops use PI only (Td = 0) because pressure signals are noisy.

Initial tuning for a constant-pressure pump

  1. Start with a small Kp (e.g. 0.5 of full scale), a long Ti (e.g. 5 s) and Td = 0
  2. Increase Kp until it starts to oscillate, then halve it
  3. Reduce Ti gradually until the value returns to setpoint quickly enough without oscillating (typically 1–5 s for water pressure)
  4. Test by actually opening and closing taps, watching both the dip and the overshoot

Try the effect of Kp, Ti and Td in the PID lab (Thai), and a pressure loop with a VFD pump in the virtual instrumentation lab (Thai).

Commanding a drive from the PLC Intermediate

MethodWiringAdvantagesLimitations
Digital + preset speedsSeveral DO → DI wiresSimple, noise-immuneOnly a few speeds
Digital + analogDO → RUN · AO → AI (4–20 mA)Continuous adjustment, easy to understandFeedback limited to what is wired (AO, relays)
Modbus RTU (RS-485)One twisted pair, several drivesRead current, frequency and faults; set parametersCommunication settings needed; slower response with many stations
Industrial EthernetPROFINET, EtherNet/IP, Modbus TCP, EtherCATFast, lots of data, full diagnosticsNeeds a module and network configuration

P6 Frequency setpoint over Modbus

Reg=f0.01 Hz(35.5 Hz→3550)orReg=ffmax×Scale100%\text{Reg} = \frac{f}{0.01\ \text{Hz}} \quad (35.5\ \text{Hz} \to 3550) \qquad \text{or} \quad \text{Reg} = \frac{f}{f_{max}}\times \text{Scale}_{100\%}
Example register map (hypothetical, for learning — every brand differs; use the real manual)
RegisterNameRead/writeValue
40001Control wordWBit 0 = RUN · bit 1 = REV · bit 2 = RESET
40002Frequency referenceW0.01 Hz
40011Status wordRBit 0 = Ready · bit 1 = Running · bit 3 = Fault
40012Output frequencyR0.01 Hz
40013Output currentR0.1 A
40014Fault codeRCode per the manual

RS-485 wiring, CRC-16 calculation and a frame builder are in Modbus RTU · TCP (Thai). Remember: set the “communication timeout” in the drive so it stops if the PLC is silent for longer than the set time — otherwise, if the communication cable breaks, the drive keeps running at the last frequency.

Example P-2

The PLC sends 4–20 mA to a VFD ranged 20–50 Hz; 42 Hz is wanted

  1. Invert P1: I=4+16 (42−20)/(50−20)=4+11.73=15.73I = 4 + 16\,(42 - 20)/(50 - 20) = 4 + 11.73 = 15.73 mA

  2. If the analog output is 0–27648 = 4–20 mA: raw value =27648×11.73/16=20275= 27648 \times 11.73/16 = 20275

  3. Set the drive to treat AI below 2 mA as a broken wire and choose the response (stop / fallback speed)

Frequently asked questions

Is it better to run PID in the PLC or in the VFD?

For a single loop where the transmitter can be wired straight into the VFD, the drive's own PID is simple, responds quickly and keeps working even if the PLC fails. If several pumps must be coordinated, there are many conditions, or the loop must be logged and adjusted from SCADA, PID in the PLC is more flexible.

What value do I send over Modbus to command 35.5 Hz?

It depends on the register units in the drive manual. Many drives use 0.01 Hz units, so you send 3550. Some use a fraction of maximum frequency, e.g. 0–16384 or 0–10000 = 0–100 %. Always read the manual and test with a low value first.

Ajarn Mod · Teerawut Savangboon
Lecturer in Electrical Engineering, Dhonburi Rajabhat University, Samut Prakan · Profile