IMDHMIControls LibraryShop main store

Troubleshooting

Call a ladder function block from SFC in CODESYS so it executes

PLC programming Published 7 min read

Short answer

Setting SetGreen in an SFC step changes data in the TrafficLight instance but never runs its ladder body, so the SR flip-flop cannot respond. Call the same TrafficLight instance once per cycle from a program MainTask reaches, and route the command through a declared input such as SetGreen rather than writing an internal variable.

On this page
  1. Why writing SetGreen does not execute the block
  2. Add one cyclic call of the same instance
  3. Worked example: one call, one task cycle
  4. Route commands through declared inputs
  5. Symptom and fix table
  6. Verify online, in this order
  7. The decision for each case
  8. Questions asked

Setting an input does not run a function block — only a call does. In the setup behind this question, step0 of the SFC writes SetGreen in the TrafficLight instance and MainTask runs the chart every cycle, yet the SR flip-flop never moves, because nothing anywhere calls the TrafficLight instance and its ladder body is never scanned. The fix is one cyclic call of that same instance from a POU that MainTask reaches, with the command routed through a declared input. Once the call exists, the flip-flop answers within one task cycle; until then, no amount of writing to the instance changes its output.

Why writing SetGreen does not execute the block #

IEC 61131-3, the standard that defines the languages CODESYS implements, models a function block as a type plus instances: each declared instance owns a private data set, the body executes only when something calls that instance, and between calls the instance data simply holds its last values. Writing TrafficLight.SetGreen := TRUE; in a step action changes one Boolean in that data. It does not invoke the body, so the SR network is never evaluated and its output keeps the value it had — FALSE — however long the step stays active.

The chart is not the caller either. MainTask calls the SFC program, and within the chart CODESYS evaluates SFC elements left to right and top to bottom, so actions and parallel branches keep a defined order — but that order governs the chart's own elements only and never pulls an uncalled function block into the scan. The rule is not CODESYS-specific: an S7-1200 PID block must execute in a cyclic OB, not OB1, for the same reason. A timer depends on its call the same way, which is what explains why a CODESYS TON works in structured text but not ladder.

Add one cyclic call of the same instance #

Two placements work:

  • A program POU the task already runs. Declare the instance in that program and call it by its name; with an example instance fbTraffic : TrafficLight;, the call is the bare line fbTraffic;. Keep the chart and the calling program in the same task, MainTask here. CODESYS runs a task's programs in the order the task configuration lists them: the caller listed after the chart lets the flip-flop see the new SetGreen in the same pass, listed before costs one cycle.
  • Inside the chart. Insert a box element — the rectangle entry in the SFC element set — into the step action and type the instance name; autocomplete offers the declared instances. The block then executes while that step is active.

For logic that must stay alive across the whole chart, use the first placement: a task-level call runs every cycle whether any step is active or not.

Exactly one instance must sit under both the write and the call. If the SFC writes fbTraffic while the calling program runs an example second instance fbPilot, you have edited one and executed the other, and the output you watch belongs to an instance nobody calls. In the opening case the instance existed and the write reached it — what was missing was any call at all.

Worked example: one call, one task cycle #

All values here are example values. Setup: TrafficLight is a ladder function block with SetGreen : BOOL in VAR_INPUT and the SR flip-flop's output in VAR_OUTPUT; MainTask runs the chart and the calling program at an example interval of 10 ms; the calling program declares fbTraffic : TrafficLight; and contains fbTraffic;; step0's action contains fbTraffic.SetGreen := TRUE;.

  • Caller listed after the chart: the task runs the chart first and SetGreen becomes TRUE, then the caller runs fbTraffic, the SR network scans with set TRUE and reset FALSE, and the flip-flop output goes TRUE in the same pass. Added latency: 0 ms.
  • Caller listed before the chart: the flip-flop evaluates against the previous FALSE and answers one cycle later, at t = 10 ms. Worst-case latency: one task interval.
  • Evaluation rate once the call exists: 1000 ms ÷ 10 ms per cycle = 100 evaluations of the ladder body per second.
  • The same call parked in a second task with a 100 ms interval: still one evaluation per cycle, but 100 ms ÷ 10 ms = 10, so worst-case latency grows to 100 ms.
  • Call deleted: the evaluation rate is 0 per second and the output never moves, whatever the step writes.

The check that proves the fix: log in online, make step0 active, and watch the flip-flop output rise within one task cycle of SetGreen. If it does not, the call is not being reached — compare the instance the SFC writes with the instance the call names.

Route commands through declared inputs #

