IMDHMIControls LibraryShop main store

How-to

Map CODESYS I/O for ladder and structured text use

Controllers and I/O Published 7 min read

Short answer

Map every physical channel once, in the I/O mapping dialog of the device in the project tree, to a named global variable. Ladder and structured text then reference the same variables, because the mapping belongs to the device, not to either language. Prove the link with one input and one output through the full field-device-to-program path before mapping the rest.

On this page
  1. One mapping, both languages
  2. Map the channels in the device editor
  3. Use the same variables from ladder and structured text
  4. Prove one input and one output end to end
  5. Worked example: one input and one output from a DIO module in both languages
  6. Mistakes that break two-language I/O
  7. Questions asked

Map each physical channel once, in the I/O mapping view of the device in the project tree, to one named global variable. Ladder and structured text then reference those same variables, because in CODESYS the mapping links a device's input and output channels to variables of the project: it belongs to the device and its configuration, not to any programming language. If your application already runs in structured text and a digital I/O module is attached to the controller, the only work left is to link that module's channels to variables both POUs can see, then prove the link on one input and one output through the full field-device-to-program path. The steps below do that in order.

One mapping, both languages #

Mapping, in CODESYS terms, is the link between a channel on a device and a variable in the project. The link is stored with the device configuration. Every POU in the application compiles against the same variable space, and nothing in a mapping records which language uses a variable, so one variable serves ladder and structured text equally.

That has one practical consequence: never create a second mapping of the same channel just to give the second language "its own" copy. Two variables on one input both receive the field value, but two variables on one output are two independent drivers on the same physical point, and that is a fault, not a convenience. Map once, link once, share the variable.

For the channel-level mechanics — picking the module under the device, reading each channel's address and entering its variable — the walk-through on mapping physical I/O channels in CODESYS V3.5 covers the same dialog step by step.

There is a second, optional access route: addressing channels directly as IEC objects. That requires the symbolic access option for I/O, switched on in the device's PLC settings. If you link variables as described below you do not need it; use one route or the other consistently.

Map the channels in the device editor #

  1. Open the project tree and find the digital I/O module under the controller device. If the module does not appear, fix the module configuration first: a missing module is a device-setup fault, and no mapping can be written for channels that do not exist in the project.
  2. Double-click the module to open its device editor and switch to the I/O mapping view. This is the view the CODESYS documentation describes as the place where input and output channels of the device are linked with variables of the project.
  3. Work down the channel list and enter an application variable for each channel you need. Pick an existing global variable or let the view create one in place. A digital channel carries one bit, so its variable is a BOOL.
  4. Name variables after the field device, not the channel position, and declare their types once, for example ixStartPB : BOOL for an input and qxRunLamp : BOOL for an output. These are illustrative names; use your own plant naming.
  5. Keep the mapped variables in a global variable list, so every POU in the application sees them without copies, parameters or extra declarations.
  6. Confirm each row before you leave the view: correct channel, correct variable, correct type.
  7. Download the application with the updated device configuration. The mapping travels with the configuration, so a controller still running the previous download has no links at all, however correct the project looks on your PC.

Use the same variables from ladder and structured text #

From ladder, the mapped names appear as ordinary contacts and coils: a contact on ixStartPB, a coil on qxRunLamp. From structured text, the same names appear in assignments and conditions:

IF ixStartPB THEN
    xRun := TRUE;
END_IF

There is no per-language mapping anywhere in this. If a ladder rung and a structured text condition both read ixStartPB, they read the same bits, refreshed from the field each scan. Write access follows one rule: exactly one POU may write each mapped output, and any number of POUs may read it.

Which parts of the logic belong in ladder and which in structured text is a separate design question from mapping; the guide on which IEC 61131-3 PLC language to use for each control task sorts that out per task type.

If one language appears to behave differently from the other on the same variables, suspect how the POUs call their logic, not the mapping. The case of a CODESYS TON that works in structured text but not ladder is that class of fault, worked through in detail.

Prove one input and one output end to end #

Before mapping the rest of the module, prove the path once with a single input and a single output:

  • Toggle the real input device and watch the mapped variable change in the device editor's live values, then watch the ladder contact and the structured text condition react to the same variable.
  • Write TRUE to the output variable from the structured text POU, and confirm both the ladder rung and the field device respond.
  • Repeat the output write from the ladder POU and confirm the same two responses.
  • Remove any write or force commands you used for the test when the test is done.

Drive the test output into a lamp or a spare terminal only, never into an actuator that can move a machine part or product while you are proving the path.

