Siemens S7-1200 Constant Pressure Water Supply PID Closed-loop Control Program (TIA Portal Project) · Complete Reverse Analysis and Manual PID Explanation
Preface: Why This Reverse Analysis Is Worth Reading
There are many articles online discussing "constant pressure water supply," but most of them merely present "schematic diagrams + a screenshot,"failing to provide a project that you can directly copy and modify..
This project stands out—it is areal, complete source code, and a fully Chinese-variable S7-1200 constant pressure water supply PID closed-loop program. The hardware configuration, OB/FB/DB structure, and every intermediate variable of the PID controller (setpoint, feedback value, proportional, integral, current error, previous error, dead zone, upper and lower limits, etc.) are all clearly presented.This article takes you through the disassembly of each file: What PLC does it use? How is the PID calculated manually? How is the analog quantity scaled? How are I/O connected? How are the parameters tuned?
After reading, you will obtain a set ofdirectly reusable constant pressure water supply PID control ideas, as well as a "variable map." If you want to implement water supply for residential areas, buildings, factory constant pressure makeup water, or cooling circulating water, this program can be directly referenced.
First, let's clarify the true nature of the project (to avoid being misled by the title)
Downloading such projects is most prone to "not being what it seems". Based on file-by-file verification, let's first lay out the facts clearly:
| # | Folder / Promotional Claim | Actual Reading Situation | Impact |
|---|---|---|---|
| 1 | "S7-1200 achieves constant pressure water supply" | The internal structure of the project is aTIA Portal project (.ap14 + System/PEData.plf), and the CPU tested is aS7-1200 CPU 1214C DC/DC/DC (6ES7 214-1AG40-0XB0, firmware V4.1) | requiring the use ofTIA Portal (B&R)Open; cannot use STEP 7 V5.x |
| 2 | "Includes complete project files" | Includes complete hardware configuration, OB1, FB "PID_1", DB1/DB2, PID is handwritten in SCL, all variables are in Chinese | Compilable, downloadable, and runnable, belonging to a "complete project" |
| 3 | "PID closed-loop control" | PID is not using the ready-made PID_Compact instruction, buthandwritten incremental PID in FB "PID_1" using SCL(including proportional/integral/error/dead zone/upper and lower limits) | Logically transparent, modifiable, and learnable, more suitable for teaching and secondary development than black box instructions |
In a nutshell: This is a constant pressure water supply PID project using S7-1200 + TIA Portal, with PID handwritten and all comments/variables in ChineseThe constant pressure water supply PID project using S7-1200 + TIA Portal, with PID handwriting and annotations/variables all in Chinese. The title "S7-1200 Constant Pressure Water Supply Routine" alone is not precise enough, so this article adopts a more descriptive naming convention.
II. Hardware and System Architecture (Illustrated with a Diagram)
2.1 Controller and Signal Board
- CPU:
CPU 1214C DC/DC/DC(6ES7 214-1AG40-0XB0, firmware V4.1)—— 14-point digital input, 10-point digital output (transistor),2-channel on-board analog input (0–10 V, 10-bit). - Signal Board SB:
SB1232(6ES7 232-4HA30-0XB0)—— mounted above the CPUwith 1-channel analog output (12-bit)for issuing frequency commands to the frequency converter. - Project Name:
PID闭环运算恒压供水; Original version TIA PortalV14 (STEP 7 Basic 14.00.01.00), upgraded to V15 in October 2018.
2.2 Field instruments and actuators
| Equipment | Interface | Function |
|---|---|---|
| Pressure transmitter (4–20 mA / 0–10 V) | CPU on-board analog input%IW64 | Detects water supply network pressure → PID feedback value |
| Variable frequency drive (VFD) ×1 (extensible to 2) | Signal board analog output%QW80(and%QW200 referenced in the code)) | receives 0–10 V / 4–20 mA frequency commands to adjust the pump speed |
| pump motor | motor driven by a frequency converter | actuator, with the speed determining the water flow rate and pressure |
| (optional) HMI / touch screen | Ethernet (CPU comes with a PROFINET port) | to set target pressure, display real-time pressure, and switch between manual/automatic modes |
system architecture overview:
压力变送器(4-20mA)
│ %IW64
▼
┌──────────────────────┐
│ S7-1200 CPU 1214C │
│ OB1 主循环 │
│ └─调用 FB「PID_1」 │ ← 增量式 PID(SCL 手写)
│ 设定值 - 反馈值 │
│ → 比例+积分+死区 │
│ → 限幅[下限,上限] │
└──────────┬───────────┘
│ %QW80 (SB1232 AO)
▼
变频器(VFD) ──► 水泵电机
│
供水管网压力 ◄── 闭环反馈到压力变送器Engineering Key Points: This is a typical "single pressure sensor + single/dual frequency converter + PID speed regulation" constant pressure water supply structure. The PID output drives the frequency of the frequency converter, which adjusts the speed of the water pump. The speed changes the flow rate, which in turn changes the pressure of the pipe network, and the pressure is fed back to the PID - forming a closed loop.
III. Overall Program Structure: OB / FB / DB
3.1 Organization Block OB (Scan Scheduling)
| OB | Type | Function in this Project (Inferred from Block Structure) |
|---|---|---|
| OB1 | Main Loop (Main Program Sweep / Cycle) | Reads pressure analog values, performs scale transformation, feeds set values and feedback values to FB "PID_1", and writes the scaled PID output to the analog output; it is the "backbone" of the entire machine |
| (Startup-type OBs such as OB100 are not evident in this simplified project) | — | This routine is the smallest viable project for a focused PID closed-loop, with the main logic centered on the OB1 |
3.2 function block FB "PID_1" - the most valuable part of this project
. The interface variables of FB "PID_1" are fully extracted from the project database (all named in Chinese). This is the "parts list" for the handwritten PID controller:
| Variable (Chinese) | Meaning | Role in PID formula |
|---|---|---|
| Setpoint | Target pressure (e.g., 0.40 MPa) | PID setpoint r(k) |
| Feedback value | Actual pressure (value scaled from pressure transmitter) | PID process value / feedback y(k) |
| p value | Proportional gain Kp | Proportional term coefficient |
| Integral | Accumulated value of integral term | Eliminate steady-state deviation |
| Current error | e(k) = set value − feedback value | Current cycle deviation |
| Previous error | e(k−1) | Previous cycle deviation (required for incremental PID) |
| Calculate current output value | u(k) | Controller output for this cycle |
| Calculate previous output value | u(k−1) | Previous cycle output (for incremental accumulation) |
| Temporary storage of feedback process value / Temporary storage of output process value | Intermediate buffer | Intermediate quantity for scaling/filtering |
| Output control value / PID output | The final control value sent to the frequency converter | The limited execution value |
| PID dead zone | Dead zone bandwidth | Maintain output when the deviation is less than the dead zone to prevent jitter |
| PID upper limit / PID lower limit | Output limit | Limit the frequency command to the range of 0–100% / 0–10 V |
Conclusion: Variable naming directly exposes the algorithm structure - with 'current error/previous error' and 'calculate current output value/calculate previous output value', which is typicalIncremental (velocity-mode) PID; retaining the "integral" term, "dead zone", and "upper and lower limits" simultaneously, is the most reliable and anti-jitter writing method in industrial fields.
3.3 Data Block DB
- DB1: Thebackground data block (Instance DB)of FB "PID_1" stores all the static variables mentioned above.
- DB2: Data DB, which stores setpoints, proportional/integral parameters, upper and lower limits, dead zones, and other "recipes/parameters", facilitates HMI modification and tuning.
IV. Core Algorithm: Handwritten Incremental PID (disassembled variable by variable)
The following logic is derived from the variable naming and structure of FB "PID_1", and is not speculative. Below is the standard incremental PID writing method (SCL approach) corresponding to each of these variables, which you can directly use after copying into the software.
4.1 Incremental PID Formula
误差: e(k) = 设定值 - 反馈值 // 当前误差 上次误差: e(k-1) = 当前误差(上一周期) // 上次误差 比例项: P = p值 × ( e(k) - e(k-1) ) 积分项: 积分 = 积分 + Ki × e(k) // 积分 累加(消除静差) 增量输出: Δu(k) = P + 积分 // (如需微分可加 Kd 项) 本次输出: u(k) = u(k-1) + Δu(k) // 计算当前输出值 = 计算上次输出值 + 增量
- Dead Zone Handling:
IF ABS(e(k)) < PID死区 THEN Δu(k) = 0(If the deviation is too small, do not adjust to prevent frequent jittering of the pump near the set point). - Limiting:
u(k) := LIMIT(PID下限, u(k), PID上限)(Lock the frequency command within the range of 0–100% / 0–10 V to protect the equipment). - Rolling Storage: Store
e(k)in 'Last Error' andu(k)in 'Calculated Last Output Value' for use in the next cycle.
4.2 Why use Incremental instead of Position Type?
- Incremental only calculates 'Change': Even if the computer/PLC restarts occasionally,
u(k-1)Recovering from the background DB, the output will not jump to the full range.It is safer for the water pump.. - It is friendly to natural amplitude limiting.The output is clamped between the lower and upper limits, eliminating the overshoot caused by "integral saturation" in position-type systems.
- The advantage of handwriting: It is more transparent and easier to modify than the built-in
PID_Compactinstructionof Siemens.You can add feedforward, pump switching, and sleep logic at your own discretion.
By the way: The S7-1200 comes with a built-in
PID_Compactprocess object (whose structure is also available in this engineering type library), suitable for scenarios where "you don't want to write your own algorithm and want to use self-tuning"; while this project chooseshandwritten PIDThis is precisely for the readability of teaching and secondary development. Both routes are correct, and you can choose as needed.
V. I/O and Analog Mapping (Key Address Overview)
| Address | Type | Meaning | Scale Description |
|---|---|---|---|
| %IW64 | Analog Input | Pressure Transmitter Feedback | On-board AI0 (0–10 V / 4–20 mA), NORM_X → Engineering Quantity (e.g., 0.00–1.00 MPa) |
| %QW80 | Analog Output | Frequency command for frequency converter (main pump) | SB1232 AO (12-bit), SCALE_X maps 0–100% to 0–27648 / 0–10 V |
| %QW200 | Analog output | Second frequency command (standby pump/expansion) | Referenced in code, corresponding to 'one-to-two / dual pump' expansion wiring |
| %MD20 / MD24 / MD28 / MD32 / MD36 / MD40 | Global memory | PID intermediate variables (error, integral, output buffer, etc.) | Used for scale transformation and cross-cycle buffering |
Analog scale transformation (standard practice in Botou):
// 读入: pressure_raw := "压力变送器"(%IW64); // 归一化: norm := NORM_X(MIN:=0, MAX:=27648, VALUE:=pressure_raw); // 0..1 // 工程量: 反馈值 := SCALE_X(MIN:=0.0, MAX:=1.0, VALUE:=norm); // 0.00..1.00 MPa // 输出: out_norm := NORM_X(MIN:=0.0, MAX:=100.0, VALUE:=PID输出); // 0..100% // 写出: "变频器"(%QW80) := SCALE_X(MIN:=0, MAX:=27648, VALUE:=out_norm); // 0..27648
Key points: The analog input of S7-1200 is0–27648 (including 5530–27648 for 4–20 mA), and the output of 0–27648 corresponds to 0–10 V / 4–20 mA. All "engineering quantities" must be scaled using the pair of functions
NORM_X / SCALE_X, otherwise the pressure values and frequency percentages will not match.
VI. Constant Pressure Water Supply Control Principle (Closed-loop Block Diagram + Workflow)
6.1 Closed-loop Control Block Diagram
┌─────────┐ 设定值 ┌──────────────┐ PID输出 ┌────────┐ 频率 ┌──────┐ 流量
│ HMI │───────────►│ │────────────► │ 变频 │─────────►│ 水泵 │────┐
└─────────┘ │ FB「PID_1」 │ │ 器 │ └──────┘ │
│ (增量式PID) │ └────────┘ │
反馈值 ◄───────────────│ │◄────────────── 管网压力 ◄─────────────────┘
└─ 压力变送器(%IW64) ◄─┘ 误差=设-反 ┘6.2 A Complete Work Cycle (Events Occurring in OB1)
1. Sampling: Read%IW64 → NORM_X/SCALE_X→ Obtain the "Feedback Value" (actual pipe network pressure).compare: 当前误差 = 设定值 − 反馈值. 3Operation: FB "PID_1" performs increment, accumulates integral, adds dead zone, and limits amplitude → obtains "PID output / output control value".Execute: PID输出 → NORM_X/SCALE_X→ Write%QW80→ Adjust the pump speed using a frequency converter. closed loop: High water consumption → Pressure drop → Increased error → PID frequency increase → Flow rate increase → Pressure stabilizes; Low water consumption → Conversely, frequency decreases. scrollStore the current cycle error/output in "Last Error/Calculate Last Output Value" and proceed to the next cycle.
This is the essence of "constant pressure""It's not about keeping the water pump running at full speed all the time, but rather..."Keep the pressure of the pipeline network constant near the set valueIt automatically adjusts speed when water consumption changes. It not only ensures water supply but also saves 20%–50% of electricity compared to direct on-line starting at mains frequencyPower saving 20%–50%. It also allows for soft start and stop, extending the lifespan of the pump.
VII. Project Highlights: 7 pieces of reusable practical experience
1. PID handwriting, all variables in Chinese: Setpoint/feedback value/proportional/integral/error/dead zone/upper and lower limits are clear at a glance, with zero threshold for secondary development.Incremental PID + Limiting: Prevents integral saturation and restart transitions, ensuring maximum safety for the water pump. Dead zone designWhen the deviation is less than the dead zone, maintain the output to prevent the water pump from frequently starting and stopping/oscillating near the set point. Put parameters in DB2Kp, integral, upper and lower limits, and dead zone are all in the data block. The HMI can be directly modified, and the tuning does not require changing the program. Analog full-scale: NORM_X/SCALE_Xconverts channel values into engineering quantities, providing intuitive display of pressure/frequency.Dual output reserved: %QW80 + %QW200Two analog channels naturally support the "one-to-two / active/standby pump" expansion. "Structural minimalism and focus"OB1 + FB "PID_1" + DB1/DB2, with no redundant blocks, even a novice can understand it at a glance.
8. How to open, run, and tune PID
1. software: TIA Portal (B&R) V14 or higher(This project has been upgraded from V14 to V15, and can be opened using V15/V16/V17, with automatic migration). open"B&R → Open Project → Select"PID闭环运算恒压供水.ap14.Hardware confirmationCPU selection6ES7 214-1AG40-0XB0(Or of the same series), signal board hangingSB1232(6ES7 232-4HA30-0XB0)If your hardware model is different, simply change the order number and recompile. wiringPressure transmitter 4–20 mA connected to CPU on-board AI0 (address %IW64); SB1232 AO (%QW80) connected to frequency converter analog frequency input. DownloadFirst, configure the hardware, then download OB1 / FB "PID_1" / DB1 / DB2, and switch the PLC to RUN. PID tuning (on-site mnemonic):
- firstIntegral = 0, set the dead zone to be small, only applywith a proportional p-value of, increasing from small to large, until reaching "pressure quickly approaches the set value without severe oscillation";
- then addintegralto eliminate steady-state deviation (increase the integral when the pressure is always a little short of reaching the target);
- tune PID dead zoneto eliminate small jitter near the set point;
- finally setPID upper/lower limits(such as 0–100%) to prevent frequency exceeding limits.
7. Extend one-to-two: Connect the standby pump to%QW200, and switch/activate the second pump when the main pump's pressure is insufficient at 50 Hz (the switching logic can be implemented by adding a simple comparison in OB1).
IX. Two reminders for engineers
- Don't be underestimated by the word "routine": The PID of this routine ishandwritten incrementally, which is more suitable for learning and modification than many projects that "just drag a PID_Compact and call it done". If you want to implement feedforward, pump switching, or sleep, it's all up to you in your own code.
- Analog values must be scaled: The most common pitfall for beginners is "reading out 0–27648 directly as pressure" and "the output doesn't multiply by a factor, so the frequency converter doesn't work". Be sure to use
NORM_X/SCALE_Xto properly scale the engineering quantity. - Be aware of 4–20 mA disconnection: If the pressure transmitter is disconnected, the AI will drop to < 5530 or even 0, and the PID will misjudge "pressure as 0" and output at full frequency. This should be added in practical engineering projectsDisconnection detection + fault frequency limiting/pump stoppingProtection (not included in this simplified routine, must be added before going online).
X. Frequently Asked Questions (FAQ)
Q1: What software should be used to open the S7-1200 constant pressure water supply program?A: UseTIA Portal (B&R)to open the.ap14project (V14 and later versions are supported and will be automatically migrated). This is a TIA project, and STEP 7 V5.x cannot be used.
Q2: How is the PID control for constant pressure water supply implemented?A: The pressure transmitter measures the pressure of the pipe network (feedback value) and compares it with the set value to obtain an error. The FB "PID_1" uses incremental PID to calculate the control quantity, which is output to the frequency converter via analog output to adjust the pump speed, ensuring the pressure remains constant near the set value. Changes in water consumption → automatic speed adjustment.
Q3: How do I connect the S7-1200 to a pressure transmitter (4–20 mA)?A: Connect the transmitter to the CPU board-mounted analog input (in this project, %IW64), and use it in the programNORM_X/SCALE_XMap 5530–27648 to engineering quantity pressure (e.g., 0.00–1.00 MPa).
Q4: Which should I choose, PID_Compact or manual PID?A:PID_Compactis a Siemens built-in process object with self-tuning and convenience;manual PID (such as the FB "PID_1" in this project) is more transparent, customizable, and easier to teach. For learning/secondary development, choose manual PID, and for catching up with the schedule, use PID_Compact.
Q5: What should I do if the pressure keeps unstable/oscillating?A: First, adjust the proportional p value (too large is prone to oscillation, too small results in slow response), then add integral to eliminate static error, and finally usePID dead zoneto eliminate oscillation near the setpoint; the output must be limited.
Q6: Can I modify it to achieve "one-to-two" dual-pump constant pressure water supply?A: Yes. This project has reserved a second analog output channel%QW200When the main pump still lacks pressure at 50 Hz, the standby pump can be activated. The switching logic is implemented through comparison in OB1.
Conclusion + Resources
This S7-1200 constant pressure water supply PID project is a rare, transparent sample with all variables in Chinese and handwritten PIDA rare, transparent sample with all variables in Chinese and PID handwritten: it uses incremental PID for closed-loop control, employs dead zone and amplitude limiting for safety, utilizes data blocks to store parameters for convenient tuning, and reserves expansion with dual analog outputs. It is of great reference value for engineers working on constant pressure water supply, cooling circulating water, and constant pressure water replenishment in residential areas, buildings, and factories.
*All conclusions in this article are derived from the file-by-file analysis of engineering documents (project metadata.ap14+ internal databaseSystem/PEData.plf). Hardware models, block structures, FB "PID_1" interface variables, and I/O addresses are directly extracted without any external speculation.
Leave a Reply