MOULDLKnowledge BaseElectrical Knowledge Base3D Model DownloadsMembership
中文版Model Store »
Knowledge Base › Electrical Knowledge Base › How do edge instructions differ between CODESYS and TwinCAT?
HomeModels
Table of contents

On this page

PLC & Programming (100 articles)

Quick Reference

  • PLC & Programming
  • Electrical & Electronic Basics (monthly)(每月更新中)
  • Electrical Control & Drawings (monthly)(每月更新中)
  • Drives & Actuators (monthly)(每月更新中)
  • Sensors & Detection (monthly)(每月更新中)
  • Industrial Communication & Networks (monthly)(每月更新中)
  • HMI & Host Systems (monthly)(每月更新中)
  • Industrial Robots & Motion Control (monthly)(每月更新中)
  • Mechatronic System Integration (monthly)(每月更新中)
  • Fault Diagnosis & Standards (monthly)(每月更新中)

How do edge instructions differ between CODESYS and TwinCAT?

A counter-intuitive conclusion first: CODESYS and TwinCAT barely differ on edge detection — both follow IEC 61131-3, where the standard method is the R_TRIG / F_TRIG function block, which requires an instance and keeps its own memory. The real contrast is between Mitsubishi-style platforms and IEC platforms: Mitsubishi's LDP/ANDP are instructions needing no instances. This article covers IEC usage, the translation table, and the two instance-borne traps.

1. Two paradigms compared

PlatformStyleState storedKey point
Mitsubishi FX/QLDP X0 / ANDP M10 instructionsImplicit, inside the systemWrite and go, nothing to declare
CODESYS (IEC)R_TRIG block: trig1(CLK:=x0)In the instance variable (remembers last CLK)One instance per detection point; Q is the edge output
TwinCAT (IEC)Same R_TRIG as CODESYSSameLadder also offers P contacts (edge contacts) directly

The standard ST form on IEC platforms:

trig1 : R_TRIG; … trig1(CLK := X0); IF trig1.Q THEN …

In TwinCAT ladder you can drop an edge contact (P contact for rising edge) directly — an implicit instance managed by the system, easier than declaring in ST.

2. Trap one: instances must not be shared

An R_TRIG instance remembers "what CLK was last cycle". Two signals sharing one instance overwrite each other's memory and the edges go haywire. The rule: one detection point, one instance, named after its signal (trig_btnStart for the start button); never share to save a declaration. The same applies to the same instance called from two tasks (different cycle times) — two rhythms cross-write the memory and the edges corrupt. One instance belongs to one call site in one task.

3. Trap two: call conditions and call frequency

MistakeConsequenceFix
R_TRIG called conditionally inside an IFMemory not updated while uncalled; the next call may emit a false edgeCall unconditionally every cycle; only the use of Q goes inside the IF
Instance in a 10 ms task, signal changes in the main taskSampling rhythm mismatched; short pulses missedEdge detection in the signal's own task, or verify the cycle is fast enough
Q used across cycles without latchingQ holds one cycle only; missed if not caughtHandle Q in the same cycle or SET-then-reset

"Unconditional every-cycle calling" is the most counter-intuitive rule: a Mitsubishi instruction executes where it is written, but an IEC FB that is not called does not update — an R_TRIG parked in a conditional branch is blind while the condition is false, and its first cycle back may report a spurious edge.

4. Translating from Mitsubishi style

MitsubishiIEC ST equivalentNote
LDP X0 → SET M10trigX0(CLK:=X0); IF trigX0.Q THEN m10:=TRUE;Declare the instance alongside
LDF X0 → RST M10F_TRIG instance likewise, Q is the falling edgeOne instance per point, same as R_TRIG
ANDP M10trigM10(CLK:=m10); IF trigM10.Q AND …Series edge = edge of the intermediate

When migrating old programs, translate every P instruction into one R_TRIG instance named after the original device — the mapping stays traceable.

5. The genuinely small differences between CODESYS and TwinCAT

① Edge contacts: TwinCAT ladder natively supports P/N edge contacts (implicit instances system-managed); CODESYS ladder has similar support, though ST + R_TRIG is the mainstream; ② library dependency: R_TRIG lives in the Standard library (CODESYS) and TwinCAT_Standard Utilities — just reference the library and mind versions; ③ task model: TwinCAT's multi-task structure is more prominent, so cross-task instance calls are the likelier trap there — note the owning task in the instance name or comment.

6. Notes

IEC's functional-block approach to edge detection is a double-edged sword: explicit instances make state visible and controllable (whose memory, whose edge), but introduce the shared-instance and conditional-call traps. Coming from Mitsubishi, burn two sentences into habit — "a P instruction = one instance + an unconditional call" — and edge detection never bites.

7. References

R_TRIG/F_TRIG definitions follow IEC 61131-3 standard function blocks; CODESYS per its Standard library documentation, TwinCAT per the Beckhoff TwinCAT 3 PLC Manual library chapters; Mitsubishi edge instructions per its programming manual's applied-instruction chapter; task scheduling and cycle setting per each platform's task configuration documentation.

8. Cautions

⚠️ Three traps to avoid:
① Several signals sharing one R_TRIG instance — edge outputs pollute each other;
② R_TRIG inside an IF branch — the first cycle back after the condition emits a false edge;
③ One instance called from two tasks — two sampling rhythms cross-write and edges drop out.

Two team rules: instance names must carry the signal name (trig_ + signal); R_TRIG calls always sit in networks executed every cycle — only the use of Q goes inside conditionals.

Related reading

Related 3D models: Ready to build it? Download free 3D models & CAD drawings at MOULDL »
MOULDL · Electrical Knowledge Base · free self-study reference for electrical & automation engineers, 3D CAD models at MOULDL