31From the lab to the plant
Testing and debugging ST: the watch table, forcing and simulation
How to get a program right: the watch table, forcing and why it is dangerous online, single-scan stepping, simulation, breakpoints, and the eight most common ST mistakes.
Four tools
Every environment gives you the same four under different names:
Tool | What it does | When |
|---|---|---|
Watch table | shows live variable values while the program runs | always, it is the first thing you open |
Force | pins an input or output and overrides the program | test only, and with great care |
Single scan | executes one cycle and stops | tracing complicated logic step by step |
Simulation | a virtual PLC with no hardware | before you go to site |
Watch table first
Do not guess. Put the variables that tell the story in the table: the step number, the timer's Q and ET, the filtered value, the alarm latch. Eighty percent of bugs show up the moment you compare what you see with what you expected. In this site's lab the table updates once per scan.
Forcing, and why it is dangerous
A force does not change the program; it disconnects a value from reality. A forced output stays forced across a program download, and on some controllers across a power cycle. The rules:
- Never force an output on connected equipment without written permission and a person standing at the device.
- Never force a safety input.
- Write every force down, remove it before you leave, and confirm the force list is empty.
- If a force is needed for the station to run, you are not testing, you are hiding a fault.
The cleaner way to test is an internal simulation variable in the program: it is visible in the code and it disappears with the code.
Single scan
It freezes time. Execute one cycle, read the table, understand. Excellent for finding out in which scan exactly a value changes, and for watching edge detection work. What it is useless for: anything that depends on real time passing, because timers count the clock, not scans.
Simulation
- PLCSIM and PLCSIM Advanced in TIA Portal: a virtual controller that talks to the program and the HMI.
- CODESYS simulation: runs the project on your laptop with one click, breakpoints included.
- This site's lab: an editor, a virtual tank with a pump and a valve, and a watch table, for learning and quick experiments.
Simulation finds logic bugs. It does not find wiring bugs or the real timing of a motor. Treat it as stage one of three: simulate, then bench-test with a real controller and switches, then commission on site.
Breakpoints
CODESYS and TwinCAT let you set a breakpoint on an ST line. Be clear about what happens: the controller halts there, outputs hold their last value, and timers keep counting. On real equipment that means freezing a machine mid-stroke. Breakpoints belong in simulation and on the test bench, not on a running line.
Eight mistakes that keep happening
=instead of:=—Pump = TRUE;is a comparison with a discarded result, not an assignment. Most compilers reject it; a few accept it quietly.- A missing semicolon — the error is usually reported on the next line, so look above the line named.
- A missing
END_IF— the compiler complains about the end of the block while the real cause is a condition opened far earlier. - A timer called in a branch that does not run every scan — the classic ST bug. An instance only advances when it is called.
- Integer division —
7 / 2is3. Convert toREALfirst, or multiply before you divide. - An undetected edge — compare the current state with the previous one, or use
R_TRIG. Without it, one button press counts once per scan. - Writing an output twice —
Pump := TRUE;in one place andPump := FALSE;in another: the last assignment wins and the first vanishes without a trace. - An uninitialised
RETAIN— after a battery change or a cold download the batch counter or the setpoint starts at zero. Check them on the first scan.
The lab program has a bug
The program loaded under this lesson is supposed to fill the tank to 60%, hold for 3 seconds, then open the drain valve. Run it and the level stops at 60% and nothing else happens.
The cause is on the list above: the timer is called inside IF Start THEN. Start is a momentary button, so the timer instance only advances while your finger is on it and never completes its three seconds. The watch table says so plainly: Hold.ET stays at zero.
The fix: take the timer call out of the condition so it runs every scan. Delete the IF Start THEN line and its END_IF, leaving the call at program level:
AtLevel := Level >= 60.0;
Hold(IN := AtLevel, PT := T#3s);The starting condition belongs in the timer's IN, never in a condition wrapped around the call. That rule alone prevents the whole class of bug.
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
- Run the program and hold OPEN for a second: the beacon lights, then the door rises.
- Hold CLOSE, and while it comes down turn on "car in doorway": the door keeps going and CRUSH appears. That is the bug.
- In the watch table notice Beam = TRUE while MotDown is still TRUE.
- Fix it by adding AND NOT Beam to the MotDown line, and test again.
People find this lesson by searching
plc debuggingplc watch tableforcing plc outputs dangerplcsim simulationstructured text common mistakessingle scan plctest plc program offline
Course outline
- 01What is Structured Text, and when to use it instead of ladder
- 02How a PLC runs your code: the scan cycle and the process image
- 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
- 04Your first program: starting a pump with seal-in logic
- 05Data types: BOOL, INT, REAL, TIME and the rest
- 06Declaring variables: VAR, VAR_INPUT and RETAIN
- 07Literals and constants: 16#FF, 2#1010 and T#5s
- 08Type conversion and scaling a 4-20 mA signal
- 09Assignment and arithmetic in ST: := and operator precedence
- 10Boolean logic, comparisons and bit operations
- 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
- 12IF, ELSIF and ELSE: the rules and when to avoid them
- 13The CASE statement and your first state machine
- 14The FOR loop and the watchdog
- 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
- 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
- 17Timers: TON, TOF and TP
- 18Counters: CTU, CTD and CTUD
- 19SR and RS flip-flops: which one wins
- 20Writing your own function
- 21Writing a function block
- 22Arrays in Structured Text
- 23Structs and enumerations
- 24References and pointers
- 25The state machine pattern: a batch sequence that cannot get stuck
- 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
- 27Analogue signal processing: counts to engineering units, NE43 and filtering
- 28Closed-loop control in ST: hysteresis first, then a practical PID
- 29Motors and valves in practice: permissives, feedback and travel time
- 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
- 31Testing and debugging ST: the watch table, forcing and simulation
- 32An ST style guide: naming, comments, constants and version control
- 33Final project: a complete tank station with a state machine and alarms
- 3420 interview questions on ST and PLC programming, with answers