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.
| Platform | Style | State stored | Key point |
|---|---|---|---|
| Mitsubishi FX/Q | LDP X0 / ANDP M10 instructions | Implicit, inside the system | Write 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 CODESYS | Same | Ladder also offers P contacts (edge contacts) directly |
The standard ST form on IEC platforms:
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.
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.
| Mistake | Consequence | Fix |
|---|---|---|
| R_TRIG called conditionally inside an IF | Memory not updated while uncalled; the next call may emit a false edge | Call unconditionally every cycle; only the use of Q goes inside the IF |
| Instance in a 10 ms task, signal changes in the main task | Sampling rhythm mismatched; short pulses missed | Edge detection in the signal's own task, or verify the cycle is fast enough |
| Q used across cycles without latching | Q holds one cycle only; missed if not caught | Handle 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.
| Mitsubishi | IEC ST equivalent | Note |
|---|---|---|
| LDP X0 → SET M10 | trigX0(CLK:=X0); IF trigX0.Q THEN m10:=TRUE; | Declare the instance alongside |
| LDF X0 → RST M10 | F_TRIG instance likewise, Q is the falling edge | One instance per point, same as R_TRIG |
| ANDP M10 | trigM10(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.
① 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.
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.
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.
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.