Mastering IF Statements in Codesys Using ST Language

 Introduction:


In the field of industrial automation, PLCs (Programmable Logic Controllers) are widely used for controlling and monitoring machinery and process operations. Codesys is a commonly used PLC programming environment that supports multiple programming languages, including Structured Text (ST). ST language, akin to high-level languages, provides robust control structures. This article will introduce the IF statement in ST language, helping everyone to understand and apply this basic control structure.


1

   

Statement Introduction

The IF statement is a fundamental conditional control structure used to execute different blocks of code based on whether a condition is true or false. Its basic syntax is as follows:

IF condition THEN

(* Statements if condition is true *)

ELSIF another_condition THEN

(* Statements if another_condition is true *)

ELSE

(* Statements if none of the above conditions are true *)

In the structure above, condition is the condition to be evaluated. ELSIF and ELSE parts are optional and are used to handle other scenarios. Here's how it looks with these additional components:

2

   

Difference Between IF and ELSIF


  • IF Statement: Used to evaluate the first condition. If the condition is true, the corresponding block of code is executed.
  • ELSIF Statement: Used to evaluate subsequent conditions when the previous IF or ELSIF condition does not hold true. There can be multiple ELSIF statements, evaluated in sequence.


The key difference is:

IF is the initial condition check, while ELSIF is for subsequent condition checks. IF and ELSIF statements are fundamentally mutually exclusive! This means that once the IF statement is executed, the program will skip over the ELSIF statements. In a program containing an IF statement and multiple ELSIF statements, only one condition (the first one that is met) will be executed; if none are met, the ELSE is executed.


3

   

IF Programming Approach


When using IF statements, it's crucial to plan the conditions and corresponding actions clearly. Here are key points for the programming thought process:


  • Define Conditions: Clearly identify the conditions that need to be evaluated and strive to simplify these conditional expressions.
  • Handle Branches: Define specific operations for each conditional branch, ensuring the logic is correct.
  • Consider Sequence: IF statements evaluate conditions in sequence, so arrange them according to priority.
  • Optimize Code: Avoid code duplication and keep the code concise and readable.


4

   

Case Study Introduction


Suppose we need to control a heater based on readings from a temperature sensor. The scenario is as follows:


  • Temperature below 20°C: Heater turns on
  • Temperature between 20°C and 25°C: Maintain current state
  • Temperature above 25°C: Heater turns off


Here's an example implementation using IF statements:

VAR

temperature : REAL;

heaterOn : BOOL;

END_VAR

IF temperature < 20.0

THEN heaterOn := TRUE;

ELSIF temperature > 25.0 THEN heaterOn := FALSE;

ELSE

END_IF

In this example, the heater's state is controlled based on different temperature ranges to ensure the temperature is maintained at an appropriate level.

CONlusion:

IF statements are a basic control structure in ST (Structured Text) language, ideal for simple conditional judgments and branching operations. By using IF and ELSIF statements judiciously, engineers can create code that is logically clear and easy to maintain. Mastering the use of these statements in everyday development will help efficiently tackle various control problems. I hope this article helps you to better understand and apply IF statements.