Siemens S7 Data Blocks: DB vs M Memory and Retentivity Guide

David Krause17 min read
SiemensTechnical ReferenceTIA Portal
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

Siemens S7 Data Blocks: DB vs M Memory, Retentivity, and HMI Integration

Data blocks (DBs) are the principal named memory containers in the Siemens S7-300, S7-400, S7-1200, and S7-1500 families. They coexist with the simpler M (flag) memory area, but they differ structurally, semantically, and in their behavior across power cycles. This reference explains what a data block is, how it differs from M memory, when to use each, and how the retentive and initial-value mechanisms apply during loading, restart, and warm restart of the CPU.

Scope. This article applies to STEP 7 V5.x (S7-300/400) and to the TIA Portal (S7-1200/1500). Where the behavior differs between the two generations, both cases are shown.

1. What Is a Data Block?

A Data Block (DB) is a logically grouped area of the CPU's work memory that holds user data structured by a declaration. Each element inside the block has a defined type (BOOL, INT, REAL, ARRAY, STRUCT, etc.) and an address that the program accesses symbolically or absolutely. In the IEC 61131-3 model used by Siemens, a DB is the equivalent of a tag table or global variable list that has been compiled into the user program.

Formally, a data block is a block of bits whose length equals the size of the declared data structure - in the cryptographic sense the term comes from operating-system and storage theory where a block is "a sequence of bytes or bits, usually containing some whole number of records, having a fixed length" (Block (data storage) - Wikipedia). The NIST Computer Security Resource Center defines a data block in the same terms: "a sequence of bits whose length is the block size" (Data Block - CSRC Glossary). In Siemens PLCs the same word is reused for an IEC 61131-3 user data structure.

Three fundamental block types exist in the S7 system:

  • Organization Blocks (OB) - the CPU's program entry points (OB1 for the main cyclic scan, OB100 for warm restart, OB101 for hot restart, OB102 for cold restart, OB82/OB86/OB122 for error handling).
  • Function Blocks (FB) and Functions (FC) - the executable code units.
  • Data Blocks (DB) - the data containers associated with the program.

DBs come in two principal variants:

  • Global DB (also called "DB" in STEP 7 V5.x or "Global data block" in TIA Portal) - declared once, accessible from any code block in the program.
  • Instance DB - automatically generated when an FB is called with a background DB; the instance DB stores the static data of that specific FB call.

Reference: Siemens Industry Online Support - entry ID 109751433, "Programming and operating manual STEP 7 Basic/Professional".

2. DB Memory Layout and Address Syntax

Inside a DB every byte has an absolute address and a symbolic tag. The access syntax depends on the CPU family.

Table 1 - DB access syntax by CPU family
Address granularity S7-300/400 (STEP 7 V5.x) S7-1200/1500 (TIA Portal) Data type
Bit DB1.DBX0.0 "MyDB".Tag.BIT_0 BOOL
Byte DB1.DBB1 "MyDB".Tag.BYTE_1 BYTE
Word DB1.DBW2 "MyDB".Tag.WORD_2 WORD
Double word DB1.DBD4 "MyDB".Tag.DWORD_4 DWORD / REAL
Array element DB1.DBD[INDEX*4] "MyDB".Recipe[INDEX] User-defined

Symbolic access is preferred on S7-1200/1500 because TIA Portal enforces it in many programming languages (SCL, GRAPH). On S7-300/400, absolute access is the historical default and remains available for compatibility with installed code.

3. DB vs M Memory: When to Use Each

The M area (also called flag memory or merker memory) is a flat byte-addressed range located in the CPU's work memory. The default size is 256 bytes on a CPU 315-2 PN/DP and 8192 bytes on an S7-1516, with part of the range declared as retentive. The M area is intended for short-lived, low-level scratch flags that exchange signals between code blocks. It has no declaration, no initial value, and no structure.

