S7-300 PLC Setpoint Selection with Hysteresis for 6 Fan Outputs

David Krause17 min read
S7-300SiemensTutorial / How-to
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

Problem Definition: Dual-Source Setpoint Selection for Six Cooling Fans

A SIMATIC S7-300 PLC drives six circulating-air cooling fans in a six-chamber tunnel oven. Production passes on a conveyor belt whose instantaneous speed is measured in metres per minute. The control specification has two regimes and one retention requirement:

  1. Low-speed protection mode (conveyor ≤ 10 m/min): Each of the six fans must run at a fixed 30 % reference. Operator HMI entries are locked out because, at low belt speed, the active product inside the oven can be thermally damaged if the cooling airflow drops below the protection value.
  2. Normal operation mode (conveyor > 10 m/min): Each fan follows its own operator-entered HMI setpoint (0–100 %), used for fine-tuning individual chamber airflow for product-quality reasons.
  3. Retention requirement: When the conveyor stops or drops below the threshold, the latest operator-entered values must be retained so that, when the conveyor accelerates through 10 m/min again, those exact values are restored to the live setpoints. Operators expect the line to "come back exactly where they left it".
Engineering note: A naïve comparator IF speed > 10 THEN operator_setpoint ELSE 30% will chatter when the conveyor speed hovers around 10 m/min due to encoder quantisation, VFD ramp transients, or load variations. A hysteresis band, a debounce timer, and a snapshot mechanism are mandatory for a robust implementation. The 5-line STL snippet originally proposed in the engineering discussion handles only the multiplexer; it is a useful starting point but is not production code.

Prerequisites

Hardware

  • SIMATIC S7-300 CPU with retentive memory. Candidates are CPU 314C-2 PN/DP (integrated AI/AO, cost-effective), CPU 315-2 PN/DP (larger work memory, PROFINET), or CPU 317-2 PN/DP for high-end applications. Refer to the SIMATIC S7-300 Automation System, Hardware and Installation manual for order numbers, slot rules, and wiring practices.
  • SM 331 analogue input for the conveyor speed transmitter (4–20 mA, two-wire). Typical order number: 6ES7331-7KF02-0AB0 (8 AI, 12-bit, suitable for slow process signals).
  • SM 332 analogue outputs for the six fan drives. Two SM 332 AO 4 modules (6ES7332-5HD01-0AB0) cover six channels; the integrated AO of a CPU 314C-2 PN/DP provides the seventh channel.
  • Operator panel: SIMATIC HMI such as KTP1200 Basic, TP700 Comfort, or MP 377. Configure with WinCC flexible 2008 SP5 (classic S7-300) or TIA Portal V18 with the S7-300 add-on package.
  • Speed sensor: incremental encoder + FM 350-1 counter, or a 4–20 mA tachometer scaled 0–50 m/min. Use shielded, twisted-pair cable with shield grounded at the cabinet entry only.
  • Six VFDs (e.g., SINAMICS V20 or G120) configured for analogue setpoint with 4–20 mA input and "fallback to last value" behaviour on signal loss.

Software

  • STEP 7 V5.6 + SP2 (classic project) or TIA Portal V18 with the S7-300 HSP.
  • Programming languages: STL (Statement List) shown for the kernel; LAD (Ladder) shown for the multiplexer; SCL is available on CPU 314C-2 and higher.
  • WinCC flexible 2008 SP5 or TIA Portal WinCC for HMI screens.

System Architecture and I/O Assignment

The signal list below is a starting point; the engineer must reconcile it with the actual SM 332 channel assignments on the rail.

