Edges & one-shots
A button held for half a second is seen by ten scans in a row. When you want to do something once per press, count a part or toggle a lamp, you need the moment the signal changes: its rising or falling edge.
By the end of this module you can
- Explain the difference between a level and an edge
- Use rising-edge and falling-edge contacts that are TRUE for exactly one scan
- Build a push-on/push-off toggle
- Know how Siemens, Rockwell and Mitsubishi edge instructions differ, and which one Logivyx matches
A normal contact looks at a level: is the bit 1 right now? An edge contact looks at a change: was the bit 0 last scan and 1 this scan? That is a rising edge (positive transition). The reverse, 1 → 0, is a falling edge. An edge is TRUE for exactly one scan.
Below, rung 1 adds 1 to a counter on every scan while PB1 is held. Rung 2 adds 1 only on the rising edge, rung 3 only on the falling edge. Press PB1 a few times, briefly and long.
At a 50 ms scan, a half-second press adds about 10 to Count_Level. In the trace each edge is a one-scan spike. Pause the simulator and use Single scan while holding PB1 to see the edge last exactly one scan.
Inside, an edge detector is tiny: it remembers the bit's value from the previous scan.
// Rising edge, what R_TRIG does internally Q := CLK AND NOT Prev; // 1 only if it is 1 now and was 0 before Prev := CLK; // remember for the next scan
That memory is why an edge needs its own instance or storage bit, and why every edge contact must use a different one. Share a storage bit between two edge instructions and they will miss edges.
One button, one lamp: press once for on, press again for off. A level contact would make the lamp flicker on every scan while the button is held. The edge makes it change exactly once per press.
Rung 2 is the exclusive-OR from module 05: when the pulse is 1 the lamp flips, otherwise it keeps its value. Click Show as Structured Text to see it as one line.
PB1_Rise(CLK := PB1); // R_TRIG instance
IF PB1_Rise.Q THEN
Count := Count + 1;
END_IF;
IEC 61131-3: function blocks R_TRIG and F_TRIG, and in ladder the contacts -|P|- and -|N|-. Logivyx: Contact — Rising Edge (One Shot) (OSR) and Falling Edge (OSF), each with an Instance name for its hidden R_TRIG/F_TRIG. It detects the edge of its own tag, regardless of the contacts before it, like the Siemens P contact.
"PB1" ---| P |----------------( "Toggle_Pulse" ) "PB1_Mem" (edge memory bit below the contact)
TIA Portal LAD: -|P|- / -|N|- contacts scan one operand for an edge and need a separate edge memory bit each (e.g. a %M bit or DB bool). P_TRIG / N_TRIG detect an edge of the whole rung result (RLO). -(P)- / -(N)- coils, and the R_TRIG / F_TRIG function blocks (instance DB) in LAD and SCL.
XIC PB1 ONS PB1_Storage ADD Count 1 Count XIC PB1 OSR PB1_Storage2 Toggle_Pulse
ONS is an input instruction: it passes power for one scan when the rung up to that point goes true, so put it after the conditions it should react to. OSR / OSF are output instructions with a storage bit and an output bit. Each needs its own storage bit, never shared. ST and FBD use OSRI/OSFI.
LDP X0 ; rising-edge contact on PB1 OUT M0 ; one-scan pulse LD X0 PLS M1 ; same idea as a coil instruction LDF X0 ; falling edge PLF M2
Pulse contacts LDP/LDF, ANDP/ANDF, ORP/ORF are drawn with an up or down arrow. PLS/PLF set a device for one scan. Many instructions also have a pulse form ending in P, e.g. MOVP, INCP, executed once on the rising edge.
Use PLC_Trainer.
L07_Edges
- New ladder routine
L07_Edges. Rung 1: Contact — Rising Edge (One Shot) onPB1, instance namePB1_Rise→ OTEToggle_Pulse. Create the BOOL tagToggle_Pulsewhen asked (or with + Add Tag). - Rung 2: the XOR toggle from this module → OTE
Q1. - Rung 3: rising edge on
PB2, instancePB2_Rise→ Raw ST ActionN1 := N1 + 1; - Rung 4: XIC
PB3(a plain level contact) → Raw ST ActionN2 := N2 + 1; - Rung 5: XIC
PB4→ Raw ST ActionN1 := 0;and a second outputN2 := 0;(reset). - Call
L07_EdgesfromMain, save, F5. - PB1 toggles Q1 once per press. PB2 adds 1 to N1 per press; PB3 makes N2 race upward while held. Explain the difference to a colleague in one sentence.