IMDHMIControls LibraryShop main store

Troubleshooting

Why a structured text TON trips an alarm immediately in an FX CPU

PLC programming Published 8 min read

Short answer

The instant alarm comes from a timer preset that never receives a value: a TON whose PT input is empty meets its finish condition on the first scan, so Q drives Alarm before the stroke has run at all. Assign the preset a real TIME value, call every timer instance once per scan so none freezes with stale state, and replace the interlocked flags with a CASE state machine so Alarm has exactly one writer.

On this page
  1. Match the symptom to the defect
  2. Why a zero preset trips the TON on the first scan
  3. Setting the preset for a ten-second pusher timeout
  4. Why a skipped timer instance keeps stale state
  5. Confirm both faults in the device monitor
  6. Replacing the flags with a CASE state machine
  7. Test the rewrite against each fault case
  8. Questions asked

Your alarm trips immediately because the timer preset never receives a value. AlarmTid feeds PT on both TON calls, and a TIME label that never gets a value leaves the timer with no delay at all, so the finish condition is already true on the first scan after Aktiver — before the stroke has had any time to fail. That one label stops the instant trip, but three more defects wait behind it: the timers are called inside IF branches and freeze with stale state, two timer instances both write Alarm, and Aktiver works as a level instead of an edge. The full fix is to assign the preset, call every timer once per scan, and replace the interlocked flags with a CASE state machine. The TON behavior here is the standard function block of IEC 61131-3, documented for FX structured text in the FXCPU structured programming manual.

Match the symptom to the defect #

Run down this table before you edit anything; each row names the defect behind one part of the 'buggy' behavior.

What you see Likely cause First check
Alarm sets on the first scan after Aktiver; the motor never visibly moves AlarmTid is never assigned, so PT is 0 Watch the preset value in the device monitor
Alarm already TRUE at power-up or RUN, before any command Alarm is latched or retentive and the M8002 init block never writes it Check the retain or latch setting on the label; clear it explicitly in the init logic
Alarm comes back right after Reset The timer instance was skipped while the alarm was active, so it still holds Q and ET Watch TON_1.ET across a reset
Pusher extends, touches SensorUte, then runs out again or never retracts A held Aktiver re-asserts SkyvUt every scan Release Aktiver mid-stroke and watch whether the cycle completes
The retract timeout fires late or never Two instances write Alarm, and the retract timer only runs inside its own branch Watch which instance has Q TRUE when the alarm lands

Leave the sensors and wiring alone for now. An alarm that appears within one scan of Aktiver is arithmetic, not mechanics: the timeout has had no time to elapse.

Why a zero preset trips the TON on the first scan #

IEC 61131-3 defines the TON so that ET accumulates while IN is TRUE and Q sets once ET reaches PT, with PT typed as TIME. Your code never assigns AlarmTid, so TON_1 starts its first evaluation with a preset of 0 and the finish condition is already satisfied — Q returns TRUE in the same scan that sets SkyvUt and SkyverUtMotor := TRUE. The IF Alarm block at the bottom of the program then clears both motor outputs before the pass ends.

The motor output is therefore written TRUE and then FALSE inside one scan. The output image transfers to the physical terminals at end-of-scan processing, so a bit flipped on and off within a single pass may never visibly energize — what the PLC scan cycle does in each pass walks through those stages. That is the whole 'buggy' first symptom in one line of arithmetic. Giving AlarmTid a real TIME value is the first fix; the next section works it out for your case.

Setting the preset for a ten-second pusher timeout #

This works your own case: the maneuver must complete inside the limit your test problem sets, timed by TON_1 on the way out and TON_2 on the way back.

  • Required timeout: 10 s — the limit the problem statement places on the maneuver.
  • Preset label: AlarmTid, type TIME, already wired to PT on both calls in your code.
  • Timer instances: two, one per step, so each step can carry a full timeout.

The rule in words: a TON sets Q when ET has accumulated continuously for exactly PT while IN stays TRUE, so the assigned preset must equal the longest a step may legitimately run, and the value must exist before the step's first scan.

The arithmetic: as the program stands, AlarmTid never receives a value, so PT is 0 ms and the finish condition ET ≥ PT is satisfied on the very first evaluation — Q sets in the same scan the step starts. With AlarmTid := T#10s, PT is 10,000 ms and Q stays FALSE until 10,000 ms of continuous TRUE have accumulated, so the alarm lands no earlier than the stroke has genuinely been stuck for the full timeout, and within one scan of it.