The DB is the equivalent of a typed structure: a recipe table, a set of machine parameters, the working storage of an FB. The DB can be referenced symbolically, exported as a UDT, downloaded separately from the program, monitored, and force-modified without recompilation.

Table 2 - DB vs M memory comparison
Property DB (global) M memory
Structure Typed declaration (BOOL, INT, REAL, ARRAY, STRUCT, UDT) Untyped flat byte range
Symbolic access Yes (preferred on S7-1200/1500) Yes (M0.0, MW2), but no per-bit symbol typing
Initial value at start Yes - declared in the data block Zero (or retentive last value)
Default value at power-on Initial value Zero for non-retentive; last value for retentive
Retentivity configurable per tag Yes (S7-1500) / only at block level (S7-300) Yes, but in fixed MB ranges
Use with HMI Direct connection to OP/TP/WinCC tags Possible but not recommended
Use as recipe storage Yes (standard practice) No
Indirect addressing Yes (OPN DB + DIX/DID, or symbolic with array index) Yes, but flat only
Visibility in PLC tag tables Yes Yes (as M tags)
Maximum size Up to work memory (e.g. 16 MB on S7-1518) Limited (default 256 B on S7-300, up to 8 KB on S7-1500)

Rule of thumb used by senior STEP 7/TIA Portal engineers:

Use M memory only for single-bit handshakes, edge flags, and intermediate Boolean results. Use a DB for everything that has meaning, value, or a name an operator will see on the HMI.

3.1 Concrete Example: db1.dbb1 vs mb1

Both DB1.DBB1 and MB1 resolve to byte 1 in their respective memory areas. The differences are:

  • Location - DB1.DBB1 is byte 1 inside the work memory of data block 1; MB1 is byte 1 of the flag area.
  • Initial value - DB1.DBB1 is initialized to whatever value the declaration view specifies; MB1 starts at 16#00 (or the retentive last value).
  • Symbolic name - DB1.DBB1 can be tied to a symbol such as Recipe.Slot[0].Temperature; MB1 typically carries a one-word symbol such as Edge_Memory.
  • HMI connection - TIA Portal and WinCC automatically pick up the structured DB tag; M flags must be added manually to the HMI tag table.

4. Initial Value vs Actual Value

Every tag inside a global DB has two values associated with it in STEP 7 / TIA Portal:

  • Initial value (Startwert / Initial value) - the value defined in the declaration view and stored in load memory. This is the value the CPU writes into work memory at the end of the start-up OB (OB100/101/102).
  • Actual value (Istwert / Actual value) - the value currently held in work memory after the program has executed. It is the runtime value.

Behavior across CPU operating states:

Table 3 - Effect of CPU restart on initial vs actual value
Event Initial value loaded to work memory? Retentive tag preserved? Non-retentive tag?
Cold restart (OB102) Yes - all tags set to initial value No - all overwritten by initial No - all overwritten
Warm restart (OB100) Yes for non-retentive Yes - last value retained Yes - initial value
Hot restart (OB101, S7-400 only) No - all tags keep last value Yes Yes
STOP -> RUN (no restart) No Yes Yes
Power down (non-retentive) On next start: initial value Yes Lost

To monitor both values in TIA Portal, open the Watch table with the DB in question, right-click the tag, and switch the display between "Initial value" and "Actual value". In STEP 7 V5.x this is done via Monitor/Modify > Data block with the Initial values toggle.

Field note. When a project is downloaded, TIA Portal asks whether the actual values should be retained or whether the initial values from the new project should be loaded. Selecting the wrong option is the most common reason for "my recipe values disappeared after the download" - the initial values from the offline project replaced the runtime values.

5. Retentivity of Data Blocks

Retentivity defines which tags keep their value across a power cycle or STOP->RUN transition. On S7-300/400 the retentive area is configured under CPU Properties > Retentive Memory as a range of bytes (e.g. MB0..MB15, DB1.DBW0..DB1.DBW200). On S7-1200/1500 the retentive area is set per tag inside the DB: open the DB, switch to the Retain column, and tick the box for each tag that must survive a power cycle.

