PLC Programming

PLC-Languages 

Programmable Logic Controllers (PLC) are computers which are commonly used in commercial and industrial control applications. Generally speaking, PLCs monitor inputs and other variable values, make decisions based on a stored program and control outputs to automate a process or machine.

A PLC typically consists of input and output modules, a Central Processing Unit (CPU) and a programming device. The primary function of the input unit is to convert the signals at the inputs into logic signals which can be used by the CPU. The CPU analyzes the status of inputs, outputs and other variables and executes the stored program. After that, the CPU changes the output signals.

../../_images/Example_PLC_Wiring.jpg
Fig. 14 Example for PLC wiring for industrial applications

Advantages in contrast to hard-wired control:

  • Easy to modify input and output devices
  • High flexibility due to modular design
  • Significant reduction of cabling
  • Solid-state, no moving parts
  • Smaller physical size
  • Integrated diagnostics and override functions
  • Easy and cost-effective duplication possible
  • Communication capabilities

Information processing

Information at the inputs of the PLC are processed in cycles. First, the CPU queries the input channels and stores the data in the working memory. This storage area is called “input image” as the stored input data does not show the current state of the inputs but the available data at the time of sampling.

Next, the program is executed step by step and the variables are stored in the memory. Finally, the calculated output parameters are stored in the “output image” and transferred to the output channels to control the connected machine.

../../_images/PLC_Process.JPG
Fig. 15 Cyclic information processing of PLCs

Types of input and output signals:

  • Binary inputs: Input module can distinguish between a high and a low level of the input signal (typical value: 0V and 24V).
  • Binary outputs: Output modules generate TRUE (5V) or FALSE (0V) values which are typically intensified (24V) using transistors or relays.
  • Analog inputs: A physical measurand is transformed into a voltage or current signal using a transducer. The resulting signal is transferred to the analog inputs of the PLC.
  • Analog outputs: A voltage or current signal is generated to control actuators.

PLC Programming (IEC 61131)

PLC programming is standardized in the IEC 61131.

Controller configuration and resources

Creating a controller configuration, the hardware structure is specified in the programming environment. All resources which are controlled by the PLC software (input, output modules, interface cards, etc.) are declared. Nowadays, this is usually done automatically by scanning the connected components or by drag-and- dropping the parts.

Input channels are declared using %I and outputs specified writing %Q. The following letter defines the data type of the signal (bit X, byte B, word W, doubleword D). In this context, the declaration %IX1.2 calls the second channel of the first binary input card.

Labelling of input and output addressing
  1. Letter
  1. Letter
Example
AT% I Input x Bit AT%IX1.2
Q Output B Byte, 8 Bit AT%QB0
W Word, 16 Bit AT%QW7
D Doubleword, 32 Bit AT%QD5

Tasks

Tasks organize the time schedule of programs. It works a bit like a cyclic step counter, which selects the instructions of the program in a clock-controlled way. Based on that, the CPU processes the instruction which is stored in the selected storage area.

Several programs can be assigned to one task running all in the same cycle time. The following process is conducted (IPO principle; input-processing-output):

  • Read input data of all programs.
  • Processing of all programs.
  • Output of the output data of all programs.

The cycle time of all programs assigned to one task is identical as the output data of all programs is transferred simultaneously at the end of the processing cycle.

One CPU can process multiple tasks with different cycle times (multitasking). Therefore, the computing power is distributed between the different tasks using interrupt signals for changing.

Program Organization Units (POUs)

An object of the type POU is a Program Organization Unit in a CODESYS project. You write source code for the controller program in POUs. There are the following types of POUs:

  • Programs
  • Function Codes FC
  • Function Blocks FB
../../_images/Function_FunctionBlock.JPG
Fig. 16 Difference between Function Codes and Function Blocks

Function Codes can have several input variables but only one output variable. This variable is the return value of the Function Code. Function codes have no internal memory, thus they cannot store intermediate values. Function Codes can be separated between standard and user-specific functions. Standard functions like for example AND, ADD or BYTE_TO_WORD can be used directly during programming without declaring it separately.

