Mastering SCL Variables
Kenneth Jensen
Automation, Integration, & Controls Engineer / Electrical Engineer / Power Coordinator / Drive Engineer
Introduction
Variables are essential elements in PLC programming, used to store and manipulate data. In Structured Control Language (SCL), variables are categorized based on their scope and access type. Understanding how to define and use these variables is crucial for effective PLC programming.
Types of Variables
Input Variables (`VAR_INPUT`)
Input variables are used to receive data from external sources, such as sensors, switches, or other devices. These variables are read-only within the function block.
Example:
VAR_INPUT
StartButton : BOOL; // Start button input
Temperature : REAL; // Temperature sensor input
END_VAR
In this example, StartButton and Temperature are input variables that receive data from external sources.
Output Variables (`VAR_OUTPUT`)
Output variables are used to send data to external devices, such as actuators, indicators, or other controllers. These variables are write-only within the function block.
Example:
VAR_OUTPUT
MotorOn : BOOL; // Motor state output
CoolingFan : BOOL; // Cooling fan state output
END_VAR
In this example, MotorOn and CoolingFan are output variables that send data to external devices.
Input-Output Variables (`VAR_IN_OUT`)
Input-output variables are used to both receive data from and send data to external devices. These variables can be read and written within the function block. This is particularly useful for User-Defined Data Types (UDTs), making it easier to edit them without affecting the function block.
Example:
VAR_IN_OUT
Pressure : REAL; // Pressure value input-output
FlowRate : REAL; // Flow rate value input-output
END_VAR
In this example, Pressure and FlowRate are input-output variables that can be read from and written to external devices.
Local Variables (`VAR`)
Local variables are used within the function block for internal processing. These variables are not accessible from outside the function block.
Example:
VAR
Counter : INT; // Internal counter
TempValue : REAL; // Temporary value for calculations
END_VAR
In this example, Counter and TempValue are local variables used for internal processing within the function block.
领英推荐
Practical Examples
Example: Control Logic with Different Variable Types
Let's create a simple control logic example that demonstrates the use of input, output, input-output, and local variables.
FUNCTION_BLOCK FB_ControlLogic
VAR_INPUT
StartButton : BOOL; // Input variable for start button
StopButton : BOOL; // Input variable for stop button
END_VAR
VAR_OUTPUT
MotorOn : BOOL; // Output variable for motor state
END_VAR
VAR_IN_OUT
Setpoint : REAL; // Input-output variable for setpoint
END_VAR
VAR
Running : BOOL; // Local variable for running state
Counter : INT; // Local variable for counting cycles
END_VAR
BEGIN
// Control logic for motor
IF StartButton AND NOT Running THEN
MotorOn := TRUE;
Running := TRUE;
ELSIF StopButton THEN
MotorOn := FALSE;
Running := FALSE;
END_IF;
// Increment counter if motor is running
IF Running THEN
Counter := Counter + 1;
END_IF;
// Adjust setpoint based on some condition
IF Counter > 100 THEN
Setpoint := Setpoint + 1.0;
Counter := 0; // Reset counter
END_IF;
END_FUNCTION_BLOCK
Explanation of the Example
- Input Variables: StartButton and StopButton are inputs that control the start and stop of the motor.
- Output Variable: MotorOn is an output that indicates the state of the motor.
- Input-Output Variable: Setpoint is an input-output variable that can be adjusted within the function block.
- Local Variables: Running and Counter are local variables used for internal logic and counting cycles.
Defining Variables in Siemens PLCs
In Siemens PLCs, tags (variables) are often defined in data blocks (DBs), which allows for centralized and organized data management. However, you can also define variables directly within a function block in SCL without explicitly worrying about data blocks. When you define variables within a function block, they are inherently managed within the scope of that block.
Advantages of Defining Variables in Function Blocks
- Encapsulation: Variables are encapsulated within the function block, providing a clear and organized structure.
- Simplified Access: Directly defining variables in the function block simplifies access and reduces the need for additional data block management.
- Modularity: It enhances the modularity of your code, making it easier to reuse and maintain function blocks.
When to Use Data Blocks
- Global Data: Use data blocks when you need to manage global data that is accessible across multiple function blocks or programs.
- Complex Data Structures: Data blocks are useful for managing complex data structures and large arrays.
In summary, while data blocks are a powerful tool for data management, defining variables directly within a function block in SCL is a perfectly valid and often convenient approach, especially for encapsulated logic and simpler projects.
Final Thoughts
Understanding how to define and use different types of variables in SCL is crucial for effective PLC programming. By mastering these concepts, you can create more organized and efficient control logic.
Stay tuned for the next article in my series, where I'll dive into more advanced SCL programming techniques!
---
About the Author:
Ken Jensen brings over 24 years of experience in electrical, automation, & controls engineering across diverse industries like nuclear, steel, and glass. He is a highly skilled problem-solver with expertise in electrical systems, PLCs, SCADA, and Industry 4.0 technologies. He is adept at leading projects, driving innovation, and fostering collaboration.