The retentive memory is implemented in a battery-backed or non-volatile section of the CPU (NVRAM on S7-1500, MMC-backed on S7-1200 from firmware V4.4). Maximum retentive sizes are documented in the CPU datasheet; for example, the S7-1516-3 PN/DP allows up to 472 KB of retentive DB data.

5.1 How to Verify That a DB Is Retentive

  1. Open the DB in TIA Portal.
  2. Right-click the column header and select Show/Hide > Retain.
  3. For each tag set the Retain checkbox (Set in IDB) for tags that should keep their value.
  4. For S7-300/400: open CPU Properties > Retentive Memory and confirm the DB byte range is included in the retentive area.

5.2 Alternatives to Retentive DBs

When a DB cannot be made retentive (e.g. on a CPU whose NVRAM is full), the following options exist:

  • Store on MMC / SIMATIC Memory Card - use RecipeSave from the Recipe library or write the data to a file on the SD card via FileHandleC (S7-1500).
  • Use the system DB for data logging - DataLogWrite (S7-1500, firmware V1.8+) writes to CSV files on the SD card.
  • Remanent M area - declare a number of MB flags as retentive on the CPU and copy the OP panel data into MB at the end of OB100.
  • Non-volatile PLC tags (S7-1500) - the entire PLC tag table supports a Retain attribute, which is functionally equivalent to a global DB with retentive tags.

Reference: Programming and Operating Manual - SIMATIC S7-1500 with TIA Portal (Siemens).

6. Using DBs with HMI Panels (OP/TP/WinCC)

Reading from and writing to a data block is the standard way to expose process data to an operator panel such as an OP17, TP177, KTP1200, Comfort Panel, or WinCC Runtime Advanced. The connection is direct because the HMI tag table accepts the absolute DB address or, in TIA Portal, the symbolic tag name.

6.1 STEP 7 V5.x and ProTool/OP17

  1. In STEP 7, create a DB (for example, DB100) and declare the tags that the operator will read or write.
  2. In ProTool/Pro, open the tag editor and add a tag pointing to DB100.DBW0 as an input/output field, or DB100.DBX0.0 as a button.
  3. Wire the tag to a screen object; the value entered on the OP is written into the DB and is immediately available to the PLC program.

6.2 TIA Portal and Comfort Panel

  1. Create a global DB (for example, "Recipe_DB") with the data structure.
  2. In the HMI tag table, click Add new tag > PLC tag; the editor shows the symbolic structure of the DB.
  3. Drag the tag onto an I/O field; the panel reads and writes the tag without any address arithmetic.

This is the recommended pattern. M flags are not preferred because every additional HMI tag adds to the configuration effort and offers no symbolic documentation.

7. DB Use Cases in Real Programs

7.1 Recipe Storage

The classic application. A recipe is a setpoint package (temperature, time, speed) loaded by the operator. Store the recipe in a DB declared as an ARRAY of STRUCT. The array index is the recipe number; the elements are the parameters. The DB can be made retentive so the operator's edits survive a power cycle.

// TIA Portal SCL example
DATA_BLOCK "Recipes"
{ S7_Optimized_Access := 'TRUE' }
AUTHOR : 'Eng'
VERSION : 0.1
NON_RETAIN
VAR
  Slot : ARRAY[1..50] OF STRUCT
    Temperature : REAL := 80.0;
    Duration_s  : INT  := 120;
    Speed_pct   : INT  := 50;
  END_STRUCT;
END_VAR
END_DATA_BLOCK

7.2 Indirect Addressing with Arrays

DBs are the natural home for arrays because the access DB[index].Element is symbolically clean. The IEC check on the index prevents out-of-bounds reads on optimized DBs (S7-1500), making it safer than bit-twiddling M flags.

7.3 Instance DBs for FBs

