16Standard function blocks
Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
How to make something happen once when a button is pressed instead of once per scan, using R_TRIG and F_TRIG.
The bug everyone writes once
You want to count how many times the operator pressed start, so you write:
IF Start THEN
Starts := Starts + 1;
END_IF;One short press and the counter reads 14. A short press is about 300 ms, the scan is 20 ms, so the condition was true for 15 consecutive scans. The program never sees a press. It sees the state of the button, once per scan.
The edge
What you want is the edge: the moment the value changed, not the value itself.
Kind | Block | Q is TRUE when |
|---|---|---|
Rising | | it was FALSE last scan and is TRUE now |
Falling | | it was TRUE last scan and is FALSE now |
Q stays TRUE for exactly one scan, which is why this pattern is called a one-shot.
Using it
The block has memory, so declare one instance in VAR per signal you watch:
VAR
StartEdge : R_TRIG;
StopEdge : F_TRIG;
END_VAR
StartEdge(CLK := Start);
IF StartEdge.Q THEN
Starts := Starts + 1; (* once per press *)
END_IF;Never share one instance between two signals. An instance stores the previous value of one signal.
What is inside
There is no magic in it:
Q := CLK AND NOT Last;
Last := CLK;You can write that by hand with a single BOOL, but the standard block reads clearer in review and travels between vendors.
When you actually need an edge
- Counting: batches, parts, number of starts.
- One step of a sequence: move state on the press, not for the whole press.
- Acknowledge and reset: the ack button clears the alarm once.
- Logging an event: writing a record or sending a message. Without an edge you send 50 messages a second.
- Falling edge: start a run-on delay when the pump stops, or log the run duration when it ends.
The rule
A level asks "is it on now?"; an edge asks "did it just start?" Anything that counts, logs, or advances a step needs the second question.
Try it in the lab
The lab runs in your browser: an ST editor, a virtual process that suits the lesson, the same program as a live ladder diagram, a watch table, and export to Siemens SCL.
What to try
- Run the program and give PB1 a short press: L1 lights; press again: it goes dark.
- Compare Presses with Scans in the watch table: one goes up by 1, the other by tens for a single press.
- Notice L3 flash the moment you let go.
- Replace Rise.Q with PB1 in the L1 line and try it: the lamp flickers for as long as the button is held.
Terms in this lesson
People find this lesson by searching
R_TRIG structured textF_TRIG plcrising edge detection plcone shot plc logiccounter increments every scanedge detect structured textfalling edge plc
Course outline
- 01What is Structured Text, and when to use it instead of ladder
- 02How a PLC runs your code: the scan cycle and the process image
- 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
- 04Your first program: starting a pump with seal-in logic
- 05Data types: BOOL, INT, REAL, TIME and the rest
- 06Declaring variables: VAR, VAR_INPUT and RETAIN
- 07Literals and constants: 16#FF, 2#1010 and T#5s
- 08Type conversion and scaling a 4-20 mA signal
- 09Assignment and arithmetic in ST: := and operator precedence
- 10Boolean logic, comparisons and bit operations
- 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
- 12IF, ELSIF and ELSE: the rules and when to avoid them
- 13The CASE statement and your first state machine
- 14The FOR loop and the watchdog
- 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
- 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
- 17Timers: TON, TOF and TP
- 18Counters: CTU, CTD and CTUD
- 19SR and RS flip-flops: which one wins
- 20Writing your own function
- 21Writing a function block
- 22Arrays in Structured Text
- 23Structs and enumerations
- 24References and pointers
- 25The state machine pattern: a batch sequence that cannot get stuck
- 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
- 27Analogue signal processing: counts to engineering units, NE43 and filtering
- 28Closed-loop control in ST: hysteresis first, then a practical PID
- 29Motors and valves in practice: permissives, feedback and travel time
- 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
- 31Testing and debugging ST: the watch table, forcing and simulation
- 32An ST style guide: naming, comments, constants and version control
- 33Final project: a complete tank station with a state machine and alarms
- 3420 interview questions on ST and PLC programming, with answers