One scan-level fact belongs in this test: mapped variables exchange with the program through the process image once per scan, so a counter or one-shot fed from a level in logic that executes every scan triggers every scan. That is why count and event logic needs its own edge detection, as shown on the page about why a counter can increment every scan instead of once.

Worked example: one input and one output from a DIO module in both languages #

The case is the one this article addresses: a digital I/O module attached to a CODESYS application that already runs in structured text, with a ladder POU being added.

Inputs and where each comes from:

  • 1 digital input channel, wired to a maintained pushbutton — the attached module and running application come from the reader's project state.
  • 1 digital output channel, wired to a test lamp — the test-lamp choice is this site's acceptance method, not a CODESYS rule.
  • 2 POUs: the existing structured text program and one ladder program added to the same application and task.
  • 2 mapped variables, ixStartPB : BOOL and qxRunLamp : BOOL, in a global variable list.
  • 3 toggle cycles on the input and 1 write from each POU on the output — the method defined in the previous section.

The rule, in words: each channel maps once to one global variable; every scan refreshes input variables from the field and output variables to the field; both POUs reference those variables, so each toggle must reach both languages and each output write must be seen by the other language and by the field device.

The arithmetic:

  • Input side: 3 toggles × 2 POUs that must react = 6 observations.
  • Output side: 2 writes (one per POU) × 2 observers (the other POU's logic and the lamp) = 4 checks.
  • Total acceptance checks: 6 + 4 = 10.

The result: the mapping is proven only when all 10 checks pass — every toggle seen by both POUs, every output write seen by the other POU and by the lamp, and no write landing on a channel other than the one commanded.

The decision: with 10 of 10 passing, map the remaining channels the same way in the same view and expand the logic. If any single check fails, stop expanding. A failed check points at the channel-to-variable link, a stale download, or a POU that no task calls — all device and configuration matters — not at ladder or structured text itself. Fix the configuration, repeat the failing check, and only then continue.

Mistakes that break two-language I/O #

  • Mapping one output channel to two different variables and driving one from each POU. The last write in task order wins every scan, so the two languages fight on the wire. Give every mapped output exactly one owning POU; if both must command it, have each write a request variable and let one small POU arbitrate.
  • Declaring a local variable with the same name as a mapped global inside one POU. That POU now reads its own local copy and never sees the field signal. Keep mapped names global and unique.
  • Adjusting logic to fix a channel that was never mapped. If the variable does not appear in the I/O mapping view, no POU can read it; correct the mapping first.
  • Editing mappings and then running without a download. The mapping is part of the device configuration and takes effect only when the configuration reaches the controller.

Questions asked

Does the second language need its own copy of the mapped variables?

No. The variables live in a global variable list of the application, and the mapping links device channels to those variables. The ladder program and the structured text program declare nothing extra; they reference the same names. A local variable with the same name in one POU shadows the shared variable, so keep the mapped names global and use them unchanged in both POUs.

Do I need to redo the runtime or library integration before mapping I/O?

No. An application that is already running proves the integration is complete. Work at the device level: confirm the I/O module appears under the device in the project tree and that its channels show in the I/O mapping view. Re-integration is a last resort, only when the module itself never appears, and it follows the vendor's own setup guide.

Why does my mapped input change but the ladder rung never reacts?

The mapping updates the variable every scan, but a program only acts on variables it actually evaluates. Check that the ladder POU is assigned to a task and is called each cycle. An uncalled POU compiles cleanly and never executes, so its contacts sit at their initial state while the variable itself moves in a watch table.

What stops the two languages from fighting over one output?

Ownership. Assign every mapped output to exactly one POU. If both the ladder and the structured text program must command the same output, arbitrate in logic: each program writes its own request variable, and one small POU combines the requests and alone writes the mapped output. Otherwise the last write in task order wins every scan.

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

  • Selection guide

    Which IEC 61131-3 PLC language to use for each control task

    Match each control task to the IEC 61131-3 language whose form shows its dominant data path: ladder for discrete interlocks, structured text for calculations and data handling, function block diagram for connected signal functions, SFC for phased sequences. Instruction List is deprecated, so new code uses the other four, and the controller's supported languages set the final limit.

    PLC programming

  • Explainer

    How SoapBox Snap runs ladder logic with USB I/O

    SoapBox Snap is a GPL-v3 Windows application that edits and executes ladder logic on the PC. It has no I/O of its own: every physical channel comes from a loaded driver, and the described release documents two — Phidgets USB devices and a Twitter web service. Commission the device path first with no load connected, and keep machine safety hardwired.

    PLC programming