Function Blocks can also be standardized or user-specific. Examples for pre-defined FBs are triggers, counters and timers. FBs can store intermediate values for the next IPO cycle and can have several outputs Q. The output signals are created by connecting the inputs I and the states Z.

Variables

Variables are used to establish a communication between different program organization units. Different kinds of variables are available:

  • VAR: Local variables which are only valid in the associated POU.
  • VAR_GLOBAL: Global variables are valid in all POUs. They are used for communication between different programs.
  • VAR_INPUT: Input variables which are used to write inputs in FCs or FBs.
  • VAR_OUTPUT: Output variables of FCs or FBs.
  • VAR_IN_OUT: Input and output variables can be changed in the FB and then be released.
  • VAR_RETAIN: Variables retain the value when the PLC is put off and on again.
  • VAR_PERSISTENT: Variables retain the value if the software is loaded on the PLC.

Standard data types:

Datatype Size Range Example
Bit sequence BOOL 1 Bit FALSE / TRUE FALSE
BYTE 8 Bit 16#00 … 16#FF 16#00
WORD 16 Bit 16#0000 … 16#FFFF 16#0000
DWORD 32 Bit 16#00000000 … 16#FFFFFFFF 16#00000000
Whole numbers SINT 8 Bit -128 … 127 0
INT 16 Bit -32768 … 32767 0
DINT 32 Bit -2147483648 … 2147483647 0
Whole numbers without sign USINT 8 Bit 0 … 255 0
UINT 16 Bit 0 … 65535 0
UDINT 32 Bit 0 … 4294967295 0
Floating point figure REAL 32 Bit -3.4 * 10^38 … 3.4 * 10^38 0
Time TIME
Time of day TIME_OF_DAY
Date DATE
Character sequence STRING

A typical variable declaration consists of five parts:

<variable name> AT<address> :<data type> :=<initial value>; (* comment *)

example: buttonStart AT%IX0.0 :BOOL :=FALSE; (*Start Button*)

Note

It is helpful to use speaking names instead of numbers to make it easy to understand the program (e.g. buttonLight and buttonVentilator instead of button1 and button2).

Derived data types:

Standard data types can be modified to fulfil special needs: TYPE Name : ... END_TYPE