When an FB is created with a multi-instance capable declaration (static VAR section), the FB's internal state lives in an instance DB. The instance DB is created automatically at the first call or by clicking Generate instance DB. On S7-1500 the multi-instance model means a single DB holds the data of all FB instances of the same parent FB.

7.4 Hand-Over Areas Between PLC and SCADA

A common pattern is to expose a DB with a fixed layout as the SCADA hand-shake area. The SCADA writes commands into one half, the PLC writes status into the other half, and a handshake word toggles to acknowledge the new data. This avoids the proliferation of M flags and gives the SCADA library a single source of truth.

8. Optimized vs Standard Access (S7-1200/1500)

On S7-1200 and S7-1500 each DB can be set to Optimized block access (default since TIA Portal V13) or Standard access (compatible with S7-300/400). The differences matter for any code that uses absolute addressing.

Table 4 - Optimized vs standard DB access
Property Optimized (S7-1500 default) Standard (S7-300 compatible)
Symbolic access only Yes (compiler enforced) No - both symbolic and absolute
Per-tag retentivity Yes No - block-level only
Memory layout Compiler-managed, gaps possible Sequential, predictable byte layout
Use with HMI absolute addressing No - HMI must use symbolic tag Yes
Download without re-initializing Yes - actual values preserved No - usually re-initializes

For new projects, keep optimized access. For migration projects with many absolute addresses, switch to standard access or rewrite the address logic symbolically.

9. Diagnostics and Common Errors

Table 5 - DB-related diagnostics matrix
Symptom Likely cause Check Fix
CPU goes to STOP, SF LED on, diagnostic buffer: "Area length error when reading" Absolute access beyond DB length Cross-check the address in the offending code block against the DB declaration Correct the offset or extend the DB
Recipe values reset to default after every power-on Tags not marked retentive DB > Show/Hide > Retain column Tick Retain for the required tags; confirm CPU has retentive memory free
Tags reset after a download from TIA Portal Download option "Reset to initial values" selected Online > Download to device > Options Re-download and select "Consistent download; do not initialize retentive data"
HMI shows "###" or "Address error" DB type changed, HMI tag no longer matches Re-compile the HMI tag table Update the HMI connection and re-compile
Time-stamp conflict during download Block was modified offline but the S7-1500 sees the timestamp as online Compare online/offline; check "Consistent download" Disable the time-stamp check for the affected block, or do a full download
M flags cleared on STOP->RUN transition MB range not in retentive area CPU Properties > Retentive Memory > Bytes Add the MB range or move the data to a retentive DB

10. Best Practices

  1. Use symbolic access on S7-1200/1500 projects. Avoid absolute addresses inside optimized DBs because the compiler may reorder memory.
  2. Set retentivity per tag on optimized DBs. Reserve retentive memory for data that must survive a power cycle, not for convenience.
  3. Prefer a global DB over scattered M flags for any value that has operator or maintenance meaning.
  4. Use a UDT (User-Defined Type) for repeated structures. Declare a UDT once, instantiate it inside a DB as an ARRAY, and the HMI picks it up symbolically.
  5. Document the DB layout in the DB header comment and in the watch table. A new commissioning engineer should be able to understand the data flow from the DB comment alone.
  6. Verify retentivity on first commissioning. Power-cycle the CPU with the plant in a known state and confirm that the operator's recipe values return intact.
  7. When migrating from STEP 7 V5.x to TIA Portal, convert each DB to a UDT where possible; this makes the structure reusable across multiple instances.
  8. Keep the number of M flags small. Reserve MB0..MB15 for handshake bits and use the DB for everything else. This rule is taught in Siemens ST-7PRO1 and ST-7PRO2 training and is the de facto standard on S7-300/400 installations.

11. Procedure: Creating a Global Data Block in TIA Portal

11.1 Prerequisites

  • TIA Portal V16 or later installed.
  • S7-1200 or S7-1500 CPU added to the project (S7-1500 recommended for the full retentivity model).
  • Program blocks folder with at least one code block such as OB1 "Main".

