IMDHMIControls LibraryShop main store

Troubleshooting

When a CODESYS TON works in structured text but not ladder

PLC programming Published 6 min read

Short answer

The structured text routine counts because its four statements run in sequence inside one scan; the ladder version fails when the reset network clears Timer.Q before the addition samples it, or an unconditional reset restarts the timer every cycle. Put the call, the Q test, the addition and the reset of the same TON instance in one ladder network, left to right in that order, and it counts exactly one B per interval.

On this page
  1. What the structured text version really does
  2. Where the ladder translation breaks
  3. Scan trace: counting A up by 5.0 every 2 s with a 10 ms cycle
  4. Build the rung
  5. Questions asked

Your structured text routine counts because its four statements run in one fixed sequence inside every scan: call the TON with IN TRUE, test Timer.Q, add B to A, then call the same instance again with IN FALSE. The ladder version fails when the networks break that sequence — a reset network clears Timer.Q before the addition network samples it, or an unconditional reset restarts the timer every cycle so Timer.ET never reaches the preset. Keep the call, the Q test, the addition and the reset of that one TON instance in a single ladder network, left to right in exactly that order, and the ladder version counts one B per interval, same as the structured text. Below are the scan-order mechanics, a numeric trace of the broken and the fixed order, and the rung to build.

What the structured text version really does #

Under IEC 61131-3, a TON starts its elapsed time ET from zero on a rising edge of IN; while IN stays TRUE, ET accumulates and Q turns TRUE at the call where ET reaches PT; when IN goes FALSE, Q drops and ET returns to zero inside that same call.

Read the ST version against that behavior. Timer(IN := TRUE, PT := T) runs every scan and holds IN TRUE, so ET climbs toward T. On the scan where ET reaches T, Q is TRUE and A := A + B executes once. The closing call, Timer(IN := FALSE), runs only on that completion scan, and it zeroes ET and drops Q immediately, so the next scan's TRUE call starts a fresh interval. The conditionality of that second call — reset only when Q fired — and the order of all four statements are exactly what a naive ladder translation loses.

Where the ladder translation breaks #

In ladder, every network evaluates once per scan, top to bottom, and within a network the elements run left to right. The structured text order — time, test, add, reset — has to survive the translation, and when it does not the routine fails in one of three recognizable ways.

  • A never changes and Timer.Q pulses TRUE once per interval in the online watch. The reset network runs in the same scan the timer sets Q but before the addition samples it, so the pulse lives and dies inside one scan.
  • A never changes and Timer.Q never goes TRUE. The reset rung is not gated on Timer.Q: it drives the timer FALSE every scan, IN sees a rising edge every cycle, and ET snaps back to zero long before it reaches T.
  • A climbs every scan instead of every interval. The reset never touches the instance that is timing — a second TON instance took the reset, or the reset rung never energizes — so Q sticks TRUE and the addition runs every cycle.

Work down this list in the online watch:

  • The timing call and the reset call must name the same Timer instance; a second instance produces the every-scan climb.
  • The reset must be gated on Timer.Q; an always-TRUE rung driving it FALSE restarts the timer every scan.
  • The timer call must evaluate before the Q-gated addition, and the reset must be the last element that touches the instance in the scan.
  • Timer.ET should climb to T and return to zero once per interval; ET pinned near zero means restart every scan.

This once-only execution trap is not a CODESYS quirk; on Logix controllers the CompactLogix fault routine called once troubleshooting guide covers the matching case — a routine that executes once, and the order-dependent logic bugs that causes.

Scan trace: counting A up by 5.0 every 2 s with a 10 ms cycle #

The inputs come from the routine's own declarations — A : LREAL, B : LREAL, T : TIME — with illustrative values, since the code itself fixes none: A starts at 100.0, B := 5.0, T := T#2s, task cycle 10 ms. Substitute your own values before reusing the numbers.

The rule in words: with IN held TRUE, the TON sets Q at the call where ET reaches PT and holds it while IN stays TRUE; a call with IN FALSE zeroes ET and drops Q in that call; networks evaluate once per scan in order.