Keep SetGreen a declared input, in VAR_INPUT, never an internal variable written from outside. Internals are internal for a reason: a chart that writes into VAR couples itself to the block's private layout, and restructuring the ladder later breaks the chart in ways the compiler may not flag. If SetGreen sits in VAR, move it to VAR_INPUT; keep the SR flip-flop's latch bits in VAR; expose the flip-flop output through VAR_OUTPUT when another POU reads it. The interface then documents itself: the chart writes the inputs it owns, the block owns its state, and autocomplete offers SetGreen while you build the step action. If SetGreen is already in VAR_INPUT — as the wording 'the SetGreen input is set' suggests — the interface needs no change; the missing call was the whole fault.

While step0 stays active its action re-executes every scan, so the assignment behaves as a cyclic write rather than a one-shot pulse — the pattern that keeps a command from going stale when call order or tasks shift, as note on how cyclic writes resolve stale output commands describes.

Symptom and fix table #

What you observe What it means The fix
SetGreen TRUE in the instance while the flip-flop output stays FALSE The ladder body never executes; nothing calls the instance Add one cyclic call of that instance in a POU MainTask reaches
Output updates only while the step is active The call sits inside the step action, so it stops with the step Move the call to a program the task runs every cycle
The output you watch never moves and a second instance exists The chart writes one instance while the call runs another Make the step's write and the call name the same declared instance
The call line will not compile It names the FB type, which carries no instance data Call the declared instance name instead of the type

Verify online, in this order #

  1. Read the step action and note the instance it writes.
  2. Open the calling POU and confirm the call uses that exact instance name.
  3. Check the task configuration: both the chart program and the calling program are assigned to MainTask.
  4. Log in online, make step0 active, and confirm SetGreen rises in the written instance.
  5. Watch the flip-flop output rise within one task cycle; if it does not, put a breakpoint on the call line and confirm the call is reached every pass.
  6. If the flip-flop output drives anything beyond indication — a contactor, a valve, a start command — prove the corrected sequence against the machine's risk assessment and its documented safety functions before it returns to production.

The decision for each case #

  • SFC writes an input and no call exists anywhere — the opening case: add one cyclic call of the same instance in a program MainTask also runs. The flip-flop answers within one task cycle; done.
  • The call already exists in a slower task: it still drives the block but adds latency equal to that task's interval. Move the call into MainTask when the sequence cannot wait one slow cycle.
  • Two instances are in play: delete the extra declaration. One instance, one chart write, one call every cycle.
  • The command aims at an internal variable: move SetGreen to VAR_INPUT and write it only from the step action.
  • The call is bound to a step box: keep it only when evaluation should ride the active step; otherwise move it to the task-level program.

Questions asked

Why does CODESYS report 'A function block call requires a function block instance'?

The call names the function block type instead of a declared instance. A type is only a template and holds no data of its own, so it cannot execute. Declare one instance in the calling POU's variable table, then write the call with that instance name. CODESYS has issued this message at least as far back as version 2.3, and the cure has never changed: instance name, not type name.

Does the block keep running when the SFC moves to the next step?

Only if something calls it outside the chart. A call placed in a step action or a box runs while that step is active; when the step leaves, evaluation stops and the instance freezes with the outputs it last produced. When the step becomes active again, the block resumes from that frozen state and evaluates once against whatever its inputs are at that moment. For output logic that must stay serviced every cycle, call the instance from a program in the task.

Can two programs in different tasks call the same instance?

The compiler allows it, but the block then evaluates more than once per scan period with no defined order between the calls, and one task can read inputs the other has not written yet. Keep one writer and one caller per instance: let the chart own the input writes and let a single cyclic task call the block.

How do I tell which instance my SFC is writing?

Read the step action's assignment and take the leading name, the part before the dot: that identifier is the instance the chart writes. In online mode, make the step active and watch that path change value. Then compare it with the instance named in the call line. If the two differ, you are executing one block while watching another, which looks exactly like a function block that never runs.

Keep reading

  • How-to

    Connect a TON output to a function block input in TwinCAT ladder

    In TwinCAT ladder, the rung's power flow always lands on a block's first input, so with NT_GetTime it lands on NETID instead of START. Right-click the rung and choose Box with EN to create an enable path, then right-click the short line before START and use Insert at Block... to place a box there, replacing the default AND with TON by typing the name or using Input Assistant with F2.

    PLC programming

  • Troubleshooting

    Why a CODESYS TON output stays on after its rung goes false

    A false EN does not reset a CODESYS TON — it stops the block from executing at all, so Q holds the value from the last executed call. Route the process condition to IN, keep the enable permanently true, and the timer resets itself the way the standard intends.

    PLC programming

  • How-to

    How to OR a comparator with a contact in Machine Expert HVAC ladder

    Machine Expert - HVAC will not build a GT comparator block inside a ladder parallel branch. Get the same Boolean OR by splitting the logic across two networks or by replacing the block with an expression combined through an OR block, then verify all four input combinations before download.

    PLC programming