Result: an instant trip becomes a trip after one full preset interval. Decision: with a 10 s requirement, assign exactly that — AlarmTid := T#10s — and test nothing else first; whatever the monitor shows for the preset at RUN decides this fault on its own.

Why a skipped timer instance keeps stale state #

A function block instance only updates when it executes. TON_1 runs only while SkyvUt AND NOT SkyvTilbake AND NOT Alarm is true, and TON_2 only inside the retract branch; skip the branch and the instance freezes with whatever Q and ET it last had. A TON clears itself only when it executes with IN FALSE, and a block that is never called never sees IN go FALSE — the same mechanism behind why a CODESYS TON output stays on after its rung goes false.

That produces three consequences in your program:

  • When SensorUte ends the push-out step, TON_1 stops being called with its internal IN still TRUE and ET partway up. The next push starts the timer from stale state, not from a clean start.
  • When a timeout trips, NOT Alarm removes both branches from execution, so neither timer can ever see IN go FALSE. Reset writes Alarm := FALSE, but the tripped instance still holds Q, and the moment its branch runs again it re-asserts the alarm.
  • Both calls map Q onto the same bit, so each executed call overwrites Alarm with its own result and one timer can erase the other's trip — a double-coil, in structured-text form.

The rule that comes out of this: a TON can only answer 'has this step run too long' if it is called every scan with IN tied to the step condition. Never call a timeout timer from inside the logic it supervises.

Confirm both faults in the device monitor #

Put the FX CPU in RUN and open the device and label monitor in GX Works 2. Add AlarmTid, TON_1.ET, TON_1.Q, TON_2.ET, TON_2.Q, Alarm, SkyvUt, SkyvTilbake and Aktiver, then work down the list in order.

  1. Read the preset first. Whatever the monitor shows for AlarmTid decides this fault by itself, so correct it before touching anything else.
  2. Clear the alarm with Reset and watch TON_1.ET. If ET does not drop while the instance is not being called, the stale-state defect is confirmed.
  3. Power-cycle or STOP/RUN with Alarm TRUE. If it is still TRUE after the M8002 pulse, the bit is retentive or latched and the init block never clears it — clear it explicitly in the init logic.
  4. Hold Aktiver TRUE through a full stroke. If the pusher never settles in the retracted position, the level-triggered start is overriding the retract step.

The first two checks take under a minute and decide whether the rest of the list matters.

Replacing the flags with a CASE state machine #

Four interlocked flags — SkyvUt, SkyvTilbake, Ferdig, Alarm — plus Reset writing into them let several parts of the program write the same bits in the same scan. One integer state removes the overlap: only one state is active at a time, each state owns its motor outputs, and Alarm stops being a flag written from two timer calls and becomes a decoded output written in exactly one place. Call both timers unconditionally every scan with IN tied to the state each one supervises, and every step keeps its full timeout, including the first pass after a reset.

The rewrite also fixes the start: Aktiver is edge-detected with a one-scan compare and honored only in the home state, so holding it high cannot re-assert the push-out mid-stroke, and Reset acts only in the alarm state, sending the machine back through the retract step instead of straight to idle. A CASE state machine is the structured-text workhorse for sequence logic — which IEC 61131-3 PLC language to use for each control task puts sequences like this one in ST for exactly this reason.

The names that are not yours are example declarations: State, PrevStart and StartNow are new, while TON_1, TON_2, the sensors, the motor outputs and the M8002 init pulse are exactly as your program has them.

(* example declarations *)
State     : INT  := 1;         (* 1 home, 2 push out, 3 retract, 9 alarm *)
PrevStart : BOOL := FALSE;
AlarmTid  : TIME := T#10s;     (* the timeout preset, assigned here *)
Alarm     : BOOL := FALSE;

IF M8002 THEN
  State  := 1;               (* if the pusher can sit out at power-up,
                                enter through state 3 to bring it home *)
  Ferdig := FALSE;
END_IF;

StartNow   := Aktiver AND NOT PrevStart;   (* one-scan rising edge *)
PrevStart  := Aktiver;

