IMDHMIControls LibraryShop main store

Explainer

Dividing a PLC FBD into subsystems: blocks, not memory tags

PLC programming Published 6 min read By IMDHMI controls team

Short answer

Wrap each functional group of the diagram in a function block (or a function when it holds no state) whose body is FBD, with every crossing signal declared as an input or an output, and call one instance per scan from the parent diagram. Memory tags used as ports only hide the wiring: the interface stays scattered, any network can write it and nothing shows when it is used. IEC 61131-3 supplies the unit; the block declaration is the subsystem boundary.

On this page
  1. The unit the standard already defines
  2. Why a memory tag is not a port
  3. The state test picks function or function block
  4. Partition by responsibility, then declare the boundary
  5. One call per scan keeps the execution order
  6. What the split buys you
  7. The decision for each case
  8. Questions asked

Yes — and IEC 61131-3 already names the unit to do it with. The standard assembles a program out of program organization units — programs, functions and function blocks — and the body of any of them can be an FBD. The subsystem you are reaching for is a function block, or a function when the group holds no state, whose declaration lists every crossing signal as an input or an output and which is called once per scan from the diagram above it. Memory tags used as ports are not a subsystem boundary: they hide the wires instead of replacing them, spread the interface across the tag table, and let any network write a value another subsystem owns.

The unit the standard already defines #

IEC 61131-3 defines three program organization units. A program is the top level: one instance, assigned to a task. A function computes one result from its inputs and keeps nothing between calls — the standard reinitializes its local variables at every invocation. The function block is the one that matters here. It has a declaration part and a body: inputs are declared as VAR_INPUT, outputs as VAR_OUTPUT, and everything else lives in internal VAR that no other code can reach. Each declared instance carries its own copy of that state, and the values persist from one call to the next. Because the body may be written in FBD, a subsystem that is already drawn as a diagram moves into a block without being rewritten in another language.

Why a memory tag is not a port #

A global tag is shared data, not an interface. Its declaration says nothing about which side drives it and which side only reads, so to learn what one sub-diagram needs you must search every network that references the tag — the interface exists, but it is distributed over the whole project. Two networks can write the same tag, and the write later in scan order wins, silently. A block input is the opposite: it has exactly one driver, the value wired at the call — the same one-driver rule that decides how CODESYS FBD accepts two box outputs on one box. Put the boundary where the standard puts it, in a declaration, and the subsystem's needs can be read in one place.

The state test picks function or function block #

Before wrapping a group, apply the two tests that decide function or function block: does the group hold a value from one scan to the next, and is it needed more than once with independent data? A yes on either makes it a function block.

Take a signal-conditioning group as a worked example. A 12-bit ADC delivers 0 to 4095 counts for 0.0 to 100.0 °C, and the group scales the count, then filters it. The scaling is pure arithmetic:

temp [°C] = raw [counts] × 100.0 [°C] / 4095 [counts]

With raw = 2048 counts: 2048 × 100.0 = 204800, and 204800 / 4095 = 50.01 °C. No state survives the calculation, so a function could do it.

The filter is first-order: y = yPrev + k × (x − yPrev), with k = 0.2. With last scan's output yPrev = 48.00 °C and today's x = 50.01 °C:

y = 48.00 °C + 0.2 × (50.01 °C − 48.00 °C) = 48.00 °C + 0.402 °C = 48.40 °C

yPrev must survive from one scan to the next. In a function the standard reinitializes it at every call, and the output would collapse to 0.2 × x — about 10.0 °C for this reading. So the filter group is a function block, and the scaling folds into it or stays a function called inside it.

Partition by responsibility, then declare the boundary #

Draw the line around one responsibility — signal conditioning for one loop, the interlocks for one station, the sequence for one axis — not around whatever fits on a screen. Then list every value crossing the line. A value computed outside and read inside becomes VAR_INPUT; a value computed inside and read outside becomes VAR_OUTPUT; everything else moves inside as private state. The filter block's declaration, with example names, looks like this:

FUNCTION_BLOCK ZoneFilter
VAR_INPUT
    rawCount : INT;            (* ADC counts, 0 to 4095 *)
    filterK   : REAL := 0.2;   (* filter constant *)
END_VAR
VAR_OUTPUT
    tempC : REAL;              (* filtered temperature, 0.0 to 100.0 degrees C *)