Signal Type Address / Tag Range Source / Destination
Conveyor speed raw AI 4–20 mA IW 0 0–27648 SM 331 channel 0
Conveyor speed (engineering units) REAL MD 100 0.0–50.0 m/min FC1 scaling
Operator setpoint fan 1 REAL DB2.DBD0 0.0–100.0 % HMI tag, retentive
Operator setpoint fan 2 REAL DB2.DBD4 0.0–100.0 % HMI tag, retentive
Operator setpoint fan 3 REAL DB2.DBD8 0.0–100.0 % HMI tag, retentive
Operator setpoint fan 4 REAL DB2.DBD12 0.0–100.0 % HMI tag, retentive
Operator setpoint fan 5 REAL DB2.DBD16 0.0–100.0 % HMI tag, retentive
Operator setpoint fan 6 REAL DB2.DBD20 0.0–100.0 % HMI tag, retentive
Snapshot (last good) fan 1..6 REAL DB3.DBD0..DBD20 0.0–100.0 % FC30 snapshot, retentive
Active setpoint fan 1..6 REAL MD 110..MD 134 0.0–100.0 % FC10 multiplexer
Analog output raw fan 1..6 REAL→INT QW 0,2,4,6,8,10 0–27648 SM 332 PQW 0..10
Mode flag (operator mode) BOOL M 10.0 1 = operator setpoints FC20 hysteresis
Conveyor-stop latch BOOL M 10.1 1 = belt stopped for ≥ tStopDbnc FC20 debounce
FP one-shot: stop rising BOOL M 11.0 edge memory FC30
FP one-shot: mode rising BOOL M 11.1 edge memory FC30
Mark DB2 and DB3 as retentive: open DB properties → "Non-optimised block" + tick "Retain". S7-300 supports up to 2048 bytes of retentive bit memory and full data-block retention when configured in the CPU hardware properties, per the S7-300 hardware manual.

State-Machine View

Model the system as two states and one snapshot memory. Drawing this on paper before coding prevents most of the back-and-forth in the original engineering discussion.

PROTECTION rSpd ≤ 9.5 m/min fans = 30 %, HMI locked OPERATOR rSpd ≥ 10.5 m/min fans = HMI setpoints rSpd rises above 10.5 restore snapshot → DB2 rSpd falls below 9.5 snapshot DB2 → DB3 DB3 retentive snapshot last six HMI setpoints

Conveyor Speed Scaling (FC1)

The 4–20 mA analogue input is linear 0–50 m/min. Convert raw to engineering units with the standard Siemens norm scaling. The function below is one-shot in OB1 and easy to retune by changing the two constants.

// FC1 - Scale AI 0..50 m/min
//   Input : IW0  (0..27648 Siemens norm)
//   Output: MD100 REAL m/min
L   IW0
ITD
DTR
L   5.000000e+001
*R
L   2.764800e+004
/R
T   MD100

The 10 m/min threshold therefore corresponds to raw 10 × 27648 / 50 = 5529.6, rounded to 5530. Using REAL scaling throughout avoids integer rounding and gives clean hysteresis boundaries.

Hysteresis Parameters

Parameter Symbol Default value Comment
Speed threshold (high, ON) rSpdOn 10.5 m/min Switch to operator setpoints when rising
Speed threshold (low, OFF) rSpdOff 9.5 m/min Switch back to fixed 30 % when falling
Hysteresis band Δ 1.0 m/min rSpdOn − rSpdOff
Stop debounce tStopDbnc 2000 ms Time below rSpdOff before "stopped" latches
Fixed protection speed rFixed 30.0 % Protection setpoint when stopped or low speed
Operator input max rOpMax 100.0 % Clamp on HMI input
Operator input min rOpMin 5.0 % Avoid 0 % which trips VFDs
A 1 m/min hysteresis on a 0–50 m/min full scale is 2 % of range; comfortable for noisy encoders. If the encoder resolution is poor (e.g., 1 pulse/m only), increase Δ to 2 m/min or use a low-pass filter on MD100 (PT1 with T = 500 ms).

STL Implementation (FC20 - Hysteresis and Debounce)

The compact STL kernel suggested in the original exchange handled the multiplexer only. Production code must add hysteresis, debounce, and snapshot. The complete FC20 follows.

// FC20 - Mode flag with hysteresis and stop debounce
//   Input : MD100   rConvSpeed  (REAL m/min)
//   Output: M10.0   bOperatorMode
//           M10.1   bConvStopped (latched after tStopDbnc)
A   M 10.0          // currently in operator mode?
JC  MODE_OP         // yes - test only high threshold
// currently in protection mode (30 %)
L   MD 100
L   1.050000e+001   // rSpdOn = 10.5
>R
=   M 10.0          // raise operator mode
JU   END

