Variables and Assignment
Variable Naming
In Structured Text (ST), variables are the building blocks of your logic. They represent inputs, outputs, and internal states within your PLC program.
Naming Rules
-
Must begin with a letter (A–Z or a–z)
-
Can contain letters, digits, and underscores ( _ )
-
No spaces, symbols (except _ ), or starting digits
-
Case-insensitive in most environments (but using consistent case is recommended)
Good Naming Conventions
-
Use descriptive names that reflect the purpose of the variable.
-
Use camelCase or PascalCase consistently (or whatever style your team/project uses).
-
Prefixes can be helpful (e.g., is, has, num, val, btn, etc.)
Examples:
Motor_Start // Boolean to start motor
Counter_Value // Integer counter
Temperature_Set // Real number (e.g., 23.5)
Valve_Open // Output signal to open valve
Avoid names like:
x1, val, temp2, a123 // Not meaningful
Assignment Operator
In Structured Text, assignment is done using :=, not =.
Variable := Value;
-
:= assigns the value on the right to the variable on the left.
-
A semicolon ( ; ) must follow the assignment.
Examples:
Motor_On := 1;
Counter := Counter + 1;
Note: The = symbol is used only in comparisons, such as IF x = 10 THEN.
Tag References
variables (also called tags) are categorized into:
1. Input Tags
-
Represent physical or logical input signals from sensors, buttons, etc.
-
Usually read-only inside logic.
Start_Button, Temp_Sensor, Limit_Switch
2. Output Tags
-
Represent signals that go out to actuators, motors, alarms, etc.
-
Controlled by your logic.
Motor_Start, Buzzer, Valve_Open
3. Internal Tags
-
Variables used for intermediate logic, memory, and calculations.
-
Not tied to physical I/O.
Counter_Value, Temp_Average, isRunning
Each tag type can be used in ST just like normal variables, but their source (hardware or logic) determines how they behave in the program.
Examples
// Start the motor when the start button is pressed
Motor_Start := Start_Button;
// Increment the counter value
Counter_Value := Counter_Value + 1;
// Turn on the fan if the temperature exceeds the setpoint
IF Temp_Sensor > Temp_Setpoint THEN
Fan_On := 1;
END_IF;
// Reset the counter when the stop button is pressed
IF Stop_Button THEN
Counter_Value := 0;
END_IF;