CASE State OF
  1: (* home *)
    SkyverUtMotor  := FALSE;
    SkyverInnMotor := FALSE;
    IF StartNow THEN
      Ferdig := FALSE;
      State  := 2;
    END_IF;

  2: (* push out *)
    SkyverUtMotor := TRUE;
    IF SensorUte THEN
      SkyverUtMotor := FALSE;
      State := 3;
    ELSIF TON_1.Q THEN
      State := 9;
    END_IF;

  3: (* retract *)
    SkyverInnMotor := TRUE;
    IF SensorInne THEN
      SkyverInnMotor := FALSE;
      Ferdig := TRUE;
      State := 1;
    ELSIF TON_2.Q THEN
      State := 9;
    END_IF;

  9: (* alarm *)
    SkyverUtMotor  := FALSE;
    SkyverInnMotor := FALSE;
    IF Reset THEN
      State := 3;            (* return to home through the retract step *)
    END_IF;
END_CASE;

Alarm := (State = 9);        (* decoded output, written in one place *)

(* both timers run every scan, IN tied to the step each one supervises *)
TON_1(IN := (State = 2), PT := AlarmTid);
TON_2(IN := (State = 3), PT := AlarmTid);

Two details matter. BOOL labels get TRUE and FALSE — the original Reset wrote Alarm := 0, which mixes a number into a BOOL — and if the alarm really must survive a power cycle, make that a deliberate retentive latch and clear it in the init logic; as written above it clears on the M8002 pulse because State reinitializes. Prove the new sequence on a machine that can cycle safely before running parts: a state machine with a defect still drives the pusher motor with full authority.

Test the rewrite against each fault case #

Run these six checks against the monitor from the previous section; each one maps to a row in the symptom table.

  • Normal cycle. Pulse Aktiver. Expect state 2 with the out motor on, SensorUte moves it to state 3 with the in motor on, and SensorInne returns it to state 1 with Ferdig TRUE.
  • Held start. Keep Aktiver TRUE through the whole stroke. Expect exactly one cycle, with no restart until it is released and pressed again.
  • Out timeout. Never make SensorUte. Expect the alarm one full preset interval after entering state 2, both motors off; time it against the preset you assigned.
  • Return timeout. Make SensorUte but never SensorInne. Expect the alarm one full preset after entering state 3 — measured from the transition, not from the start button.
  • Reset path. With the alarm active, press Reset. Expect a retract to home through state 3, Alarm FALSE the moment State leaves 9, and neither timer re-tripping on stale state.
  • Second cycle. After a reset and a completed cycle, start again. Both timeouts must run from a clean start, because each instance now executes every scan and saw its IN go FALSE when its step ended.

Questions asked

Can two TON instances drive one alarm bit if I keep the flag structure?

Not by passing Q to Alarm from both calls, because each executed call overwrites the bit with its own result and one timer can erase the other's trip inside the same scan. If you keep the flag structure instead of the state machine, compute the alarm once in a single assignment such as Alarm := TON_1.Q OR TON_2.Q, and let no other line of code write that bit.

How can I prove the timeout works before running the real machine?

Cycle the logic in the simulator that ships with the programming package, or force the inputs from the monitor with the machine disabled. Make the out sensor never come on and watch the elapsed value of the push-out timer: its Q must set one full preset after the step starts, with both motor outputs off. Repeat for the retract step by holding the out sensor on and the in sensor off.

Where should the preset value live if other blocks need the same timeout?

Declare it once as TIME and pass it into each block as an input, or give it an initial value where the label is declared. What matters is that the value exists before the first evaluation of any timer that uses it: a constant, a declared initial value and a write from a setup block all qualify, provided nothing can write a shorter value while a stroke is running.

Does scan time shift when the timeout fires?

By less than one scan. The timer is evaluated once per program pass, so Q can set no earlier than the preset and no later than one preset plus one scan. Against a ten-second timeout that error is negligible; it only matters when a step can legitimately finish within a single scan, in which case a scan-based timeout is the wrong tool for that step.

Keep reading

  • Troubleshooting

    When a CODESYS TON works in structured text but not ladder

    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.

    PLC programming

  • Explainer

    How a REPEAT loop runs in PLC structured text

    In IEC 61131-3 structured text, a REPEAT loop executes its body first and evaluates the UNTIL condition after it, so the body always runs at least once and the whole loop completes inside one task invocation. With i := 0 and UNTIL i >= 10, the body runs 10 times and i ends at exactly 10; the test position is what explains the off-by-one cases and when WHILE fits better.

    PLC programming

  • How-to

    Run Logix 5000 PID loops in a periodic task

    No. Thirty PID instructions in a FOR loop in a continuous task, with no timer, execute once per scan at a varying interval, so their loop update time setting never matches reality. Move the routine into a periodic task whose period equals the shared update time, and write that same period into every loop's .UPD.

    PLC programming