END_VAR
VAR
    yPrev : REAL := 0.0;       (* last scan's output, updated at the end of each call *)
END_VAR

The test that proves the move: hold rawCount at 2048 with yPrev at 48.00 °C, and the instance's tempC must read 48.40 °C after the call — the same number the flat network produced. When a boundary needs many related values — a recipe with a setpoint, a ramp rate and a high limit — pass them as one structure-typed variable, and keep in mind why a TwinCAT struct is a type, not a data block: the type groups the data, but the block declaration is still what owns the boundary.

One call per scan keeps the execution order #

An instance executes only where something calls it — the fact behind calling a ladder function block from SFC in CODESYS so it executes. That is exactly the control you want while restructuring: place the call in the network where the original logic ran, and every value in the block is computed in the same scan position as before. Networks still run top to bottom, and inside the block the same data-flow rules apply, with feedback read from the instance's last execution exactly as a flat network reads its last-scan value.

Two rules follow. One call site per instance — a second call would run the filter or PID twice per scan and double its effective update rate. One instance per physical subsystem — the second zone gets its own declared instance with its own private state, which is the reuse the memory-tag method cannot give you, because copied tags must be renumbered by hand.

What the split buys you #

Aspect Memory tags as ports Function block boundary
Where the interface lives Distributed over the tag table One declaration
Who may write a crossing value Any network; the last write in scan order wins The block writes its outputs, nothing else
Internal state Visible project-wide Private to the instance
Reuse on a second zone Copy networks and renumber tags Declare a second instance
Execution control Invisible Fixed by the call site

After the move, check four things: every input has one driver; each instance is called once, in its original network position; the forced-input test above reproduces the flat version's outputs; and any value that must survive a power cycle still does — wrapping logic in a block does not make state retentive, that comes from how the instance is declared.

The decision for each case #

A group of pure arithmetic — scaling, unit conversion, a limit comparison with no memory — becomes a function: one result, no state, callable from anywhere.

A group that holds anything from one scan to the next — a filter, a timer, a PID, a sequence step, a totalizer — becomes a function block, with one declared instance per physical subsystem, called once per scan in the network where the old logic ran.

A boundary carrying many related values gets one structure-typed variable across it, grouped by what the data describes.

Memory tags keep only the values that belong to the whole line — line mode, health summary, part count — never a wire between two halves of one diagram. If you catch yourself adding a tag just to carry a signal from one page to the next, that signal is an input or output you have not declared yet. And one line does not move at all: logic that implements a safety function stays in the safety program with its own validation, whatever shape the standard-control diagram takes.

Questions asked

Can the body of a function block be an FBD?

Yes. IEC 61131-3 allows the body of any program organization unit to be written in any of its languages, and CODESYS-based environments let you select FBD as a function block's implementation language. The declaration part is unchanged; only the body is drawn as a diagram, so the subsystem stays a diagram inside the block instead of being rewritten in another language.

How many signals should cross one subsystem boundary?

Roughly three to eight. Past about a dozen, either bundle related values into one structure-typed variable or admit the block is doing two jobs and split it again. The purpose of the boundary is that a reader takes in the subsystem's needs at a glance; a twenty-input block has rebuilt the unwieldy diagram one level up, with extra clicking.

Does the split lengthen the scan?

The instructions still run the same number of times per scan; each instance adds one call and return, small next to the arithmetic inside it. The real hazard is an instance with two call sites, which silently doubles a filter's or PID's update rate. On a controller already close to its scan limit, time the program before and after on that same controller.

Where do timers and controller instances go?

Inside the block that owns that step, declared among its internal variables. They become part of the subsystem's private state, so the parent diagram cannot reset them by accident, and a second zone or machine gets its own copies when you declare its instance. A timer two blocks both care about belongs to the one that decides the timeout; the other reads an output.

Does wrapping logic in a block make its state retentive?

No. Retentive behavior comes from how the instance or its variables are declared, never from the block itself. Prove it before the machine depends on it: write a known value into the state variable, cycle power, and check what the instance holds on restart, because a filter value or step number that restarts at zero can move an output on the first scan.

Keep reading

  • Comparison

    Function or function block: the two tests that decide

    Run two tests. If any local value must survive between calls, or the POU declares a timer, counter or other function block inside itself, it must be a FUNCTION_BLOCK; only an instance keeps memory from one call to the next. Everything else is a FUNCTION, called directly with no instance.

    PLC programming

  • Selection guide

    S7-300 vs Q series IEC 61131-3 language support

    Both platforms meet IEC 61131-3 once you buy the right toolchain: S7-GRAPH gives the S7-300 sequential function chart with function block calls from steps, and GX Works3 gives the Q series the SFC nesting that IEC Developer V5.0 lacked. With languages equal, integrated motion decides: one QD77MS16 module synchronizes 16 axes over SSCNET III/H at a 0.222 ms cycle, while the S7-300's 4-axis FM 357-2 is discontinued. Choose Q with GX Works3 for new multi-axis machines; stay with Siemens only inside an installed S7-300 base.

    PLC programming

  • Comparison

    When to use structured text instead of ladder logic in a PLC

    Use ladder logic for machine-facing interlocks, permissives and outputs, because maintenance reads power flow under time pressure. Use structured text for arrays, structures, iteration and calculations, where a loop replaces a jump-heavy network. Both languages come from IEC 61131-3, so the practical decision is where each function lives and what crosses the boundary between them.

    PLC programming