CoDeSys Version 3.5 Textbook Version 1.1
CoDeSys Version 3.5 Textbook Version 1.1
Textbook
Festo Didactic
Version Control Record
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
3
Table of Contents
Table of Contents
Table of Contents ....................................................................................3
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
4
Table of Contents
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
5
Table of Contents
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
7
Table of Contents
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
8
Table of Contents
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
9
Introduction
Chapter 1
Introduction
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
10
Introduction
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
11
Introduction
1.0 Introduction
CoDeSys is a comprehensive software tool for industrial automation. It
is a programming standard for controllers and PLCs as per IEC 61131-3
CoDeSys (Controller Development System). It consists of two basic
components:
The run time system turns any system into an IEC 61131-3 controller
that can be programmed with CoDeSys. Integrated compilers ensure
that the program code is processed at maximum speed.
And the best thing about it: you can download CoDeSys for free, once
you have registered.
Email: info@3s-software.com
Internet: www.3s-software.com
1.3 Download
To download, go to http://www.3s-software.com/
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
12
Introduction
This will take you to the download page. Here you first need to enter
your registration. If you already have a registration code, then you can
click on the button “Enter download area”. (Continue with1.5 Select
CoDeSys version).
1.4 Registration
If you do not have a registration code, go to:
http://www.3s-software.com/index.shtml?en_contact
(authorization required)
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
13
Introduction
However, with version 3.5, you are able to convert the files written in
version 2.3 but again as earlier stated, the devices might not be able
to handle the new version.
The file that you download contains all of the necessary files and
documentation that you will need.
Click on “Support/Downloads”
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
15
Introduction
Double-click on “Setup_CoDeSysV3.exe”
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
16
Introduction
Click “Next”
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
17
Introduction
Click “Next”
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
18
Introduction
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
19
Introduction
If the process takes too long, click “Cancel” but do not exit the
setup.
The screen will be updated.
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
20
Introduction
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
21
Introduction
Click “Next”
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
22
Introduction
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
23
Introduction
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
24
Introduction
Interaction with PLC via Gateway, Scada, NET and C++ applications.
Project
Global
Declaration variables
Code Libraries
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
25
Introduction
Data types: this entails defining the various data types that are used in
the project. The user can define their own data types, structures and
enumerations (self-defined data types).
1.8.3 Visualisation
HMI: this is where the various visualization screens are composed and
saved.
1.8.4 Resources
The resources include the Global variables and the available libraries.
Likewise tools for monitoring and graphical display of variables.
POUs
Data types
Visualization
Resources
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
26
Introduction
All other programs, functions and function blocks are called from
PLC_PRG.
Textual languages:
Instruction List (IL)
Structured Text (ST)
Graphical languages:
Sequential Function Chart (SFC)
Function Block Diagram (FBD)
Ladder diagram (LD)
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
27
First Steps
Chapter 2
First Steps
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
28
First Steps
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
29
First Steps
When either one of two inputs and together with the third input is
activated, the output is switched on.
Before starting the new project, you might want to change some
settings.
The following are just recommended settings; you need not set
them if you wish.
Go to “Tools”, “Options”
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
31
First Steps
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
32
First Steps
Click “OK”
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
33
First Steps
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
34
First Steps
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
35
First Steps
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
36
First Steps
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
37
First Steps
Click “Next”
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
38
First Steps
Click “Finish”
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
39
First Steps
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
42
First Steps
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
43
First Steps
A new window will appear on the right; you would also be able to
see a tab with the “Digital Inputs” heading.
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
44
First Steps
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
45
First Steps
Example: %IX100.0
Byte. Bit
X: Bit
B: Byte
W: Word
I: Input
Q: Output
M: Memory
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
47
First Steps
The new library will be added and you should be able to see what it
contains at the bottom left window.
Right-click on “Application”
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
48
First Steps
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
49
First Steps
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
50
First Steps
At the symbols you will see question marks; you are required to
allocate the relevant variables.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
51
First Steps
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
52
First Steps
Anytime during the writing of the program, you can change to another
language.
Ladder Diagram
Function Block Diagram
Instruction List
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
53
First Steps
Click “FBD/LD/IL”
Click “View”
Choose the language you want to view your program.
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
54
First Steps
Click on the “Build” in the menu bar and then click “Build” or press
“F11”.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
55
First Steps
Click “Online”
Click “Scan Festo devices”
After scanning, you would be able to see the PLC with its IP address.
Take note of the address.
If you wish, you can change the IP address of the PLC.
This is to make sure that if there are more than one PLC in the
network, they have the same first three numbers and a different last
number.
Click “OK”
Now, you must change the computer network IP address for your PC
from your control panel, network settings.
This is done in your PC depending on the version of Windows you are
using.
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
56
First Steps
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
57
First Steps
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
58
First Steps
Key in the IP address for the PC based on the one from the PLC,
remember that the last number should be different.
Click “OK”
Then close all windows.
Before downloading the program, we need to set the task. This will tell
the software which program to download to the hardware.
Right-click on “Application”
Click “Add Object”
Click “Task Configuration”
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
59
First Steps
Click “Open”
Click “Add”
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
60
First Steps
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
61
First Steps
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
62
First Steps
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
63
First Steps
Click “Online”
Click “Login”
If there is an existing program in the PLC, click “Yes” to overwrite
the existing program.
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
64
First Steps
When the program is running, you might want to monitor the IOs.
Click on “Debug”
Click “Write values”
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
65
First Steps
Click “Run”
When you run the program, you would be able to see the contacts
change colour.
To logout, go to “Online”
Click “Logout”
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
66
First Steps
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
67
Structured Text
Chapter 3
Structured Text
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
68
Structured Text
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
69
Structured Text
To illustrate the use of the CoDeSys software, we will program the PLC
using the “Garage Door” example. For this first part, we will be using
the “Structured text” programming language.
Pressing the “Button Up” inside the garage with the “Limit Switch Up”
is disengaged.
Pressing the “Button Up” outside the garage with the “Limit Switch
Up” disengaged and the “Key Operator Switch” on.
The inputs:
Limit switches:
Input0_0 Limit switch up NC
Input0_1 Limit switch down NC
Buttons inside
Input0_2 Button up
Input0_3 Button down
Buttons outside
Input0_4 Key-operator switch
Input0_5 Button up
Input0_6 Button down
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
70
Structured Text
The outputs:
The name “Input0_0, Input0_1” does not explain what the input
means. Therefore I would like to change the name in the global
variables list.
Limit switches:
I0_0_LimitUp
I0_1_LimitDown
Buttons inside
I0_2_InButtonUp
I0_3_InButtonDown
Buttons outside
I0_4_SwitchEnableOut
I0_5_OutButtonUp
I0_6_OutButtonDown
Outputs
O0_0_DoorUp
O0_1_DoorDown
Add in the symbolic name in the variables list.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
71
Structured Text
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
73
The Ladder Diagram
Language (LD)
Chapter 4
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
74
The Ladder Diagram
Language (LD)
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
75
The Ladder Diagram
Language (LD)
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
76
The Ladder Diagram
Language (LD)
An “New POU” window will pop-up and you can enter the required
details.
Give your new POU a name.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
77
The Ladder Diagram
Language (LD)
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
78
The Ladder Diagram
Language (LD)
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
79
The Function Block
Diagram (FBD)
Chapter 5
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
80
The Function Block
Diagram (FBD)
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
81
The Function Block
Diagram (FBD)
Function
Inputs
A function is processed from the top down and from left to right. Each
successive function is only processed after the preceding function has
been processed to completion.
When attaching variables in the editor, you can position the cursor on
a “link” and enter a variable name. Question marks do not always
appear behind the link.
When using the FBD, we will be working with a different set of editor
functions from the ones used for Ladder Diagram programming.
To insert a block, you can click on the icon to open the “Input
Assistant”
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
83
The Function Block
Diagram (FBD)
Choose the category and then choose the function block you want.
Click “OK”
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
84
The Function Block
Diagram (FBD)
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
85
Using Memories
Chapter 6
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
86
Using Memories
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
87
Using Memories
NOTE!
IL is compiled line by line, and does not comply with the Boolean order
of operations BNAO (Brackets, Not, And, Or).
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
88
Using Memories
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
89
Using Memories
Chapter 7
Using Memories
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
90
Using Memories
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
91
Using Memories
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
92
Using Memories
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
93
Using Timers
Chapter 8
Using Timers
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
94
Using Timers
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
95
Using Timers
8. Using Timers
8.1 Description of the Example
In the following example we are going to use a timer. For this we use
the “Function Block Diagram” computer language.
What’s the idea behind the door example program: after pushing the
button “UP”, we will have to wait some time before the door opens.
On a rising edge of input IN, the timer output Q goes True and the
timer starts. After the preset time, the timer output Q goes False.
Once input IN has become False and the time has elapsed, the pulse
can be used again.
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
96
Using Timers
The timer starts on a rising edge of input IN. After the preset time, the
timer output Q goes True. If input IN goes False during the timer
duration, then the timer stops. If input IN goes True again, then the
timer restarts.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
97
Using Timers
On a rising edge of input IN, the timer output Q goes True; once the
input is again False, the timer starts to run. If input IN becomes True
again before the preset time has elapsed, then output Q stays True.
The timer will restart only when the input IN has again become False.
After the preset time, the timer output Q goes False.
We learnt how the TON timer is working. Now we are going to use it in
our example.
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
98
Using Timers
Click at the point just after the last “AND” function block before the
output.
Name the block “T1” or any other name you wish and give the
preset time to “T#3s”. This is based on the IEC 61131-3
convention.
The preset time (PT) of the timer = the execution time of the timer.
The time interval (ET); this is an option. When does the timer run?
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
99
Using Timers
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
101
Using Counters
Chapter 9
Using Counters
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
103
Using Counters
9. Using Counters
9.1 Description of the Example
We are going to make a new program that uses a counter. There is a
lamp, which indicates that the garage is full. We also have an input,
which resets the counter.
The inputs CU and RESET and the output Q are of type BOOL. Input
PV and output CV are of type WORD
When the input RESET becomes True (1), the value 0 will be placed
in CV.
When a rising edge is observed on CU, CV is incremented by 1
(one). As soon as CV is greater than or equal to the value of PV,
output Q will become True (1).
The inputs CD and LOAD and the output Q are of type BOOL. Input
PV and output CV are of type WORD
When the input LOAD becomes True (1), the value of PV will be
placed in CV.
When a rising edge is observed on CD, CV is decremented by 1
(one). As soon as CV reaches the value 0, output Q will become
True (1).
The value of CV cannot become less than 0.
LOAD is the input with that causes the counter to be loaded with the
specified value. The counter will be continuously kept at the specified
value for as long as this input is True. This forces the output CV (the
number of pulses still to be counted) to be kept equal to the preset
value PV. In order to use the counter, the LOAD input must therefore
be False.
So, to use this counter, both the input RESET and the input LOAD must
be False.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
107
The Sequential Function
Chart (SFC)
Chapter 10
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
109
The Sequential Function
Chart (SFC)
Our example with the garage door, which was used for the other four
languages, is of purely logical nature and does not include any
sequences. Thus a new task should be selected for the first SFC
example: The Bedside Table Lamp Controller.
Have you seen such a lamp? The lamp can be switched on and back off
again with a single button. Button depressed: Lamp on Button
released: Lamp remains on Button depressed: Lamp off Button
released: Lamp remains off.
The SFC elements enable you to split control programs up into sets of
actions and conditions (transitions).
Close inlet
4
Heating on
65°C
Heating off
5
Stir 8 min
Stop stirring
6
Open outlet
Vessel empty
This is part of a SFC; the steps are processed from the top down. An
action lasts until the transition condition becomes “True” (1). Then the
next action is executed.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
111
The Sequential Function
Chart (SFC)
10.2.1 Step
You can assign one or more actions to an IEC step. Each action can
consist of a number of Boolean variables.
The action “Clamp” is linked to this step. If you link multiple actions to
a single step, then they will be processed in the order in which they
are linked.
You may link a maximum of nine (9) actions to a single IEC step.
This type of step action will be processed as soon as the step has
become active and before the "step active" action.
The action is associated to a step via an entry in the "step entry" field
of the step. It is represented by an "E" in the lower left corner of the
respective step box.
This type of step action will be processed when the step has become
active and after a possible "step entry" action of this step has been
processed. However in contrast to an IEC step action it is not executed
once more when it gets deactivated and it cannot get assigned
qualifiers.
The action is associated to a step via an entry in the "step active" field
of the step. It is represented by a small triangle in the upper right
corner of the respective step box.
An exit action will be executed once when the step has got
deactivated. Notice however that this execution will not be done in the
same, but at the beginning of the subsequent cycle.
The action is associated to a step via an entry in the "step exit" field of
the step. It is represented by an "X" in the lower right corner of the
respective step box.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
113
The Sequential Function
Chart (SFC)
Syntax:
or just
<transition condition>;
(for example a=100)
Once a POU has been called, the action with the double frame, the Init
step, is executed first. A step in which the action is being executed is
referred to as an active step. In online mode, active steps are
displayed in blue.
The next step becomes active as soon as the condition preceding this
step becomes “True” (1). The actions linked to this step are executed
in the following cycle.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
115
The Sequential Function
Chart (SFC)
IEC step actions get associated to a step via the “Insert action
association” command. One or multiple actions can be associated to a
step. The position of the new action depends on the current cursor
position and the command. The actions must be available in the
project and be inserted with a unique action name
No more than nine (9) actions may be linked to an IEC step! IEC actions
are not linked to a specific step, but are stored with the program as
separate modules. This enables them to be re-used without restriction
throughout all steps in the program.
When called, the deactivation is first executed, and then the active
actions are processed each time in alphabetic order.
There are different action qualifiers available which are shown in the
following table:
Action
Description Functionality
Qualifier
N Non-stored The action is active as long as the step is active
10.3 Branches
Steps are processed from top down and from left to right.
The programmer must ensure that condition B and the condition D are
mutually exclusive (totally different). The first condition from left that
is “True” is used.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
117
The Sequential Function
Chart (SFC)
Condition_A
Condition_B Condition_D
6 16
Condition_C Condition_E
7 17
Condition_F Condition_G
10
Condition_A
Condition_B
6 16
Condition_C Condition_E
7 17
Condition_G
10
When step 7 and step 17 are active and condition G is “True”, step 7
and step 17 become inactive, and step 10 becomes active.
10.3.3 Jumps
Jumps can be made using the JUMP function. The name of the
destination step is placed below the JUMP. You can jump either
forwards or backwards.
Every SFC ends with a jump. Normally the SFC ends with a jump back
to “Init”.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
119
The Sequential Function
Chart (SFC)
You will get the SFC left window in the main page.
Once the push button input is pressed, the lamp output will switch on
and then the push button is pressed again, the lamp will switch off
and this will toggle.
Highlight the transition where you wish to add in another step and
transition.
Click on the icon “Step Transition (after)” or right-click and choose
the same option.
You can also click on the icon to insert the action association.
Click in the identifier box and type in the command; a drop down
menu will appear with the choices available.
Choose the one you want.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
125
The Sequential Function
Chart (SFC)
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
127
Running Multiple
Programs
Chapter 11
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
129
Running Multiple
Programs
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
131
Running Multiple
Programs
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
133
Running Multiple
Programs
After writing the program, you will need to add in this program in
the “Main Task”
When you start the software, both programs will run concurrently.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
135
Using the Simulator
Chapter 12
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
137
Using the Simulator
There would be three inputs and one output. Only when two of the
three inputs are activated will the output be switched on. When all
three inputs are activated, the output should not switch on.
If the command is called from the “Online” menu, the currently active
application will be running.
Click “Login”
Choose “Yes”
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
139
Using the Simulator
After successful login the icon beside the device icon indicates the
simulation mode. You can use the respective online commands to test
the application.
To switch off the simulation mode, first log out and then again perform
command "Simulation". The checkmark in front of the command will
disappear, the target entry in the device tree again will be displayed in
normal letters and you can log in to a real device.
However not only device objects are managed in the device tree, but
all objects which are needed to run an “Application” on a device
(controller, PLC); thus also the “Application” objects as well as “Task
Configuration” and “Task” objects.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
141
Using the Simulator
See below for the rules for inserting objects in the device tree.
Each entry in the device tree shows the symbol, the symbolic name
(editable) and the device type (=device name as provided by the device
description)
: Device is running
: Device is not running / Device is not configured / Device has an
error/ Device is in “simulation mode”.
: Device is running, diagnostic information available
: Device is configured but not running
: PLC is in 'stop' and 'Update IO while in stop' is not set
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
143
Using the Simulator
When you open the respective components, you will see the last
opened screen. To see your program running it would be good to have
all three screens visible.
To do this, click on the tab and drag it down to the main screen, a
navigation button will appear.
If you are unable to see the whole program, you can click on the
zoom icon at the bottom right.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
145
Using the Simulator
Once you have decided, you can choose one of the following
options from the “Debug” menu or by pressing the shortcut key:
o Write values – [Ctrl] + [F7]
o Force values – [F7]
o Unforce values – [Alt] + [F7]
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
147
Using the Simulator
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
149
Using the Simulator
A new window will open with the visualisation toolbox on the right.
For the push buttons, we will use the “round rectangle” feature.
Create 3 buttons; you can both create from the toolbox or copy and
paste from the first button.
We will first change the colour; click on the [+] next to the word
“Colors” and it will expand.
The two options are; “Normalstate” and “Alarmstate’.
Click on the box next to “Fillcolor”
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
151
Using the Simulator
The colours you choose will highlight when the input is activated
or not.
Next, we choose the “Text” property.
Type in the name you want to see on the drawn element.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
153
Using the Simulator
Next, click on “text properties”; here you can set the font,
alignment, colour of the text.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
155
Using the Simulator
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
157
Using the Simulator
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
159
Functions and Function
Blocks
Chapter 13
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
161
Functions and Function
Blocks
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
163
Functions and Function
Blocks
EQ Comparison: =
GE Comparison: =
Comparison GT Comparison:
Functions LE Comparison: =
LT Comparison:
NE Comparison:
*_TO_STRING Type conversion to STRING
SR Set Dominant
Bistable Function Blocks
RS Reset Dominant
CTU Up Counter
TP Pulse
The AND function results in a logical 1 at the output when all inputs
are logical 1.
Inputs Output
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
165
Functions and Function
Blocks
13.4.2 OR Function
Inputs Output
Output
Inputs
This function enables you to invert the status of a bit. If the bit is a
logical 1, then it becomes a logical 0, and vice versa.
Input Output
If input SET to this function block goes True, then output Q1 will also
go True.
If input RESET1 to this function block goes True when input SET is
False, then output Q1 will go False.
When input SET and input RESET1 are both True, then output Q1 is
False. Output Q1 can be made True using input SET only when input
RESET1 is False.
If input SET to this function block goes True, then output Q1 will also
go True.
If input RESET1 to this function block goes True when input SET is
False, then output Q1 will go False.
When input SET1 and input RESET are both True, then output Q1 is
True. Output Q1 can be made False using input RESET only when input
SET1 is False.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
167
Functions and Function
Blocks
The inputs CD and LOAD and the output Q are of type BOOL. Input PV
and output CV are of type WORD
When the input LOAD becomes True (1), the value of PV will be placed
in CV. When a rising edge is observed on CD, CV is decremented by 1
(one). As soon as CV reaches the value 0, output Q will become True
(1).
The inputs CU and RESET and the output Q are of type BOOL. Input PV
and output CV are of type WORD
When the input RESET becomes True (1), the value 0 will be placed in
CV. When a rising edge is observed on CU, CV is incremented by 1
(one). As soon as CV is greater than or equal to the value of PV, output
Q will become True (1).
RESET is the input that resets the counter. The counter will be
continuously reset for as long as this input is True. This will keep the
output CV (the number of counted pulses) to 0. In order to use the
counter, the RESET input must therefore be False.
Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1
168
Structured Text
Instructions
LOAD is the input with that causes the counter to be loaded with the
specified value. The counter will be continuously kept at the specified
value for as long as this input is True. This forces the output CV (the
number of pulses still to be counted) to be kept equal to the preset
value PV. In order to use the counter, the LOAD input must therefore
be False.
So, to use this counter, both the input RESET and the input LOAD must
be False. PV is the number of pulses to be counted, specified as an
integer.
QU is the output from the counter which is True when the number of
counted pulses (CV) is greater than or equal to the number of pulses
to be counted (PV).
QD is the output from the counter which is True when the number of
pulses still to be counted (CV) is equal to 0.
Input Preset Date and Time (PDT) must be loaded on a rising edge of
the Boolean input EN. The Boolean output Q is a copy of the Boolean
input EN.
When EN becomes True (1), the date and time are taken over from PDT
and the clock starts to run. The clock continues to run as long as EN is
True (1). The moment EN becomes False (0, the current date and time
CDT becomes the default value of 1970-01-01-00:00:00
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
169
Functions and Function
Blocks
If no value is supplied for PDT and for as long as EN is True (1), the
function block will return the current date and time in CDT.
On a rising edge of input IN, the timer output Q goes True; once the
input is again False, the timer starts to run. If input IN becomes True
again before the preset time has elapsed, then output Q stays True.
The timer will restart only when the input IN has again become False.
After the preset time, the timer output Q goes False.
The timer starts on a rising edge of input IN. After the preset time, the
timer output Q goes True. If input IN goes False during the timer
duration, then the timer stops. If input IN goes True again, then the
timer restarts.
On a rising edge of input IN, the timer output Q goes True and the
timer starts. After the preset time, the timer output Q goes False.
Once input IN has become False and the time has elapsed, the pulse
can be used again.
When the input CLK goes from True to False, output Q gives a short
pulse. To visualize this, a delayed turn-off is placed here immediately
behind the F_TRIG function block.
When the input CLK goes from False to True, output Q gives a short
pulse. To visualize this, a delayed turn-off is placed here immediately
behind the R_TRIG function block.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
171
Structured Text
Instructions
Chapter 14
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
173
Structured Text
Instructions
14.1 Instructions
The table below shows the operations in order of priority
Exponent A**B
EXPT(A,B) AB
Negation -
Complement NOT
Multiply *
Divide /
Modulo MOD
Add +
Subtract -
Equality =
Inequality <>
14.1.1 assignment, :=
Example:
(* Main ST program *)
(* Calculates an analog value from three binary signals *)
Subprogram Calc.
In the Calc subprogram, the variables in0, in1 and in2 are defined as
input parameters.
And Temporary is a variable used to store an intermediate result.
Calcprog:
LD in2 (* Input 2 *)
BOOL_TO_INT (* result := 1 or 0 of in2 *)
MUL 4 (* result := 4 x INT of in2 *)
ST Temporary (* store the intermediate result *)
LD in1 (* Input 1 *)
BOOL_TO_INT (* result := 1 or 0 of in1 *)
MUL 2 (* result := 2 x INT of in1 *)
ADD Temporary (* result := 4 x INT of in2 + 2 x INT of in1 *)
ST Temporary (* store the intermediate result *)
LD in0 (* Input 0 *)
BOOL_TO_INT (* result := 1 or 0 of in0 *)
ADD Temporary (* result := 4 x ana (in2) +2 x ana (in1) + ana (in0) *)
ST Calc (* send result back to calling program *)
We can call a function block. To do this, we use the name of the called
function block, followed by the transferred parameters within
parentheses
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
175
Structured Text
Instructions
The result is returned with the function block name followed by a full
stop and the returned parameter.
Example:
If not all parameters are specified, then the function block will use the
“old” value or the default value of the unspecified parameter.
Example 1:
Example 2:
However, if this example was executed first of all, then the result
would be 11 + 0 = 11. This is because the default value (0) then gets
used for the unspecified parameter.
Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1
176
Structured Text
Instructions
(* Count: = is the input for the counter, Q_Count is the counter output
*)
IF NOT Count THEN
RETURN;
ELSE Q_Count := Q_Count + 1;
END_IF
In this instruction, the THEN clause is executed when the result of the
IF is True. When the result is False, then the ELSIF is executed, with the
THEN clause executed when the result of the ELSIF is True. When the
ELSIF is False, then the ELSE is executed.
Whenever the ELSIF clause is not included, then a False result from the
IF will cause immediate execution of the ELSE.
(* only an IF – THEN *)
IF max_level THEN
Alarm: = True;
END_IF
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
177
Structured Text
Instructions
CASE error_code OR
255: error_message := ‘Divide by zero’;
system_stop: = True;
1: error_message: = ‘level too high’;
2.3: error_message: = ‘wrong option’;
ELSE error_message: = ‘unknown error’;
END_CASE
The value of the FOR loop variable is tested at the start of the loop. By
default, the variable is incremented by 1 each time the loop is
processed. We can influence this by supplying a BY value.
(* the following codes searches for the word “key” in a list of 100
words *)
(* the variable “position” stores the value of i when the word is found
*)
(* the loop ends when the value of i reaches 101 before the word
“key” is found *)
FOR i: = 1 TO 100 DO
IF word[i]: = ‘key’ THEN
Position: = i;
EXIT;
END_IF
END_FOR
Another possibility is to specify the step size with BY, for example
when we only want to test the even entries in the table. The number
after BY may be any value provided it is the same type as the variable
after the FOR instruction.
FOR i: = 0 TO 100 BY 2 DO
IF word[i]: = ‘key’ THEN
Position: = i;
EXIT;
END_IF
END_FOR
Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1
178
Structured Text
Instructions
In the following example code, the variable Edge is set to False. Each
time the door is opened, a counter is incremented. The program code
ensures that the variable Edge is reset to False each time the code is
processed.
Example:
Edge: = False;
Example:
A: = 0;
REPEAT
A: = A +3;
UNTIL A: = >= 39;
END_REPEAT
If the condition evaluates to True at the end of the loop, then the loop
is processed again. If the condition is False then the program leaves
the loop and continues with the next instruction.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
179
Structured Text
Instructions
14.1.13 OR Instruction
(* IN1 OR IN2*)
result: = IN1 OR IN2 OR … OR INn;
or
result: = OR(IN1,IN2,...INn);
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
181
Instruction List
Instructions
Chapter 15
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
183
Instruction List
Instructions
15.1 Instructions
15.1.1 Labels
Example:
Begin:
LD Start (*Start button*)
AND Stop (*Stop button*)
JMP Program (*Label Program*)
Program:
LD %IX5 (*Clamp in*)
ST %QX2 (*Clamp cylinder*)
15.1.2 Modifiers
The Load (LD) instruction loads a value into the current result.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
185
Instruction List
Instructions
Store_Boo:
LD False (* Boolean constant False (0) *)
ST Boo_var7 (* Boo_var7: = False (0) *)
STN Boo_var4 (* Boo_var4: = True (1) *)
Store_ana:
LD 156 (* Constant integer 156 *)
ST ana_var8 (* ana_var8: = 156 *)
Store_tmr:
LD t#9s (* Constant time of 9 seconds *)
ST tmr_var1 (* tmr_var1: = t#9s *)
The Set instruction sets the Boolean value True in a Boolean variable
when the result of the preceding operation is True.
Examples:
LD True (* Boolean constant True (1) *)
S Boo_var2 (* Boo_var2: = True (1) *)
The Reset (R) instruction sets the Boolean value False in a Boolean
variable when the result of the preceding operation is False. If the
previous operation evaluates to False, then nothing changes.
Examples:
(* Boo_var1:= True (1) *)
(* Boo_var2:= False (0) *)
LD True (* Boolean constant True (1) *)
R Boo_var1 (* Boo_var1 becomes False (0) *)
R Boo_var2 (* Boo_var2 remains False (0) *)
(* Boolean False not changed by R*)
Examples:
LD Boo_var1 (* Boolean variable 1*)
AND Boo_var2 (* Boolean variable 2 *)
ST Boo_var6 (* Boolean variable 6 *)
15.1.9 OR Instruction
Example:
LD Boo_var1 (* Boolean variable 1*)
OR Boo_var2 (* Boolean variable 2 *)
ST Boo_var6 (* Boolean variable 6 *)
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
187
Instruction List
Instructions
Example:
LD Boo_var1 (* Boolean variable 1*)
XOR Boo_var2 (* Boolean variable 2 *)
ST Boo_var6 (* Boolean variable 6 *)
Option:
LD Option (* Option can have the value 0, 1 or 2 *)
EQ 1 (* If the variable Option has the value 1 *)
JMPC Option1 (* If True, jump to label Option1 *)
Option1:
LD True (* Load the value True into memory *)
ST Option_1 (* Set output 1 on *)
JMP Block_1 (* Jump to program block 0 *)
Option2:
LD True (* Load the value True into memory *)
ST Option_2 (* Set output 2 on *)
JMP Block_2 (* Jump to program block 2 *)
Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1
188
Instruction List
Instructions
Block_0:
LD xxx (* Start of program block 0 *)
Block_1:
LD xxx (* Start of program block 1 *)
Block_2:
LD xxx (* Start of program block 2 *)
Mainprog:
On: (* Label On *)
CAL Control (* Start subprogram Control *)
JMP Off (* Jump to label Off *)
Subprogram Control:
Mainprog:
….
LD Option (* Load the contents of the variable option *)
CAL Decide (* Call subprogram Decide *)
ST Outcome (* Outcome: = result from Decide *)
Subprogram “decide”
Option:
LD Option (* Option can have the value 0 False or 1 True *)
JMPC Option1 (* If True, jump to label Option1 *)
JMP Option0 (* If False Jump to label Option0 *)
Option0:
LD True (* Load the value True *)
ST Option_0 (* Set output 0 on *)
RET (* End of the subprogram *)
Option1:
LD True (* Load the value True into memory *)
ST Option_1 (* Set output 1 on *)
RET (* End of the subprogram *)
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
191
Instruction List
Instructions
LD a1 (* result: = a1 *)
ADD( a2 (* deferred addition, result := a2 *)
MUL( a3 (* deferred multiplication, result := a3 *)
SUB a4 (* result: = a3 – a4 *)
) (* execute multiplication, *)
(* result: = a2 * (a3-a4) *)
MUL a5 (* result: = a2 * (a3-a4) * a5 *)
) (* execute deferred addition *)
(* result: = a1 + (a2 * (a3 – a4) * a5) *)
ADD a6 (* result: = a1 + (a2 * (a3 – a4) * a5) + a6 *)
ST Result (* Store the calculated result in Result *)
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
193
Ladder Diagram Symbols
Chapter 16
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
195
Ladder Diagram Symbols
16.1 Symbols
This symbol indicates that the corresponding input signal is tested for
a logical 1 (TRUE). If this is the case, then this condition is “true”.
This symbol indicates that the corresponding input signal is tested for
a logical 0 (FALSE). If this is the case, then this condition is “true”.
16.1.3 Coil
The coil here signifies not only the coil of a relay or solenoid switch,
but also a variable of 1 bit.
This means that the result of the conditions is negated. If the logical
result is a 1 (TRUE) then the output is a logical 0 (FALSE), and vice
versa.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
197
Ladder Diagram Symbols
This AND function box with ENable function is executed whenever the
input EN (enable) is TRUE. The previous result persists when the EN is
FALSE. If the output of the AND function is TRUE, and the EN
subsequently becomes FALSE, then the output of the AND function
remains TRUE. If the EN (enable) again becomes TRUE and one of the
inputs of the AND function is FALSE, then the output of the AND
function will also become FALSE.
Select AND and hit F2 to open the Input assistant, then you can select
from all other available functions. They are all equipped with an
ENable capability.