Four different kinds can be distinguished:

  • Enumeration: TYPE A_STATUS : (START,RUN,WAIT,STOP); END_TYPE
  • Area: TYPE A_DATA : UNIT(0..16#3FF); END_TYPE
  • Field: TYPE A_4IN : ARRAY[1..4] OF A_DATA; END_TYPE
  • Structure: TYPE str_Motor : STRUCT ... END_STRUCT; END_TYPE

Enumerations are typically used if different options are possible and if easy readable code should be produced.

Programming languages (IEC 61131)

Programmable Logic Controller (PLC) programming as other programming tasks has defined set of rules described in the IEC 61131-3. In this Standard, information about programming concepts and industry accepted programming languages is provided. For this module, SCL, LAD and FBD are selected as they are most commonly used.

../../_images/programmlanguages.png
Fig. 17 Five programming languages of the IEC 61131-3 standard (source: https://www.motioncontroltips.com/iec-61131-3-plcopen/)
../../_images/lad_fbd_scl_demo.png
Fig. 18 SCL, LAD & FBD (ref: Siemens S7-1200 Programmable controller System Manual)

Structured Control Language (SCL)

SCL (Structured Control Language) is a high-level text-based programming language. It is based on PASCAL.

../../_images/scl_demo.png
Fig. 19 An example of a SCL code 1.
../../_images/blink_switch_ex.png
Fig. 20 An example of a SCL code 2.

Various important syntax for SCL are provided below:

Assignments: A := 10; (The variable A is assigned the value 10.)

Statements: blink : BOOL; (Create a variable blink and assign the type BOOL.)

Mathematical functions: +, -, *, /

Addressing of global variables (tags): “<tag name>” (Tag name or data block name enclosed in double quotes)

Addressing of local variables: #<variable name> (Variable name preceded by “#” symbol)

Comments
Single line comment: // comment

Multi line comment and comments after end of ST line: <statement>; (* comment *)

Operators: Standard operators in ST ordered after precedence:

Operation Symbol Precedence
Parentheses (expr) Highest
Function Evaluation MAX(A,B)
Negation Complement NOT
Exponentiation **
Multiply *
Divide /
Modulo MOD
Add +
Subtract -
Comparison <,>,<=,>=
Equality / Inequality = / <>
Boolean AND & AND
Boolean Exclusive OR XOR
Boolean OR OR Lowest

Note

A = B and A := B are NOT SAME!

= (equality operator) evaluates if the left and the right side is equal. If value of A is equal to value B. It returns TRUE if yes, else it returns FALSE.

:= denotes a statement. It is used for assigning value of right side to the left side. In this case, the value of B is assigned to A.

Combining operators:

IF (Input1) AND (Input2) OR (Input3) THEN
    Output1 := True;
END_IF

IF Statements: If statements are used for boolean queries.

IF [boolean expression] THEN
    <statement>;
ELSIF [boolean expression] THEN
    <statement>;
ELSE
    <statement>;
ENDIF;

CASE Statements: Case statements are one of the most important structuring methods to generate readable code. Case statements are typically used to program state machines.

TYPE
    Steps:(INIT:=0, START, RUN, END);
END _TYPE

VAR
    state: Steps; (*use of enumeration*)
END_VAR

CASE state OF
    INIT: Instruction_A;
    START: Instruction_B;
    RUN: Instruction_C;
    END: Instruction_D;
END_CASE

FOR Loops: It is used to repeat code a specific number of times.

FOR count := initial_value TO final_value BY increment DO
    <statement>;
END_FOR;

WHILE Loops: It is used to repeat the loop as long as some conditions are TRUE. A WHILE loop will repeat as long as a boolean expression evaluates to TRUE.

WHILE [boolean_expression] DO
    <statement>;
END_WHILE;

REPEAT Loops: It works the opposite way of the WHILE loop. This loop will stop repeating when a boolean expression is TRUE.

REPEAT
    <statement>;
UNTIL [boolean_expression]
END_REPEAT;

Ladder Diagram (LAD)

Ladder diagrams are specialized schematics commonly used to document industrial control logic systems.

In terms of TIA Portal program for Siemens PLCs, SCL plays an important role in programming of functions codes and function blocks. The Main [OB1] block is preferred to be programmed using the Ladder Diagrams.

A program written in LAD consists of networks. The language is based on relay logic. The flow of the logic ‘true’ across a network is similar to the flow of electricity across a relay logic. One can think of the left rail as being positive, and the right rail as being negative. The logic ‘true’ flows from the left rail to the right rail.

../../_images/lad_in_out_basic.png
Fig. 21 An example of a network in LAD.

Various logical operations can be thus programmed in LAD. The following figure shows AND & OR logic after starting the PLC or simulated PLC.

../../_images/lad_and_or_.png
Fig. 22 An example of logical operations in LAD.

A general list of Bit Logic instructions used in LAD are mentioned below

Operation Symbol
Normally Open Contact (Address) ---| |---
Normally Closed Contact (Address) ---| / |---
Save RLO into BR Memory ---(SAVE)
Bit Exclusive OR XOR
Output Coil ---( )
Midline Output ---( # )---
Invert Power Flow ---|NOT|---
Insert branch
Merge branch
../../_images/lad_ex_network.png
Fig. 23 An example of a network in LAD. (ref: SIEMENS TIA Portal STEP 7 Basic V10.5)

Function Block Diagram (FBD)

FBD is another graphical programming language. It uses Boolean algebra-based blocks to develop codes. A function block is depicted as a rectangular block. The inputs are on the left and outputs on the right side. It can have standard functions, such as logic gates, mathematical operations, counters, or user defined functions.

../../_images/fbd_blocks.png
Fig. 24 Common FBD blocks (ref: SIEMENS Function Block Diagram (FBD) Reference Manual)

Additional languages

The Instruction List (IL) is very similar to the programming language Assembler and typically used to limit the computing time. In the example, the operand A is loaded in the working memory in the first line. Next, an AND operation with the negated operand B is conducted. Finally, the result is stored in the variable C. However, IL gets very long and confusing if complex tasks are programmed. As computing time usually does not play an important role today, high-level languages like Pascal, C or Structured Text (ST) are used typically.

The Sequential Function Chart (SFC) can only be used to program sequential control tasks in the form of step chains.

Examples

SCL

A complete example (programming of a traffic light) is shown below. First, an enumeration including all process states is defined. Next, all necessary variables are declared (a variable called state is defined using the new created data type stateType). Finally, the program sequence is established using a CASE OF statement.

TYPE stateType :
    (RED:=1,REDYELLOW,GREEN,YELLOW);
END_TYPE

VAR
    state:stateType;
    button:BOOL;
    lred,lyellow,lgreen:BOOL;
    atime:TON;
END_VAR
CASE state OF
    stateType.RED:
                IF NOT lred THEN
                    lred:=TRUE;
                END_IF
                IF button THEN
                    state:=stateType.REDYELLOW;
                END_IF
    stateType.REDYELLOW:
                IF NOT lyellow THEN
                    lyellow:=TRUE;
                    atime(IN:=TRUE,PT:=#5s);
                END_IF
                IF atime.Q THEN
                    state:=stateType.GREEN;
                END_IF
    stateType.GREEN:
                IF NOT lgreen THEN
                    lgreen:=TRUE;
                    atime(IN:=TRUE,PT:=#40s);
                END_IF
                IF atime.Q THEN
                    state:=stateType.YELLOW;
                END_IF
    stateType.YELLOW:
                IF NOT lyellow THEN
                    lyellow:=TRUE;
                    atime(IN:=TRUE,PT:=#5s);
                END_IF
                IF atime.Q THEN
                    state:=stateType.GREEN;
                END_IF
END_CASE
../../_images/Ampel.gif

Introduction to SCL Programming in Siemens TIA Portal

PLC PROGRAMMING
SIEMENS
STRUCTURED CONTROL LANGUAGE (SCL)
TIA PORTAL

Introduction

With the Industrial automation and IT worlds converging, IT specialists and Automation Engineers are pushed to develop new skills, in order to stay relevant in the industry.

IT engineers are entering the programming area and starting to write PLC programs using programming languages such as Python and C++, whereas Automation Engineers are learning to deal with SQL databases and being able to write PLC programs using high-level programming languages such as Structured Control Language (SCL).

In this tutorial, you learn about the SCL PLC programming language and how to get started with it in Siemens TIA Portal.

Prerequisites

In this tutorial, we use Siemens TIA Portal software version 16. However, you can use other versions to follow along. This tutorial is meant for people with practical experience with Siemens S7 CPUs such as S7-1200 and S7-1500 CPUs and those familiar with the fundamentals of Ladder Logic PLC programming language.

Still, if you are a beginner in PLC programming, you can follow this tutorial. Having the mentioned skills will speed up your learning process for SCL programming.

Siemens PLC Programming Languages

PLC programmers are acquainted with the standard that standardized programming languages for programmable logic controllers or PLCs. Five PLC programming languages are identified in DIN EN-61131-3 (or IEC 61131-3) standard:

  • Ladder Diagram
  • Function Block Diagram
  • Sequential Function Chart
  • Instruction List, and
  • Structured Text

It’s important to mention that Structured Text (ST) is known as Structured Control Language (SCL) in Siemens software.

If you have ever programmed Siemens PLCs, you will know that when we create programming blocks, the Ladder Diagram is typically the default language and is generally acceptable for the new programming block.

Three standard PLC programming languages are included in the majority of Siemens CPU types, which are Ladder Diagram (LD), Function Block Diagram (FBD), and Structured Control Language (SCL).

But what about Sequential Function Chart and Instruction List? Some Siemens CPUs also support these languages to provide total compliance with the IEC 61131-3 standard.

SCL Definition

SCL is short for Structured Control Language and is built on PASCAL and used for advanced programming. We use letters of SCL to define this programming language. We understand it is Structured Language from S and L letters, and from the letter C, we figure out Control. So, SCL is a structured language utilized to execute control instructions in Siemens PLCs.

Other forms of structured text languages may go by different names and be used by other PLC manufacturers. For example, Rockwell and Omron call this language the Structured Text in their PLC programming software.

Structured Control Language VS Ladder Diagram

If you have programmed Ladder Diagram before for Siemens PLCs, you know that its instructions are displayed graphically like counters, math functions, and shifts, to name a few.

So, a diagrammatic technique of presenting the logic’s flow, details, and connections to external I/O points is called a Ladder. In other words, a Ladder is designed to express a sequence of logic that the PLC should follow according to your programming instructions.

The same goal is achieved using SCL, but it employs a text-based language to carry out the same reasoning.

For instance, we can use a SUB block to subtract two variables from each other in the Ladder Diagram. But in the SCL, that subtraction can be represented using an equation that appears to be more typical.

Creating SCL Programming Block

Let’s begin creating an SCL program right away! We must first launch the Siemens TIA Portal. To do this, double-click over the TIA Portal icon on the desktop or press the Windows button on the bottom left side of the screen, scroll down through apps until you find and expand the Siemens Automation folder, and then click on the TIA Portal icon to open it.

Press the Create new project item, give our project the name SCL Programming and then hit the Create button.

Once the project is built, we will add an S7-1500 CPU to create an SCL programming block. First, left-click over the Configure a device tab, select Add new device item, expand and go through the SIMATIC S7-1500 folder until you find and choose the CPU 1511C-1 PN, and then hit the Add button. One of the Siemens S7-1500 PLC Starter Kits uses this CPU and is popular among buyers.

When the Project View is opened, right-click over the Main OB under the PLC_1 folder in the Project tree, select the Switch programming language item, and then a pop-up menu is displayed. We see that programming languages supported by this PLC model are Function Block Diagram and Ladder Diagram.

The STL (or Statement List) option is grayed out. All CPUs in S7-1200 and S7-1500 series have this trait. It’s worth mentioning that the STL programming language fulfills the Instruction List (IL) language defined in IEC 61131-3 standard.

If you have selected S7-300 or S7-400 CPUs for the PLC configuration, the STL programming language option is available for you to choose for programming their Main OBs.

To build the SCL program, double-click on the Add new block item under the Programs block folder in the left pane of the screen. When the Add new block window opens, select the Function tab and give the new block the name SCL Application.

Expand the Language drop-down menu and select the SCL. Leave the numbering method as Automatic since we want the possible new free block to be assigned automatically and to avoid occurring numbering conflicts during the compilation procedure.

Once the SCL block is created, it appears under the Program block folder in the Project tree.

Double-click over the SCL Application block to open the SCL programming window, which is currently blank.

SCL Programming Window Overview

You can input the SCL program in the programming window, which serves as your workspace. Let us explore different sections that make up the SCL programming window:

  1. Sidebar: It offers the ability to specify breakpoints and bookmarks.
  2. Line Numbers: The program code is presented with the line numbers to the left of it.
  3. Outline View: It emphasizes code portions that are connected.
  4. Code Area: The SCL program is written, developed, edited, and modified in the code section.
  5. Display of the absolute operands: This table demonstrates the mapping of symbolic operands to absolute addresses.

SCL Programming Window Customization

The TIA Portal gives you the power so you can customize how the programming window and program code look. To do so, select the Settings option from the Options menu in the top toolbar.

Once the Settings window is visible in the workspace, expand the General item and select the Script/text editors. As you can see, in the first two sections of this window, you can set the font, font size, and font colors.

Based on syntax, lines are indented to give you a better picture of the program. You can use tab spacing to define the depth of indentation.

So, if you scroll down within the Script/text editors window, you can set the tab spacing. Also, you can choose the Indent among one of the three existing options: None, Paragraph, and Smart.

Additionally, you can show or hide the line numbers to the left of the program code. To do so, you can check or uncheck the Show line numbers box accordingly.

Conclusion

By reading this tutorial, you are now able to answer the following question:

  • Why is it vital to learn SCL programming?
  • Does the SCL language supported by the majority of Siemens PLCs?
  • What is the SCL definition?
  • What is SCL stands for?
  • What do the other PLC manufacturers call SCL in their programming software?
  • What is the difference between Structured Control Language and Ladder Diagram?
  • How do we create an SCL programming block in the Siemens TIA Portal software?
  • How can we customize the SCL programming window?

The skills you gain in SCL programming will allow you to build and test efficient, reliable PLC logic for various applications and tasks.