How to Use Data Blocks in Siemens PLC: Syntax and Examples
In Siemens PLC programming,
Data Blocks (DBs) are used to store and organize data persistently. You create a DB to hold variables that your program can read or write, allowing structured data management separate from code logic.Syntax
A Data Block (DB) in Siemens PLC is defined with a name and contains variables of different types. You declare variables inside the DB, which can be accessed in your program using the DB name and variable name.
Basic syntax example:
DATA_BLOCK DB1: Defines a data block named DB1.VAR...END_VAR: Declares variables inside the DB.- Variables can be of types like
INT,BOOL,REAL, etc.
Access variables in your program using DB1.VariableName.
plc
DATA_BLOCK DB1 VAR Counter : INT; MotorOn : BOOL; END_VAR
Example
This example shows how to create a data block DB1 with two variables and how to use them in a simple ladder logic or structured text program to control a motor start condition.
plc
DATA_BLOCK DB1 VAR Counter : INT; MotorOn : BOOL; END_VAR // Program code example in Structured Text IF DB1.Counter < 10 THEN DB1.MotorOn := TRUE; DB1.Counter := DB1.Counter + 1; ELSE DB1.MotorOn := FALSE; END_IF;
Output
When run, the motor will be ON while Counter is less than 10, incrementing Counter each cycle. Once Counter reaches 10, MotorOn turns OFF.
Common Pitfalls
Common mistakes when using data blocks include:
- Not initializing variables inside the DB, leading to unpredictable values.
- Confusing instance DBs with global DBs, which affects data sharing and memory usage.
- Accessing DB variables without specifying the DB name, causing errors.
- Modifying DB structure after deployment without proper migration can cause data loss.
Always initialize variables and use consistent DB naming.
plc
DATA_BLOCK DB1 VAR Counter : INT := 0; // Correct: initialized MotorOn : BOOL := FALSE; END_VAR // Wrong usage example // Missing DB name when accessing variable // MotorOn := TRUE; // This will cause error // Correct usage DB1.MotorOn := TRUE;
Quick Reference
| Concept | Description | Example |
|---|---|---|
| Data Block (DB) | Stores persistent variables | DATA_BLOCK DB1 |
| Variable Declaration | Defines data inside DB | VAR Counter : INT; END_VAR |
| Access Variable | Use DB name and variable | DB1.Counter |
| Initialization | Set default values | Counter : INT := 0; |
| Instance DB | DB linked to function block instance | Used in FB calls |
Key Takeaways
Data Blocks (DBs) store persistent variables separately from program logic in Siemens PLC.
Declare variables inside a DB using VAR...END_VAR and initialize them to avoid errors.
Access DB variables in your program by prefixing with the DB name, e.g., DB1.Counter.
Be careful with DB structure changes after deployment to prevent data loss.
Use instance DBs for function block data and global DBs for shared data.