MODE_OP:
L   MD 100
L   9.500000e+000   // rSpdOff = 9.5
<R
=   M 10.0          // drop operator mode if below low threshold
END: NOP 0

// Conveyor-stop debounce 2 s
A   M 10.0          // speed is below threshold
O   M 10.1          // or already latched
AN   T 1
L   S5T#2S
SD   T 1
A   T 1
=   M 10.1          // bConvStopped - used by FC30 retention

The 5-line STL from the original engineering exchange, reproduced verbatim for traceability:

L   iConvSpeed
L   10
>I
L   iOperatorManipulatedSetpoint
JC  _001
L   iFixedSetpoint
_001: T  iSetpointFanSpeed

This compact snippet is the multiplexer only; in a real S7-300 oven control, the snapshot (FC30) and hysteresis (FC20 above) are mandatory to satisfy the operator's requirement that the latest values be stored when the belt stops and restored when it moves again.

STL Implementation (FC30 - Snapshot / Restore)

FC30 keeps two retentive data blocks: DB2 holds the live HMI values, DB3 holds the snapshot at the moment of the most recent conveyor stop. On a transition from PROTECTION to OPERATOR, the snapshot is copied back to DB2 so that operator setpoints resume exactly where they were.

// FC30 - Capture operator setpoints on belt stop, restore on belt start
// Trigger 1: M10.1 rising edge  (stop latched)  -> DB2 -> DB3
// Trigger 2: M10.0 rising edge  (operator mode) -> DB3 -> DB2
A   M 10.1
FP  M 11.0         // one-shot on stop rising
JCN NO_CAP
L   DB2.DBD0   T DB3.DBD0     // fan 1
L   DB2.DBD4   T DB3.DBD4     // fan 2
L   DB2.DBD8   T DB3.DBD8     // fan 3
L   DB2.DBD12  T DB3.DBD12    // fan 4
L   DB2.DBD16  T DB3.DBD16    // fan 5
L   DB2.DBD20  T DB3.DBD20    // fan 6
NO_CAP: NOP 0

A   M 10.0
FP  M 11.1         // one-shot on operator-mode rising
JCN NO_RES
L   DB3.DBD0   T DB2.DBD0
L   DB3.DBD4   T DB2.DBD4
L   DB3.DBD8   T DB2.DBD8
L   DB3.DBD12  T DB2.DBD12
L   DB3.DBD16  T DB2.DBD16
L   DB3.DBD20  T DB2.DBD20
NO_RES: NOP 0
REAL values are 4-byte aligned. Use DBD0, DBD4, DBD8, DBD12, DBD16, DBD20, not DBW0, DBW2, .... Misalignment stores/reads garbage REALs, which is the most common fault with manual DB layouts in S7-300.

STL Implementation (FC10 - Six-Channel Multiplexer)

// FC10 - Active setpoint generator (six fans)
//   M10.0 = 1 -> use DB2 (operator) setpoints
//   M10.0 = 0 -> use fixed 30 % protection value
//   Outputs: MD110, MD114, MD118, MD122, MD126, MD130
A   M 10.0
JCN PROT
L   DB2.DBD0    T MD110
L   DB2.DBD4    T MD114
L   DB2.DBD8    T MD118
L   DB2.DBD12   T MD122
L   DB2.DBD16   T MD126
L   DB2.DBD20   T MD130
JU   END

PROT:
L   3.000000e+001
T   MD110
T   MD114
T   MD118
T   MD122
T   MD126
T   MD130
END: NOP 0

OB1 then scales the REAL percent (0–100) to the analogue-output raw value (0–27648) and writes QW0, QW2, QW4, QW6, QW8, QW10. Standard scaling: raw = percent × 276.48. Clip the result to [0, 27648] before assigning to the PQW to prevent signed-overflow warnings on the SM 332.