Fixed order: the preset is 2000 ms and the cycle is 10 ms, so one interval is 2000 / 10 = 200 scans. Scan 200: the timer call sets Q TRUE, the Q contact in the same network lets the addition run — A := 100.0 + 5.0 = 105.0 — and the reset call then zeroes ET and drops Q. Scan 400: A := 105.0 + 5.0 = 110.0. Scan 600: A := 115.0. One B per interval, exactly.

Broken order: the timer network sets Q TRUE at scan 200, the reset network zeroes ET and drops Q in that same scan, and the addition network samples Q FALSE — either it sits below the reset and the pulse is already gone, or it sits above the timer and reads the Q the previous scan's reset already cleared. A stays at 100.0 forever while the watch shows Timer.Q pulsing every 2 s.

Decision: if Timer.Q pulses once per interval and A does not move, the completion pulse is dying inside the scan — chain all four steps into one network. After the fix, A must read 105.0 at the 2 s mark and step by exactly 5.0 every 2 s; if it instead steps by 5.0 every scan, the reset is missing the timing instance.

Build the rung #

In CODESYS ladder, build the fix as one rung, left to right: an always-TRUE contact (or your real run permission) feeding the TON box — instance Timer, PT wired to T — then a Timer.Q contact feeding the addition element, an ADD or MOVE box computing A := A + B, then the Timer.Q contact again feeding the same Timer box with IN wired to FALSE on the pin. Elements execute left to right inside the network, so the scan runs time, test, add, reset on one instance before any other network gets a look at it.

The discipline is the same when you move logic the other way, from ladder into structured text: order decides the outcome, as it does when porting a PLC-5 filter routine to ControlLogix structured text.

Verify the fix in simulation: set T := T#100ms, run, and watch A step by exactly B ten times per second, then restore T. Do the short-preset test offline or with the machine's outputs disabled — a counting routine that fires at the wrong instant can step a live output.

If the watch then shows ET climbing to T and stepping to zero once per interval while A steps by exactly B, the ladder version is fixed; if not, one of the three broken arrangements above is still in place, and the symptoms tell you which.

Questions asked

Can the timer, the addition and the reset stay in three separate ladder networks?

Yes. Three networks work if the order is exactly the structured text order: the timer call with IN TRUE first, the addition gated on Timer.Q second, and the reset of the same instance, again gated on Timer.Q, last. Each network still evaluates once per scan in that order, so Q rises, the addition samples it and the reset clears it within one scan. What breaks the routine is any other order, a second timer instance, or a reset that runs every scan.

What should Timer.ET look like when the routine is healthy?

Watch Timer.ET rather than Timer.Q. In a healthy routine ET climbs toward the preset, snaps back to zero on the completion scan and starts climbing again, one climb per interval. ET pinned near zero every scan means the timer is restarted before it can time out, which points at an unconditional reset or an enable that drops every cycle. ET that reaches the preset and stays there means the reset never fires, so Q stays TRUE and A climbs every scan.

Is this scan-order trap specific to CODESYS?

No. One evaluation of every network per scan, in order, is standard ladder behavior under IEC 61131-3, and the TON rules, with Q falling and ET returning to zero when IN goes FALSE, come from the same standard. The structured text version hides the trap because its statements always run top to bottom in one pass. The same instance-and-order discipline decides whether this pattern works on any controller that implements the standard's ladder language and timers.

Keep reading

  • How-to

    Latch two level inputs for pump start and stop control

    Use one reset-dominant latch: set the pump when the low level probe reads dry and reset it when the high probe reads wet, so the pump holds its state between the probes. A rung that only compares the inputs drops out the moment water covers the low probe; the latch turns the probe spacing into hysteresis.

    PLC programming

  • Troubleshooting

    Start a PID ramp soak profile from ladder logic

    The profile never starts from ladder because the JMP stage handoff leaves the old stage on for three more scans, which swallows the one-scan PD pulse that must set the ramp/soak start bit. Replace the jump with SET and RST stage logic, then verify bit 11, the +33 octal control word and the +35 error flags before touching the PID instruction or table values.

    PLC programming

  • How-to

    Port a PLC-5 filter routine to ControlLogix structured text

    The filter is a first-order lag whose only state is the previous filtered value. Give every filtered signal its own instance of one filter UDT that also holds the exponent and coefficient, translate the rungs to ordered assignments in a periodic task, and preload the state on the first pass. Prove the port with a step: 63.2% of the change after one time constant, about 95% after three.

    PLC programming