Explainer
Why Modicon M340 function block variables do not retain values
Short answer
An integer declared inside a Unity Pro derived function block on a Modicon M340 restarts at its initial value after a power cycle, because the CPU does not give block-private data the retained property. Declare the value as a retained variable at program level, pass it into the block, and write each update back so the value survives every restart.

On this page
- What happens to an integer inside a Unity Pro function block when the M340 powers off?
- How do warm start and cold start treat M340 data?
- How do I make a function block value survive a power cycle?
- Which values should I retain and which can reset?
- What does the fix change with real numbers?
- What happens to retained values when I download a new application?
- How do I prove retention works before trusting it?
- Why does my value still reset after I made it retained?
- Questions asked
An integer you declare inside a Unity Pro derived function block does not keep its value through a power cycle on a Modicon M340: the block restarts at its initial value because the CPU gives the retained property to program-level variables, not to data private to a block instance. The fix is to move every value the process must remember into a retained variable outside the block, pass it in on every call, and write each update back to it. This page explains why the platform behaves that way, what warm and cold starts do to your data, what a download adds to the risk, and how to prove a value survives before you trust it.
What happens to an integer inside a Unity Pro function block when the M340 powers off? #
It comes back at its initial value: on an M340 CPU, a derived function block's private integers do not carry the retained property, so any counter, accumulator or stored setpoint declared inside the block resets when power is removed and restored.
A derived function block in Unity Pro is a type: every place the program calls it, the CPU keeps a separate instance with its own private data. On this platform the retained property belongs to variables you declare at program level, and data an instance owns cannot receive it. IEC 61131-3, the standard the PLC languages follow, defines RETAIN as a qualifier that marks a variable to keep its value across a power failure and restart; Unity Pro on the M340 applies that idea at program level, so the block's logic reloads fine while its runtime numbers go back to the declared initials. The program is never what you lose. You lose the runtime data the instance was holding.
The symptom is easy to reproduce and easy to misread: run the block, watch the internal integer count up, switch the cabinet off, power back up, and the integer is back at its declared initial value while everything around it looks normal. That single observation is the whole diagnosis: nothing is corrupted and nothing was lost to noise. The platform simply never promised that storage.
How do warm start and cold start treat M340 data? #
A warm start restarts the CPU without the power being turned off, and the M340 resumes processing with its retentive data intact; a cold start reinitializes data, and the parameter named 'Initialize %MWi reset on cold start' decides whether %MWi are reset when one happens.
A warm start keeps the values you marked retained, so they are sitting there when the first scan runs. Use it when you want a controlled recovery without touching stored values.
A cold start is the reinitializing restart, and one project setting decides part of its reach: the PLC configuration carries a parameter named 'Initialize %MWi reset on cold start', and selecting it resets %MWi, the CPU's internal word variables, on a cold start. Two M340 projects can therefore power up differently from the same event, so after a real power failure, test what your machine actually does rather than assuming the warm-start behavior carried your data. That test is the power-cycle procedure further down.
Retention is also only half the guarantee, because logic that runs at startup can overwrite a kept value on the first pass. That is exactly how a retained serial counter still resets on an S7-1500 warm restart, as described in S7-1500 warm restart resets a retained serial counter. The storage decision and the startup discipline have to be right together.
How do I make a function block value survive a power cycle? #
Declare the value as a retained variable at program level, outside the block, pass it into the block as an input, and write the block's updated result back to the same retained variable.
The pattern works for any value type and for every derived function block in the project, and it looks like this:
- List the values the process must remember through a restart: totals, counts, operator setpoints, last recipe or phase indices.
- Declare one retained variable per value at program level, outside every block, and set the retained property on the declaration itself.
- Give the derived function block an input for that value, and an output as well wherever the block changes it.
- Connect both directions at each call: the retained variable feeds the input, and the block's updated result is assigned back to the same retained variable.
- Gate any initialization assignment so it runs only on first commissioning or an explicit reset command, never unconditionally at every power-up.
The write-back is the step that gets skipped, and skipping it fails quietly: the block keeps counting its private copy while the retained storage freezes at the last explicit write, so what comes back after a power cycle is stale rather than obviously broken. Check each call site for both directions before the first download, and match the types at the interface so nothing is altered on the way in or out.
Which values should I retain and which can reset? #
Retain a value when the process depends on its history: run-hours, production totals, batch counts, operator-entered setpoints. Let reset anything the first pass can rebuild from inputs and cycle events.
- Retain: run-hours and similar equipment history, production and batch counts, operator-entered setpoints, recipe indices, anything an audit trail or a maintenance decision depends on.
- Let reset: intermediate calculations that live inside one scan or one cycle, per-part states that end when the part leaves the machine, alarm latches that have their own reset path. If the first pass can rebuild it from inputs, it should reset.
Retaining a setpoint also changes what the machine restarts with: the last operator's value comes back, not the project default, so the sequence that consumes that setpoint must check it against the project's limits on the first pass before the block or an actuator sees it. Retention is a startup dependency, not just storage. Every retained value is a value you are promising to handle correctly at power-up.
What does the fix change with real numbers? #
A compressor run-hour meter accumulated inside a derived function block loses all 1,250 hours at one power cycle; the same count as a retained external variable loses zero hours and continues from 1,251 after the next hour of running.
The setup is my example, not a vendor configuration. A compressor's run-hour meter lives in a derived function block that holds two private integers; my names RUN_HOURS and RUN_MINUTES are illustrative, not platform keywords, and both declare an initial value of 0. The logic adds 1 to RUN_MINUTES for every minute the compressor's run contact is closed, and when RUN_MINUTES reaches 60 it subtracts 60 and adds 1 hour to RUN_HOURS.
Declared inside the block, RUN_HOURS is not retained. After two years of duty the meter reads 1,250 hours and RUN_MINUTES reads 37. A drive replacement then takes the PLC down for an afternoon, and on restart the instance is rebuilt from its declared initial values, so RUN_HOURS reads 0 hours. The plant has lost 1,250 - 0 = 1,250 hours of compressor history, and the 37 minutes in the minutes counter go with it.
With COMP_HOURS, a retained integer declared at program level, the same count survives. The block receives COMP_HOURS as its input on the first call, and at each hour rollover it writes COMP_HOURS + 1 back to COMP_HOURS. At power-off COMP_HOURS holds 1,250 hours; on restart the first call reads it back unchanged, and after the next complete hour of running the write-back stores 1,250 + 1 = 1,251 hours. Lost history across that power cycle: 0 hours.
If billing or condition monitoring needs the minutes fraction too, give RUN_MINUTES the same treatment; otherwise accept that every power cycle rounds the meter down to the whole hour. Decision, per value: history the process must keep goes outside the block as a retained variable passed in both directions, and anything you can afford to lose stays private and resets by design.
What happens to retained values when I download a new application? #
A full application download can hand the PLC fresh data, so before the transfer connect Unity Pro to the PLC and run the command named 'Save Data from PLC' to save the current values, then bring them back after the new application is in.
The transfer delivers the project's data to the PLC, so a value the PLC holds but the project does not reflect is exposed at every full download. The documented remedy for exactly this case, keeping a derived function block's configuration parameters through an application download, a PID block's tuning among them, is to save before you transfer. The save step runs while Unity Pro is connected to the PLC, from its PLC menu, and it captures the PLC's current values so they can be brought back once the new application is running.
Two habits make this routine instead of a recovery: record the PLC's current values before any full transfer, and verify online afterward that the restored values match what you recorded. Treat every full download as a restart event for data, even though the CPU itself never lost power.
How do I prove retention works before trusting it? #
Write a recognizable value into the retained variable online, for example 4,321, power the PLC down and up under your site's isolation procedure, then read the variable back before the first scan finishes; if it still holds 4,321, retention works.
- With the project online, put a recognizable value into the retained variable, for this test 4,321, through the program's normal path or a direct online write, and record it.
- Take the PLC's power down and back up using your site's approved isolation procedure for the cabinet; that step is electrical, not a software restart command.
- Read the retained variable back before any startup logic runs and confirm it still holds 4,321.
- Watch the block's first calls and confirm it receives that value from its input, not from an overwritten copy.
- Repeat across the restart the machine will really see, a true power failure with the project's cold-start configuration, not only the warm restart from the programming tool.
A pass looks like this: the readback equals the recorded value, the block's working copy matches it on the first call, and one more hour of running moves the meter by exactly one increment. Anything else puts the fault in one of three places, and the next section walks them in order.
Why does my value still reset after I made it retained? #
Three causes cover nearly every case: the variable never received the retained property, the restart was a cold start where the project's %MWi reset parameter applies, or startup logic writes the initial value over the kept value on the first pass.
Cause one: the retained property never landed on the variable the program actually uses. A project often accumulates two same-named integers, one retained at program level and one still private inside the block, and the call site passes the private one. Open the call, look at which variable feeds the input, and confirm the retained property sits on that declaration.
Cause two: the restart was not the one you tested. A warm start keeps retentive data; a cold start runs the project's reset behavior, including the 'Initialize %MWi reset on cold start' parameter from the PLC configuration. If the panel breaker, not the programming tool, is what restarts the machine, test across the breaker.
Cause three: an initialization assignment overwrites the kept value on the first pass. Find every write to the variable, gate the commissioning writes behind a first-commissioning or explicit-reset condition, and let the block read the stored value before anything writes it. The order of the first pass decides what survives: what the PLC scan cycle does in each pass shows why an early write lands first, and why a safety program should copy standard data once applies the same copy-once discipline to shared data.
That leaves the decision for the whole project, and it is short. Anything the process must remember lives in a retained variable at program level and passes through each block interface in both directions; anything the first pass can rebuild stays private and resets by design; and every full download gets the save-and-restore treatment around the transfer. Run those three rules and a power cycle costs you 0 hours, 0 counts and 0 setpoints.
Questions asked
Does the retained-variable pattern apply to BOOL and REAL values too, or only integers?
It applies to any value the block must keep. A latched fault history flag, a REAL process total or an integer run-hour count all belong in retained variables at program level, passed through the block interface in both directions. Data private to the block instance restarts at its initial value whatever its type, so the type never decides; the retained declaration does.
Where does a retained value physically live on the M340?
Retained data is what the CPU restores at startup, while the application can be held in flash memory, which keeps its content with no power applied at all. That split explains the symptom: after a long power-off the program and its declared initial values come back intact, while block-private runtime values return to those initials because nothing retained them.
Is an online change handled differently from a full download?
Yes. An online change transfers only your edit and leaves the PLC's current data in place, so retained values survive it untouched. A full application download replaces application data and can hand the PLC fresh values, which is why the current values are saved from the PLC first and restored after the transfer. Treat every full download as a restart risk and run the save-and-restore sequence around it.
How many variables can I mark retained on one M340 CPU?
The retained region is a fixed part of the CPU's memory, and its size for your exact CPU reference belongs to that controller's specification, which this pattern does not depend on. Keep retained only the values the process truly needs, such as totals, counts and setpoints, and let startup logic rebuild everything else. If a project ever approaches the limit, moving long-term totals to a data logger is the cleaner answer.