11.2 Step-by-Step

  1. In the project tree, expand PLC_x > Program blocks.
  2. Double-click Add new block > Data block (DB).
  3. Choose Global DB (not Instance DB) and click OK.
  4. In the new DB editor, declare the variables. For each row, set name, data type, and default value.
  5. To enable per-tag retentivity, right-click a column header, choose Show/Hide > Retain, and tick the box for the tags that must survive a power cycle.
  6. Save and compile (Ctrl+B).
  7. Download to the CPU. The prompt "Do you want to initialize retentive data?" - select No if the current values must be kept.
  8. Open a watch table, add the DB tags, and click Monitor all to confirm the actual values match the expected values.

11.3 Verification

  • Confirm that the declared tags appear in the PLC tag table and can be browsed from the HMI configuration.
  • Power-cycle the CPU and confirm that retentive tags keep their values and non-retentive tags return to the initial value.
  • Use Online > Diagnostics > Memory to check the usage of the retentive memory area against the CPU's maximum.

12. Reference: Memory Areas in the S7 CPU

For context, the full set of S7 memory areas is:

  • I / Q (Process Image) - inputs and outputs, refreshed at the start and end of OB1.
  • M (Flags / Merker) - flat working memory, part retentive.
  • DB (Data Blocks) - structured, typed, symbolic data, partially retentive.
  • L (Local / Temp) - per-block scratch memory, not retentive, lost when the block exits.
  • PI / PQ (Peripheral I/O) - direct I/O access bypassing the process image.
  • T / C / Z (Timers, Counters, IEC counters) - system function memory.

Local data is the only area that resets on every block call. Retentive DB tags are the only user data that survives a power cycle. M flags are the only flat, byte-addressable, fast area for handshakes.

Reference documentation: Siemens Industry Online Support, search IDs 109751433 (S7-1500), 91696622 (S7-1200), 18652696 (STEP 7 V5.5 system manual). See also the IEEE / IEC 61131-3 standard for the formal definition of the data block concept in PLC programming.


Frequently Asked Questions

What is the difference between a data block and M memory in Siemens S7?

A data block (DB) is a typed, structured memory area declared in the project; every tag has a name, data type, and configurable retentive behavior. M memory is a flat, untyped byte range used for short-lived flags. Use DBs for operator-visible or semantically meaningful data; use M memory for bit-level handshakes and intermediate Boolean results.

Are data block values retained after a CPU power-down?

Only if the tags are declared as retentive. On S7-1500 each DB tag has an individual Retain checkbox; on S7-300/400 the retentive area is defined as a byte range in the CPU properties. Non-retentive DB tags return to their initial value on the next warm or cold restart.

What is the difference between the initial value and the actual value in a DB?

The initial value is the value declared in the offline project and stored in load memory. The actual value is the current value in work memory after the program has run. On a warm or cold restart the non-retentive actual values are replaced by the initial values; retentive values are kept.

Can a data block be used with an HMI panel such as an OP17 or KTP1200?

Yes. In STEP 7 V5.x the OP tag is pointed at an absolute DB address such as DB100.DBW0. In TIA Portal the HMI tag table picks up the symbolic structure of the DB directly, and the tag is dragged onto the screen object. Using a DB is the recommended pattern; M flags are not preferred for HMI exposure.

How do I store operator-entered values across a power cycle without a retentive DB?

On S7-1500 use the Recipe functions or DataLogWrite to save data on the SIMATIC Memory Card. On S7-300/400 the fallback is to copy the panel data into a retentive MB range (CPU Properties > Retentive Memory) and back into the DB on the next start. A retentive DB is still the simplest and most common solution.

What is the difference between a global DB and an instance DB?

A global DB is declared explicitly and stores data shared across the program. An instance DB is generated automatically by the compiler when an FB is called with a background DB; it holds the static VAR section of that FB call. On S7-1500 a single instance DB can be a multi-instance container for several FB instances of the same FB type.

Back to blog