Skip to content

33From the lab to the plant

Final project: a complete tank station with a state machine and alarms

The capstone that pulls the whole course together: start permissives, a CASE state machine with step timeouts, timers and a counter, a filtered level, an acknowledged alarm and a fault state.

Lesson 33 of 3416 minIEC 61131-3

What we are building

A station that runs batch after batch: fill the tank to a setpoint, hold for a settling period, drain to a low limit, log a batch, repeat. It stops safely on anything odd and raises an alarm that a human has to acknowledge. That is the skeleton of every real batch station, however large.

Decision 1: permissives before motion

No first step until the conditions are sound. Collect them into one named variable:

Permissive := NOT (LSH AND LSL) AND LevelF <= 99.0;

LSH AND LSL together is physically impossible, so it means a failed switch or a broken wire. One named condition beats seven conditions repeated in seven places: you read it in the watch table and know immediately why the station did not start.

Decision 2: the filtered level, not the raw one

A level measurement jitters, and any comparison against the raw value jitters with it. A one-line exponential filter:

LevelF := LevelF + 0.20 * (Level - LevelF);

The 0.20 means "take a fifth of the new reading", a reasonable trade between quiet and responsive. We copy the raw value once on the first scan, otherwise the filter starts at zero and we wait while it climbs.

Decision 3: a state machine, not a chain of conditions

Five states, each knowing its outputs and its one exit condition:

Step

State

Outputs

Exit

0

Idle

all off

run command and permissives healthy

1

Filling

pump

level ≥ 85%

2

Settling

all off

3 seconds

3

Draining

valve

level ≤ 12% or LSL

4

Complete

all off

one scan, counts the batch

9

Fault

all off

operator acknowledge

Two rules keep a state machine clean: an output is set in exactly one place, and each state has one clear exit. Fault is 9, well away from the sequence numbers, so the number alone tells you that you are outside the normal flow.

Decision 4: a timeout on every moving step

A step waiting on a physical event must wait with a limit. If fill is commanded and the level has not risen in 60 seconds, the pump has failed, a valve is shut, or the source is empty. Without a timeout the program waits forever while the pump runs dry.

One timer serves both moving steps, reset on any step change by comparing the current step with the previous one.

Decision 5: an ISA-18.2 style alarm

An alarm is not HiAlm := Level > 90. ISA-18.2 separates the process condition from the alarm state, and the practical consequence is this: the alarm stays visible until a human acknowledges it, even if the level has long since returned to normal. A deadband stops it chattering around the threshold:

  • Raised above 90% or on LSH.
  • Cleared only below 85% and after acknowledge.
  • The lamp flashes while unacknowledged, and goes steady once acknowledged but still active.

The lab panel has only two buttons, so a START press doubles as acknowledge whenever there is an unacknowledged alarm or a fault. On a real station that is a pushbutton or a click on the HMI.

Decision 6: edges, not levels

Start, acknowledge and batch counting are all events, and an event is read on a rising edge. Without R_TRIG the batch counter would log twenty batches a second because the completion condition is true every scan. That is also why state 4 exists: a state that lives for one scan, counts, and leaves.

Decision 7: who stops what

Stop returns the machine to idle in an orderly way. A fault goes to state 9, drops everything, and stays there. The difference is deliberate: a stop is an operator request, a fault is a condition that needs a person to understand what happened before anything restarts.

How to read the program

The lab program under this lesson is laid out in eight sections, in this order: setpoints, filter, events and edges, alarm, fault detection, state machine, step timing, outputs. The order is not decoration. Everything that reads comes before everything that decides, and everything that decides comes before everything that writes. Follow it in every program you write from here on.

Try it in the lab

The lab runs in your browser: an ST editor, a virtual process that suits the lesson, the same program as a live ladder diagram, a watch table, and export to Siemens SCL.

What to try

  1. Press START and follow Step: 1 fill, 2 settle, 3 drain, 4 complete, then 1 again.
  2. Watch Batches.CV rise with each batch; after 3 the station stops and the lamp lights. Press START for a new shift.
  3. Press STOP in any step: it returns to 0 at once.
  4. Change 80.0 to 95.0: the level reaches 90%, LSH trips and the alarm lights. That is the independent safety layer.

Terms in this lesson

People find this lesson by searching

complete plc project exampleCASE state machine structured texttank control plc programstep timeout plc sequenceISA 18.2 alarm acknowledgebatch counter plcstructured text capstone project

Course outline

  1. 01What is Structured Text, and when to use it instead of ladder
  2. 02How a PLC runs your code: the scan cycle and the process image
  3. 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
  4. 04Your first program: starting a pump with seal-in logic
  5. 05Data types: BOOL, INT, REAL, TIME and the rest
  6. 06Declaring variables: VAR, VAR_INPUT and RETAIN
  7. 07Literals and constants: 16#FF, 2#1010 and T#5s
  8. 08Type conversion and scaling a 4-20 mA signal
  9. 09Assignment and arithmetic in ST: := and operator precedence
  10. 10Boolean logic, comparisons and bit operations
  11. 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
  12. 12IF, ELSIF and ELSE: the rules and when to avoid them
  13. 13The CASE statement and your first state machine
  14. 14The FOR loop and the watchdog
  15. 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
  16. 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
  17. 17Timers: TON, TOF and TP
  18. 18Counters: CTU, CTD and CTUD
  19. 19SR and RS flip-flops: which one wins
  20. 20Writing your own function
  21. 21Writing a function block
  22. 22Arrays in Structured Text
  23. 23Structs and enumerations
  24. 24References and pointers
  25. 25The state machine pattern: a batch sequence that cannot get stuck
  26. 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
  27. 27Analogue signal processing: counts to engineering units, NE43 and filtering
  28. 28Closed-loop control in ST: hysteresis first, then a practical PID
  29. 29Motors and valves in practice: permissives, feedback and travel time
  30. 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
  31. 31Testing and debugging ST: the watch table, forcing and simulation
  32. 32An ST style guide: naming, comments, constants and version control
  33. 33Final project: a complete tank station with a state machine and alarms
  34. 3420 interview questions on ST and PLC programming, with answers