Ladder (LAD) Alternative

For teams that prefer LAD, the same logic fits in five networks of OB1. A compact version of the multiplexer:

Network 5 - Fan 1 setpoint
|----[ M10.0 ]-----------------( MOV   DB2.DBD0 -> MD110 )----|
|----[ /M10.0 ]----------------( MOV   30.0    -> MD110 )----|

Repeat the rung five more times, replacing DB2.DBD0 with DB2.DBD4 / 8 / 12 / 16 / 20 and MD110 with MD114 / 118 / 122 / 126 / 130. Use a separate network with a comparator (GE 10.5, LE 9.5) and SR flip-flop to implement the hysteresis in pure LAD if STL is not allowed by the site standard.

SCL Variant (CPU 314C-2 and higher)

// FC20 in SCL
IF bOperatorMode THEN
    IF rConvSpeed < rSpdOff THEN bOperatorMode := FALSE; END_IF;
ELSE
    IF rConvSpeed > rSpdOn THEN bOperatorMode := TRUE;  END_IF;
END_IF;
bConvStopped := bConvStopped AND (rConvSpeed < rSpdOff);
IF rConvSpeed < rSpdOff THEN
    tStop(IN:=TRUE, PT:=T#2S);
ELSE
    tStop(IN:=FALSE);
END_IF;
bConvStopped := tStop.Q OR bConvStopped;

SCL reads more like the engineering specification and is preferable on newer S7-300 CPUs and on S7-1200/1500 migrations.

HMI Integration

  • Six I/O fields, each tagged to DB2.DBD0..DBD20. Set limits 0.00–100.00 % with one decimal place, behaviour "Output"/"Input/Output".
  • Disable input on each field when M10.0 = 0 (operator mode off). Use the HMI "Enable" property bound to M10.0. This satisfies the requirement that "operator does not change these values because product inside oven can be damaged".
  • Status indicator: "Conveyor speed = X.X m/min" bound to MD100; a green lamp "Operator mode active" bound to M10.0; an amber lamp "Belt stopped" bound to M10.1.
  • Recipe view: each recipe row contains the six fan setpoints. Persist recipes on the panel's flash; copy to DB2 on "Load recipe". Keep DB3 as the snapshot, untouched by recipe load.
  • Alarm view: alarm "Conveyor speed out of range" when MD100 > 55 or MD100 < 0 (transmitter fault).

Commissioning and Verification

  1. Wire check: with the CPU in STOP, force IW0 = 0; verify the transmitter wiring per the SM 331 wiring diagram in the S7-300 hardware manual. Confirm 24 V is present at the two-wire loop terminals and that the shield is grounded at the cabinet entry.
  2. Scale check: drive the transmitter with a calibrator at 4 mA and 20 mA; confirm MD100 reads 0.0 and 50.0 ± 0.1 m/min in the VAT table. If the reading is inverted, check whether the SM 331 channel is configured for 4–20 mA or 0–20 mA in HW Config.
  3. Threshold step test: with MD100 = 10.5, the mode flag M10.0 must set; reduce to 9.5, the mode flag must reset. Verify in VAT with online monitor. Walk the speed slowly through the band (0.1 m/min steps) and confirm no chatter.
  4. Snapshot test: in RUN, change DB2.DBD0 from 50 % to 75 %; pull MD100 to 0 for more than 2 s; observe DB3.DBD0 = 75 %; raise MD100 back to 12; observe DB2.DBD0 = 75 % after restore.
  5. Fan sanity: with the VFDs in manual, command the PLC outputs from VAT; confirm each PQW follows the setpoint with the expected 0–10 V or 4–20 mA scaling.
  6. HMI lockout test: drop speed below 9.5; confirm operator I/O fields go grey and cannot be edited. Click into a field and try to type — WinCC should reject the input.
  7. Power-cycle retention test: enter six setpoints, run CPU STOP/RUN several times; confirm values remain unchanged thanks to the retentive DBs.
  8. End-to-end test: run the conveyor at 5 m/min (all fans 30 %), accelerate to 12 m/min (all fans switch to operator values), stop the conveyor for 10 s (fans revert to 30 %, snapshot stored), restart (operator values restored).

Troubleshooting Matrix

Symptom Likely cause Diagnostic step Fix
Fan chatters between 30 % and operator value No hysteresis, raw comparison only Monitor MD100 with trend; check oscillation < 1 Hz Implement FC20 hysteresis (10.5 / 9.5)
Operator values lost after belt stop DB2 not retentive STEP 7 → DB2 properties → "Non-optimised block, retentive" Re-mark DB2 as retentive
After STOP/RUN setpoints reset to 30 % DB3 not retentive either Check DB3 properties; power-cycle test Mark DB3 retentive (snapshot must persist across power dips)
Fans never reach 100 % even with operator input 100 Analog output scaling wrong (0–20 mA vs 0–10 V) Measure PQW with multimeter; verify AO card type Reconfigure SM 332 output range; check QW value matches expected
Conveyor speed reads 0 m/min when belt is running Open wire on 4–20 mA loop, channel group error Check SF LED on SM 331; read diagnostic byte in OB82 Repair wiring; verify channel group ground and 24 V supply
All six fans always 30 % even at 12 m/min M10.0 stuck because of broken FP edge in FC30 Cross-check FC20 in VAT with online monitor Verify hysteresis block is executed in OB1 cycle, not in OB100
Restore brings wrong fan values DBD offset mismatch (4-byte REAL alignment) Compare DB2/DB3 offsets in VAT table Use DBD offsets 0,4,8,12,16,20 (4-byte aligned)
CPU goes STOP with SF after power dip Retentive area not licensed / too large Diagnostic buffer in STEP 7 → PLC → Module Information Reduce retentive range in CPU properties (Hardware)
VFD reports "analog input loss" and ramps to 0 Signal loss on SM 332 wiring Check PQW with scope; check VFD parameter P0752 Set VFD to "last value" fallback; repair shield ground
Fans overshoot protection value when belt decelerates VFD ramp time too long relative to belt ramp Check VFD ramp time parameter (e.g., P1120) Tune VFD ramp to 2 s; or add ramp on MD110 in PLC

Field-Proven Caveats

  • Slipping encoder roller: if the encoder is mounted on a slipping pulley, the apparent speed may bounce between 9 and 11 m/min even though the belt is steady. Increase Δ to 2 m/min for such installations, or add a PT1 low-pass on MD100 with T = 500 ms.
  • Soft-starter ramp: during VFD ramp-up the speed may dwell at exactly 10 m/min. The 2-second debounce absorbs this and keeps the fans in protection mode until the belt has clearly stabilised.
  • Operator misuse: tying the HMI input enable directly to M10.0 prevents the operator from changing setpoints during low-speed protection, matching the requirement that "operator does not change these values because product inside oven can be damaged".
  • Setpoint sanity: clamp HMI input to [5 %, 100 %] to avoid sending 0 % to a VFD that interprets it as "stop", which would trip the drive with F0001 (overcurrent on ramp-down) on many inverters.
  • Three-segment thinking trap: the discussion in the original engineering thread went back and forth because the requirement is not a simple comparator; it is a state machine with two modes and a snapshot memory. Drawing the three boxes (current mode, snapshot, multiplexer) before coding saves hours.
  • CPU OB1 priority: do not place FC20/FC30/FC10 in OB100 (startup) because the conveyor speed is not yet valid; OB1 cyclic execution is correct.
  • Diagnostic interrupts: enable OB82 for analogue-channel diagnostics. Without it, a broken 4–20 mA loop will not raise an SF, and the conveyor speed will sit at 0 indefinitely, locking the system into protection mode.

CPU Selection Comparison

CPU Order number (typical) Work memory Bit memory Retentive (M, T, C) Onboard I/O Verdict for this application
CPU 312 6ES7312-1AE14-0AB0 32 KB 128 B All MB0..MB15 None Marginal — no onboard AI/AO, expansion SM 332/331 required
CPU 314 6ES7314-1AG14-0AB0 128 KB 256 B All MB0..MB127 None Good if external SM 331/SM 332 are already in the rack
CPU 314C-2 PN/DP 6ES7314-6EH04-0AB0 192 KB 256 B All MB0..MB127 4 AI + 2 AO + 24 DI + 16 DO Best fit for 1 AI + 6 AO with one onboard AO + one SM 332 AO4
CPU 315-2 PN/DP 6ES7315-2EH14-0AB0 384 KB 2048 B MB0..MB2047 None Good for plants with future expansion, more recipes, or PROFINET I/O
CPU 317-2 PN/DP 6ES7317-2EK14-0AB0 1024 KB 4096 B MB0..MB4095 None Over-spec for this app; reserved for multi-line control

Variants and Platform Notes

  • S7-1200 / S7-1500 (TIA Portal): The same logic translates directly to SCL with a structure IF rSpd > rSpdOn THEN bOp := TRUE; ELSIF rSpd < rSpdOff THEN bOp := FALSE; END_IF;. Replace M10.0 with a retentive global DB tag "Mode".bOperatorMode. Edge detection uses R_TRIG / F_TRIG IEC instances.
  • LOGO! 8: Implementable with the analogue threshold trigger and a soft-retentive shift register, but six independent channels plus retention is at the upper limit of LOGO! memory.
  • SINAMICS V/F fans: drive setpoint source should be "Analogue input" with the AI wired from the SM 332 output. Avoid command source "Fieldbus" because fieldbus fault defaults to 0 % on most inverters, killing the protection setpoint.
  • PROFINET variant: if the VFDs are G120 with PROFINET, replace the SM 332 AO path with PROFINET telegram 1 (setpoint) and keep the protection value 30 % in the drive's local fallback parameter (P0822 in some variants). The PLC hysteresis logic remains unchanged.

Glossary

Term Definition
Hysteresis Difference between switch-on and switch-off thresholds, used to suppress chatter
Snapshot Frozen copy of dynamic data taken at a defined trigger event
Debounce Time delay before a flag is accepted as stable
FP (Flanken-Positive) Positive-edge detection in STL
DBD Data-block double-word (32-bit), used for REAL alignment
Retain CPU keeps memory contents across STOP/RUN and power dips
Norm signal Siemens 0–27648 integer range mapped to 0–100 % of analogue range
PIW / PQW Periphery input/output word (process image of analogue channel)

FAQ

Why is hysteresis required around 10 m/min?

Without hysteresis, a single-sample comparator chatters every PLC scan when the conveyor speed sits on the boundary, driving the fans between 30 % and the operator value many times per second. A 1 m/min dead-band (switch on at 10.5, off at 9.5) prevents wear on VFDs and audible noise on fans.

Where must the operator setpoints be stored so they survive a conveyor stop?

In a retentive data block, e.g. DB2, marked with the RETAIN attribute. S7-300 keeps the values across STOP/RUN, power dips, and complete power-off within the CPU's buffered retention time defined in the CPU hardware properties, as described in the SIMATIC S7-300 hardware and installation manual.

What scaling converts the 4–20 mA conveyor speed to m/min?

For a 0–50 m/min sensor: m/min = raw × 50 / 27648. The 10 m/min threshold corresponds to raw 5530. Use an FC (FC1) for the scaling so it is one-shot in OB1 and easy to retune.

Can I omit the snapshot DB and store values directly into the operator DB?

No. The snapshot is needed because the operator may continue editing HMI setpoints (recipe load, supervisor override) while the belt is in low-speed protection. Without a separate snapshot DB, the restore phase has nothing to copy back from. Two DBs (DB2 live, DB3 snapshot) is the simplest robust pattern.

Is the 5-line STL from the field report enough for production?

No. It selects between fixed 30 % and operator setpoint only when speed > 10, with no hysteresis, no retention, and only one fan. For a real oven with six chambers and a product-protection requirement, add FC20 (hysteresis), FC30 (snapshot/restore) and FC10 (six-channel multiplexer) as detailed above.

Back to blog