Explainer
Why a TON instance persists between PLC scans in CODESYS
Short answer
A function block instance declared in a program's VAR block is allocated once, when the application starts, and every cyclic call reuses the same stored state. A 1000 ms TON therefore runs to completion inside a 20 ms task: no scan rebuilds it, no thread waits on it, and the task merely polls the stored elapsed time each pass.

On this page
- Why does a 1000 ms TON run to completion inside a 20 ms task?
- When is a function block instance created, and how long does it live?
- Why doesn't each timer need its own thread?
- How many scans does a timer preset span, and when does the logic react?
- Where should the timer instance be declared, and where should it not?
- Does the timer state survive a restart or a new download?
- How do you verify the instance is the one being watched?
- Questions asked
The timer keeps counting because nothing in the scan re-creates it. A function block instance declared in a program's VAR block is a variable like any other: the runtime allocates it once, when the application starts, and every cyclic call reuses that same storage. Your TON is not rebuilt each scan; each call reads its inputs, updates the stored state and rewrites its outputs. That is why the online view shows elapsed time marching toward the preset while Main executes dozens of times in between, and why no thread per timer exists — the task polls each instance in program order and moves on.
Why does a 1000 ms TON run to completion inside a 20 ms task? #
Because the runtime never re-creates it: a function block instance declared in a program's VAR block is allocated once, when the application starts, and every cyclic call reuses that same storage while only refreshing inputs and outputs. The instance's internal memory — its start reference, its accumulated elapsed time, its Q state — simply sits there between calls.
Each pass of the task follows the sequence described in what the PLC scan cycle does in each pass: read inputs, execute the program in order, write outputs. During the execute step your rung calls the TON once; the call compares the real time since the enable edge with the preset, updates the instance, and returns. Nothing in that sequence says "create a fresh timer," so nothing does.
An on-delay could not work any other way. A stateless function derives its result entirely from the arguments of the current call; a timer must remember that timing began and when. If each scan built a blank TON, the elapsed value would restart at zero fifty times before the preset was ever reached, and Q would never assert.
When is a function block instance created, and how long does it live? #
It is created when the runtime starts the application, and it lives exactly as long as the object that declares it — the program instance for a declaration in Main's VAR block. In IEC 61131-3 terms, an instance is a variable whose type is a function block, and the same lifetime rule that applies to your BOOLs and INTs applies to it. The declaration is the allocation; the call is only use.
The one place the rule changes is inside a method. Variables in a method's local VAR block exist only for the duration of that call — they live on the stack — so a TON declared there would get fresh storage every time the method runs. You would not declare a stateful timer there; you would pass in a reference or pointer to an instance that lives somewhere longer-lived.
Why doesn't each timer need its own thread? #
It doesn't: the task that runs Main calls each timer instance in program order, in the same execution context, and each call returns as soon as it has updated the stored state. The PLC works by polling. A TON call never sleeps for the preset and never blocks the program; it checks whether the elapsed time has reached the preset during the current scan and reacts if it has.
Two timers therefore need zero extra threads, even on a single-core controller: the task calls the first instance, then the second, in the same pass. Because one execution context touches the instance, you also get sequential behavior for free — the instance cannot be mid-update somewhere else while your logic reads it.
How many scans does a timer preset span, and when does the logic react? #
A 1000 ms preset inside a 20 ms task takes 50 calls to complete: elapsed time accrues in real time between calls, and the completion condition is evaluated only when a scan calls the instance. The count is plain division:
1000 ms ÷ 20 ms = 50 scans.
Worked example, with the enable edge landing just after scan 100 has read its inputs. At every subsequent call the runtime compares real elapsed time since the edge with the preset; the first call at or past the preset is scan 150, and that is where Q turns true. Logic placed after the call in that same scan sees Q true immediately. Because the edge lands somewhere within an interval, the worst-case reaction is one interval late: 50 × 20 ms, plus up to 20 ms of edge offset, so at most 1020 ms after the input physically rises.
Your design decision follows from that arithmetic: if downstream logic must react within a bounded window, budget one task interval of latency on top of the preset, and keep the reacting code after the call in scan order so it uses the value produced in the current scan.
Where should the timer instance be declared, and where should it not? #
Declare it in the VAR block of the program that calls it, because that storage lives with the program instance for the application's whole run — and never in a method's local VAR block, whose storage dies with the call. The choices line up like this:
| Declaration | Storage lifetime | Fit for a TON |
|---|---|---|
| Program VAR block | As long as the program instance | Yes — the standard home for a timer called by that program |
| Method-local VAR | One method call only | No — fresh storage every call, no scan-to-scan memory |
| Reference or pointer parameter into a method | The referenced instance's own lifetime | Yes — when the method must act on an instance owned elsewhere |
| Global variable list | Application lifetime | Yes — when several programs share one timer |
The ownership question is not unique to this platform: the same class of fault appears in this write-up of an FAL instruction failing inside an AOI, where an instruction nested in another block misbehaves because its operands do not live where it executes.
Two more rules keep the instance honest. Call it once per scan on the intended path — a conditional branch that skips the call freezes Q at its last value, and any input change during the skip is invisible. And give each independent timing job its own instance, because two conditions sharing one TON share its internal state and turn scan order into an unintended dependency.
Does the timer state survive a restart or a new download? #
No: retention between scans is automatic, but retention across a download, a reset, a warm restart or a power cycle depends on the variable's qualifier and the runtime's retain handling, and a plain declaration comes back initialized. After any of those events a normal TON instance has elapsed time zero and Q false, and your sequence begins again at the next enable edge.
If the process genuinely must resume timing through a restart, the declaration itself has to carry the retain and persistent qualifiers — a block of the form VAR PERSISTENT RETAIN in front of the variable — and you test that lifecycle event deliberately rather than assuming it. Persistence across scans buys you nothing across a power cycle.
How do you verify the instance is the one being watched? #
Watch the instance itself, not a summary tag: put the exact declared name, its IN value and its Q into the online view, then hold the input true and confirm the elapsed value advances across successive scans. Confirm Q asserts on the first call at or past the preset, then drop IN false through the normal program path and confirm the elapsed value returns to zero and Q drops. If Q misbehaves specifically around the reset, the separate failure where the output sticks is covered in why a CODESYS TON output stays on after its rung goes false.
If the instance behaves in one language and not the other, the problem is the call, not the lifetime — see when a CODESYS TON works in structured text but not ladder. Before trusting any single observation, search the project for every use of the instance name, because a second call with a false IN resets the timer no matter what the first call did.
Questions asked
What happens if a conditional branch skips the TON call for a few scans?
The instance keeps whatever state the last call wrote: Q and elapsed time stay frozen at their last values, and input transitions during the skipped scans are never evaluated. An IN that drops false and returns true between two calls is invisible, so the timer continues as if it never dropped. If the enable must not be missed, call the timer unconditionally and gate the logic that uses Q instead.
Can I copy one TON instance into another with a plain assignment?
Yes, when both are the same function block type: the assignment copies the whole internal state, so the target continues with the source's elapsed time and Q value. After the copy the two are independent. Use it deliberately, for example to snapshot a timer, and never as a way to share timing, because two conditions calling one shared instance is what actually shares state.
Why does my correctly declared timer still reset every scan?
Almost always because IN is dropping out briefly: a false IN resets an on-delay's elapsed time even when the false state lasts less than one refresh of the online view, which updates far slower than the task. Latch IN with a self-holding bit, or record it into an array each scan, and you will usually find a one-scan false caused by scan order, with the condition written later in the program than the timer call that reads it.
Does a TON in a global variable list behave differently from one in Main?
No. A global instance is allocated once when the application starts, exactly like a program-local one, and it keeps its state between cyclic calls the same way. The differences are visibility and ownership: every program can call it, so the discipline of one call site per scan and one timing job per instance matters more, since a stray call anywhere in the project can reset it.


