17Standard function blocks
Timers: TON, TOF and TP
The three standard IEC 61131-3 timers, what each one does to the clock, and using them for alarm de-bounce and a run-on fan.
Three blocks, four pins
The standard gives you three timers and they share the same interface:
Pin | Type | Meaning |
|---|---|---|
| BOOL | the start signal |
| TIME | preset time |
| BOOL | the output |
| TIME | elapsed time |
The whole difference is when Q goes true and when it drops.
TON · on-delay
Q goes TRUE after IN has stayed true for the whole of PT, and drops immediately when IN drops. Any break in IN before the time is up resets ET to zero and the count starts again.
IN ___----------______----___
ET ___/\/\/\/\/\__0___/\__0__
Q _______-------______________ (PT = 3s)TOF · off-delay
Q goes TRUE immediately when IN rises, and stays TRUE for PT after IN drops. The timer counts after the signal goes away.
IN ___-------_____________
Q ___-------------________ (PT = 5s after the fall)TP · pulse
Q goes TRUE on the rise of IN and stays true for exactly PT, however long IN stays on. Holding the button for ten minutes does not stretch the pulse.
IN ___-----------------____
Q ___------_______________ (PT = 500ms)Calling one
VAR
AlmDly : TON;
END_VAR
AlmDly(IN := LSH, PT := T#3s);
HiAlm := AlmDly.Q;The call must run every scan. Put it inside an IF that is sometimes skipped and the timer freezes mid-count. Keep the call at the top level and control it through IN.
Use one: alarm de-bounce
The alarm standard ISA-18.2 recommends an on-delay on every process alarm. A level hovering at 90% produces 40 alarms a minute, floods the operator, and the whole screen gets ignored. Three seconds turns that into one real alarm:
AlmDly(IN := Level > 90.0, PT := T#3s);
HiAlm := AlmDly.Q;Add a deadband on the return and the chatter is gone for good.
Use two: a run-on fan
A hot motor needs cooling after it stops, so the fan keeps turning for five minutes. That is literally a TOF:
RunOn(IN := Pump, PT := T#5s);
Valve := RunOn.Q;The same shape covers a lamp that stays lit after the event and a valve that closes late.
Writing time
T#500ms, T#3s, T#2m30s, T#1h. Never count your own scans into a REAL: a timer measures real time and stays correct when the scan time changes.
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 turn S1 on: L1 lights after 2 s while OnDelay.ET counts. Turn it off before 2 s: it never lights.
- Turn S2 on, then off: L2 stays lit for 3 s after you switch off.
- Give PB1 a short press, then a long one: the pulse is 1.5 s both times.
- In the ladder watch the timer boxes and the live ET inside them.
Terms in this lesson
People find this lesson by searching
TON TOF TP structured textplc timer on delayoff delay timer plcTON PT ET Qpulse timer plcalarm on delay ISA 18.2structured text timer examplerun on timer motor fan
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