S7-1200 Retain Memory After Power Off: DB and Merker Setup in TIA Portal
When an S7-1200 CPU such as the 6ES7214-1AG31-0XB0 (CPU 1214C DC/DC/DC) is de-energized, all data blocks and bit-memory areas default to their initial values unless they are explicitly declared retentive. This is one of the most common commissioning issues in TIA Portal projects: an operator's count, recipe parameter, hours-run tally, or state flag resets to zero on the next morning's power-up and the machine refuses to start in the correct mode. The fix is to enable the Retain attribute on individual data block tags and to define a contiguous retentive Merker range that is mirrored from the load memory on warm restart. This guide walks through the exact configuration procedure, the memory budget on the 1214C, firmware-specific behavior, and the supporting HMI text formatting (SHIFT+ENTER) required to label those retained values on a panel.
1. Problem Statement and Field Symptoms
After a clean power removal (terminal L1/N off, or a battery-backed stop when no MC/SD card is fitted) the following behavior is observed on a default S7-1200 program:
- All global DBs return to the values declared in the offline project. If a DB was downloaded with Initial value = 0, every tag starts at 0 again.
- All Merker (M) bytes M 0.0 through the last declared address become 0.
- Process image inputs/outputs are refreshed; outputs drop to their configured substitute or 0 behavior.
- On the next RUN transition, the OB1 sweep re-initializes non-retentive variables, so counters, totalizers, last-batch numbers, and recipe indices are lost.
This behavior is correct and is documented in the S7-1200 Programmable Controller System Manual (entry ID 109751826). Retentive storage is opt-in because retained data consumes the limited non-volatile area in the CPU; the more you retain, the larger the save/restore time on warm restart and the greater the wear on the internal flash.
2. Prerequisites
Before changing the configuration, confirm the following items are in place:
-
CPU identification. The hardware in question is a CPU 1214C with order number
6ES7214-1AG31-0XB0. The block identifier breaks down as6ES7(Siemens AG) +214(CPU 1214) +1AG31(DC/DC/DC, 14 inputs / 10 outputs, 24 V sensor supply, 24 VDC output) +0XB0(firmware release). The 1214C is also available as DC/DC/Relay (6ES7214-1BG31-0XB0) and AC/DC/Relay (6ES7214-1HG31-0XB0); all three share the same retain behavior. - Firmware version. Verify the firmware in TIA Portal via Online > Accessible devices > Online & diagnostics > Diagnostics > General. Firmware 4.x is required for some 1200/1500 cross-project features; the retain semantics described here apply to firmware 3.0 and 4.x identically. If the firmware is older than 2.0, upgrade using the SIMATIC S7-1200 CPU Firmware Update Procedure.
- TIA Portal project. A project opened in STEP 7 Basic V16 or later (V17, V18, V19 are also supported for the 1214C AG31). Earlier versions (V13 SP1, V14, V15, V15.1) are backward compatible with the AG31 CPU but will not see newer online diagnostics.
- Online access. Ethernet, PROFIBUS, or a configured routing path to the CPU; the user must have at minimum Read rights to perform a consistency check after the change.
3. How Retain Memory Works on the S7-1200
The S7-1200 stores retentive data in a dedicated area backed by the CPU's non-volatile memory. On a STOP-to-RUN transition (warm restart) the runtime reads the retentive image from the load memory and restores the corresponding DB tags and Merker bytes. On a power-off/power-on sequence the same restore action takes place. Retentive tags are preserved across:
- Power-off / power-on cycles (cold restart from Mains off).
- STOP → RUN transitions.
- MRES (memory reset) only if the Retentive data after MRES option is set in the device configuration; by default MRES wipes retentive memory too.
Retentive memory is not preserved across a factory reset performed with the mode selector, nor across a download of a new program unless the new program explicitly declares the same tags retentive with compatible types.
3.1 Memory Budget for CPU 1214C 6ES7214-1AG31-0XB0
The following budgets apply to the standard 1214C DC/DC/DC variant. Other 1214C order numbers share the same limits.
| Resource | 1214C (FW 4.x) | 1214C (FW 3.0) |
|---|---|---|
| Work memory (program) | 100 KB | 50 KB |
| Work memory (data) | 150 KB | 75 KB |
| Load memory (integrated) | 4 MB | 2 MB |
| Bit memory (M) area | 8 KB (M 0.0 .. M 1023.7) | 8 KB |
| S7-1500-style DB retention (non-optimized) configurable | 10 KB | 10 KB |
| Retain area (DB + M combined) | 10 KB max | 10 KB max |
Source: S7-1200 System Manual, Edition 04/2017 (and supplements). The 10 KB figure is the total payload of retentive data; if you set 5 KB of Merker retentive, only 5 KB of DB retentive data can be declared, and vice versa.
4. Configuring Retain on a Data Block (DB)
Individual DB tags can be flagged retentive, regardless of whether the DB is optimized (symbolic only) or non-optimized (absolute access enabled). The procedure differs slightly between the two block types.
4.1 Non-Optimized (Classic) Data Block
- In the project tree, expand Program blocks and double-click the data block (e.g.,
DB_Recipe). - If the block is not yet open in offline view, click Open.
- Select all the rows that must persist (or click the header to select all), then place a check mark in the Retain column. The Retain column appears as a tick box.
- Save the block with Ctrl+S and acknowledge the Definition will change; consistency is re-established after download warning.
- Download to the device: right-click the CPU in the project tree → Download to device > Software (all).
4.2 Optimized Data Block (Symbolic-Only)
Optimized blocks (created with the Optimized block access option in the DB properties) hide the Retain tick box on the tag table. Instead, retain behavior is set per tag through the Snapshot of the actual values in the monitoring table or, more commonly, via a single setting on the entire DB:
- Right-click the optimized DB in the project tree → Properties > Attributes.
- Under Retain, choose one of the following values: Non-retain, Retain, or Set in IDB. For full persistence choose Retain; if you want per-tag control inside an optimized block, choose Set in IDB and then open the DB to find the Retain column enabled again.
- For Set in IDB mode, open the DB and tick the Retain column for each tag that should persist. Untagged tags revert to their initial value on power-up.
4.3 Verification by Power-Cycle Test
- Download the modified block to the CPU.
- Place the CPU in RUN. From an HMI or watch table, set a known value into a retentive tag (e.g., write
12345to"DB_Recipe".BatchCounter). - Toggle the mode selector to STOP, wait 5 seconds, then power the chassis off.
- Wait 10 seconds, restore mains power, observe the CPU go to RUN automatically.
- Open the watch table and confirm the tag still shows
12345. A non-retentive tag would display its start value.
5. Configuring the Merker (M) Retain Range
Unlike DBs, the Merker area is configured as a single contiguous range beginning at MB 0. The range is defined in the device configuration, not on a per-tag basis.
- Open the device view of the CPU (double-click Device > PLC_1 > CPU_1).
- In the inspector window select Properties > System and clock memory.
- Look for the Retentive memory section. Two fields are shown: Number of memory bytes starting with MB0 and Number of retentive DB words.
- Enter the number of Merker bytes that must be retained. For example, entering
100retains M 0.0 .. M 99.7. The remaining 924 bytes (M 100.0 .. M 1023.7) are non-retentive. - Set the number of retentive DB words. Each DB word = 2 bytes; 5 KB of retentive DB data corresponds to 2560 words.
- Compile the hardware (Hardware > Compile) and download to the device.
5.1 Retentive Merker Example
A common pattern is to reserve the first 32 Merker bytes for handshakes and the next 64 bytes for user counters. The following FB snippet is a self-contained example for a firmware-4.x 1214C. Place the FB in OB1 and monitor M 200.0 .. M 207.7 across a power cycle.
FUNCTION_BLOCK FB_RetainDemo
VAR
RunSeconds : INT; // retentive counter
LastValue : REAL; // retentive last batch weight
END_VAR
BEGIN
// Increment on rising edge of 1 Hz clock bit (Clock_1Hz from CPU)
IF "Clock_1Hz" THEN
"RunSeconds" := "RunSeconds" + 1;
END_IF;
// Copy last good weight into a retained location
IF "WeightValid" THEN
"LastValue" := "WeightInKg";
END_IF;
END_FUNCTION_BLOCK
Declare RunSeconds and LastValue in a retentive DB and ensure the DB is configured for retain. The values will survive a power cycle; they will not survive an MRES unless the Retentive data after MRES option is enabled in the CPU properties.
6. STEP 7 Basic / TIA Portal Configuration Reference
| Setting | Location in TIA Portal | Default | Recommended Value |
|---|---|---|---|
| Number of retentive Merker bytes | Device view → Properties → System and clock memory → Retentive memory | 0 | 8 .. 256 (project dependent) |
| Number of retentive DB words | Same screen | 0 | 0 .. 2560 (max 5120 bytes) |
| DB Retain attribute (non-optimized) | DB editor → tag table → Retain column | unticked | Tick for each tag to be retained |
| DB Retain attribute (optimized) | DB → Properties → Attributes → Retain = Retain or Set in IDB | Non-retain | Set in IDB for per-tag control |
| Retain data after MRES | Device view → Properties → System and clock memory → Retentive memory → Additional | Disabled | Enabled (only if MRES must preserve data) |
| Startup after power on | Device view → Properties > Startup | Warm restart - RUN | Warm restart - RUN |
For deeper information, see the S7-1200 Programmable Controller System Manual chapter on memory areas, retentive data, and diagnostics.
7. HMI Button Text: Splitting a Label into Two Rows
A secondary symptom in the same field case is the inability to break a button caption into two lines on a SIMATIC Comfort Panel or Basic Panel HMI tag. The Enter key inserts a new event but does not insert a line break inside the text field. The HMI runtime expects the explicit line break character sequence used by Windows controls.
- Click into the Text property of the button in the HMI project tree (or in the screen editor's Properties > General > Text).
- Move the text cursor to the position where the line should break.
- Press Shift + Enter. A visible carriage return and line feed are inserted. The text now occupies two visual lines when displayed on the panel.
- Compile the HMI project and download to the panel.
7.1 Programmatic Equivalent in a Multilingual Text List
If the text is supplied through a text list (useful for translation), the line-break characters can be inserted in the source string with a literal line feed. In TIA Portal text lists, the escape sequence is the actual CR+LF pair, e.g.:
Editace rádku\r\n - dávka 1
Editace rádku\r\n - dávka 2
The runtime on Comfort Panels renders both Shift+Enter inserts and explicit CR+LF in text lists identically. WinCC Unified expects a Unicode line separator (U+2028) or the CRLF pair; verify the panel image (Comfort vs. Unified) before relying on either sequence.
8. Verification Procedure
Use the following checklist after every retain-memory modification to confirm the configuration is correct end to end.
- Project compile. Right-click the CPU → Compile → Hardware and software (rebuild all). Resolve any warnings about retain memory exceeding the 10 KB budget.
- Offline / online comparison. From the project tree, right-click the CPU → Compare > Offline/online. The retain memory section must match.
- Diagnostic buffer. Open Online & diagnostics → Diagnostics > Diagnostic buffer. Verify the most recent warm-restart entries have an event ID consistent with a normal restart, not a memory reset.
- Watch table power-cycle test. As in Section 4.3, set a sentinel value, remove power for 15 seconds, restore, and read the value back.
- Snapshot test. Modify the value, perform Online > Snapshot of the actual values on the watch table, then download to the CPU so that the new value becomes the new start value. This proves the path between runtime and load memory is healthy.
- HMI confirmation. Operate the button on the HMI to confirm the two-line label displays correctly. Repeat on the runtime simulation if the panel is offline.
9. Common Pitfalls and Edge Cases
| Symptom | Likely Cause | Remedy |
|---|---|---|
| All Merkers still zero after power-up | Retentive Merker count = 0 in the CPU properties | Increase the count in System and clock memory and recompile / download |
| Some DB tags persist, others do not | Optimized DB with Retain = Non-retain; only individual tags inside Set in IDB mode can be marked | Change DB attribute to Set in IDB and tick the tags |
| Compile error "Retain memory exceeds limit" | Combined retain payload > 10 KB | Reduce either Merker range or DB retain count, or split the recipe into a non-retentive DB plus a small retentive index DB |
| Value persists across STOP → RUN but not across power cycle | MRES performed in between, or Retain = Non-retain in the offline project | Check the DB attribute; never run MRES on a running system if persistence is required |
| HMI label still on one line | Enter key used instead of Shift+Enter, or text list CRLF ignored on Unified panel | Use Shift+Enter in the static text; for Unified, use U+2028 |
| Download fails with "Firmware mismatch" | The TIA Portal project is newer than the installed firmware | Match the TIA Portal version to the firmware, or perform a firmware update via the S7-1200 firmware update tool |
10. Firmware-Specific Notes
While the retain semantics are stable across firmware 2.0 .. 4.x for the 6ES7214-1AG31-0XB0, the following differences are worth highlighting:
- Firmware 4.2 and later add Web server access to the diagnostic buffer and a JSON API to read/write retainable DB tags from a browser. This is purely an additional channel; it does not change the persist behavior.
- Firmware 4.4 introduces the ProDiag overview but does not modify retain limits.
- Firmware 3.0 introduced the Optimized block access attribute and the corresponding Set in IDB retain mode. If a project must run on a 2.x firmware CPU, the optimized block must be recompiled as a non-optimized block before download.
- For S7-1200 firmware 2.0 the Retain tick box on the tag table is hidden; use the DB property Non-retain / Retain instead.
11. Specifications and Limits Summary
| Parameter | Limit / Value | Source |
|---|---|---|
| CPU order number | 6ES7214-1AG31-0XB0 | Siemens catalog |
| Bit memory size | 8192 bits / 8 KB / 1024 bytes | System Manual |
| Retain memory (combined DB + M) | 10 KB | System Manual, Chapter 4.2 |
| Max number of DBs | 1024 (address range 0 .. 32767) | System Manual |
| DB retain attribute scope | Per tag (optimized / non-optimized) or per DB (optimized) | TIA Portal Help |
| Min project TIA Portal version | V13 SP1 for AG31 | Siemens Compatibility Tool |
| Line break in HMI text | Shift+Enter; U+2028 on Unified | WinCC Comfort / Unified Manual |
12. Frequently Asked Questions
How do I keep DB values after a power-off on CPU 1214C 6ES7214-1AG31-0XB0?
Open the DB in TIA Portal, mark the Retain column for each tag (non-optimized block) or set the DB property to Retain / Set in IDB (optimized block), compile the project, and download it to the CPU. The retained tags are mirrored from the load memory on every warm restart.
How many Merker bytes can I make retentive?
The total budget is 10 KB shared with the DB retain area. Set the value in Device > Properties > System and clock memory > Retentive memory > Number of memory bytes starting with MB0. The retentive range always starts at MB 0 and is contiguous.
Will a STOP-RUN transition reset non-retentive Merkers?
Yes. Only the configured retentive range and the retentive DB tags are restored. The remaining Merkers and tags are re-initialized to their start values on every STOP-to-RUN transition.
How do I split a button label into two lines on a SIMATIC panel?
Place the cursor inside the text field and press Shift + Enter. On Unified Panels use the Unicode line separator (U+2028) or a literal CR+LF in the text list. A plain Enter key only confirms the field, it does not insert a line break.
Why does the CPU say "Retain memory exceeds limit" during compile?
The combined size of the retentive Merker range and the retentive DB data exceeds 10 KB. Reduce the number of retentive Merker bytes, remove the retain attribute from large arrays, or split the recipe data into a non-retentive area and a small retentive index block.
Does an MRES reset clear the retentive data?
Yes by default. If you need MRES to preserve the retentive area, enable Retentive data after MRES in the device properties. Always back up the project before issuing an MRES.
Can I retain data on an SD card rather than internal flash?
The S7-1200 stores the retentive image in the internal non-volatile area; the SIMATIC Memory Card is used for the program and for transfer cards. The retain area itself is not user-movable. The internal area is sufficient for the 10 KB limit on the 1214C.