Lecture 1. Recap On PLC Programming
Lecture 1. Recap On PLC Programming
Lecture 1.
PLC Programming Re-cap
2022
Plan
2
PLC programming Re-Cap
3
Preface
This is a recap lecture on PLC programming. Students who have
taken the bachelor level course Automation 1 (or Automation
Systems 1) should be familiar with this content
06/09/2022
4
IEC 61131 Standard
● IEC 61131-1:2003:
o Programmable controllers - Part 1: General information
● IEC 61131-2:2007:
o Programmable controllers - Part 2: Equipment requirements and tests
● IEC 61131-3:2013:
o Programmable controllers - Part 3: Programming languages
● IEC TR 61131-4:2004:
o Programmable controllers - Part 4: User guidelines
● IEC 61131-5:2000:
o Programmable controllers - Part 5: Communications
● IEC 61131-6:2012:
o Programmable controllers - Part 6: Functional safety
● IEC 61131-7:2000:
o Programmable controllers - Part 7: Fuzzy control programming
06/09/2022
5
IEC 61131-3 Programming Languages
06/09/2022
6
Software Tools
• CoDeSys
• Software tool for developing and engineering IEC 61131-3 controller
applications
• a soft PLC from 3S-Smart Software Solutions GmbH
• TwinCAT
• OEM version of CoDeSys for Beckhoff, under Visual Studio Shell
• ISaGRAF
• KW
• Tools of SIEMENS, Rockwell, Schneider Electric, ABB…
06/09/2022
7
Key Features of IEC 61131-3
● Finalised in 1993 as a joint effort of representatives of the leading
automation companies: Siemens, Rockwell Automation, ABB,
Schneider Electric, etc.
● IEC 61131-3 is the most important automation language in industry.
● 80% of all PLCs support it, all new developments base on it.
Depending on the country, some languages are more popular.
● Structured software - through use of Configuration, Resource, and
Program Organization Units (POU)
● Software encapsulation - through use of POU, and complex data
types
● Strong Data Typing - through languages that restrict operations to
only apply to appropriate types of data
● Execution control - through use of tasks
06/09/2022
8
PLC Software Model & Operation Principle
IEC 61131-3
06/09/2022
9
PLC Software Model - Configuration
06/09/2022
11
PLC operation principle – Cyclic execution !!!
CODE
exe cycle ---------- Task 2: 100ms
----------
----------
Task 3: 200ms
Set Outputs
• At the beginning of each task cycle the current values of all the input
variables are read
• POU instructions and function blocks are executed from top to bottom
and left to right
execution order
instant effect on the following instruction
9/6/22 13
Periodic and Continuous Tasks with Priority
14
Task configuration in CoDeSys
Cycle time
Figure. In Codesys, double clicking on Main Task will bring up the Configuration tab.
Global and Local variables
• Variable declaration consists of:
• Variable name (Identifier) : Data type := Initial value (optional) ; Comment (optional)
• Note: Global variables enable simple data exchange and signaling between
different POUs of PLC applications
9/6/22 16
Global and Local variables
• Declare variables either globally in a Global Variable List or locally in the
declaration part of each POU
9/6/22 17
Structured Text (ST)
Text-based programming language
(See: ST_Basics.pdf)
06/09/2022
18
Structured Text - ST
● Structured Text (ST) is a textual high-level
programming language (like e.g. C)
● ST language can be used in:
● ST programs
● Function and Function block logic coding
● Action logic coding in SFC programs
06/09/2022
19
ST Basics
Each statement must end with a semi-colon (";")
Basic statements:
• Assignment - :=
- Q:=IN;
- Q:=sin(angle);
- Q := (IN1 + (IN2 / IN 3)) * IN4;
• Conditional statement
- IF / THEN / ELSE (simple binary switch)
- CASE (enumerated switch)
06/09/2022
21
ST: Conditional Statements
● IF / THEN / ELSE / ELSIF
06/09/2022
22
ST: Conditional Statements
● CASE statement Example
CONST
plus:= 1;
minus:= 2;
times:= 3;
END_CONST
CASE operator OF
plus: a := b + c;
minus: a := b - c;
times: a := a * b;
END_CASE;
06/09/2022
23
ST Basic
Loops:
• While
• Repeat
• For
06/09/2022
24
ST: WHILE Statement
The WHILE statement contains an expression whose truth value determines
whether or not the statement that follows DO is to be carried out again.
06/09/2022
25
ST: REPEAT Statement
The statement is carried out repeatedly until the expression takes on the value
TRUE. The statement is carried out at least once
j := -4;
REPEAT
j := j + 2;
UNTIL j > 60 END_REPEAT;
REPEAT
a := in1 + in2;
b := 2 * in1;
c := in1 * in2;
UNTIL EndCondition END_REPEAT;
06/09/2022
26
ST: FOR Statement
The FOR statement carries out a DO loop in which new values are assigned to a
variable (the controlled variable).
//Searching the maximum value in a data array:
Maximum := 0;
FOR lw := 2 TO 63 DO
IF Data[lw] > Maximum THEN;
Maximum := Data[lw];
MaxIdx := lw;
END_IF;
END_FOR;
06/09/2022
27
ST - Function Block Call Example:
06/09/2022
28
Function Block Diagram (FBD)
06/09/2022
29
IEC 61131-3 Function Blocks
• Function blocks (FB) are the main building blocks for structuring PLC
programs
• They are called by programs and FBs and can themselves call functions as well
as other FBs
• Function Block or a Function (FUN) are user made subroutines
• Instead of writing the same program code several times, it can be written once
and invoked as a block with new in-/out parameters
30
Function Blocks: Parameters
31
Function Block Definition Example
FUNCTION_BLOCK HYSTERESIS
VAR_INPUT
Hysteresis
XIN1, XIN2 : REAL;
REAL XIN1 Q BOOL EPS : REAL; (* Hysterisis band *)
o ET = Elapsed Time
Off-Delay (TOF) Timing
Pump_Tmr IN
TOF
TON
Q
IN Q PT
ET |
0
T#200ms PT ET 178
06/09/2022
33
Sequential Function Chart (SFC)
06/09/2022
34
Sequential Function Chart (SFC)
35
SFC in CoDeSys
06/09/2022
36
SFC in CoDeSys – Actions
• In CoDeSys: There are two main types of actions:
A. Step Actions
B. IEC Actions
06/09/2022
37
SFC in CoDeSys – A: Step Actions
• In CoDeSys: Step Action can be
created by dragging “a Action” from
the toolbar to the highlighted corner of
a step Active action
• Active action (i.e. main activity):
Continuous execution while the step
is active
• Entry action: executed once when the
step becomes active Entry action Exit action
• Exit action: executed once when the
step is exited (deactivated)
06/09/2022
38
SFC in CoDeSys – B: IEC Actions
• In CoDeSys: These actions comply with the IEC1131-3
standard.
• They are executed according to their qualifiers
• Each action box includes the qualifier and the action name
06/09/2022
39
SFC in CoDeSys – IEC Action Qualifiers
• Action qualifiers are used to define how
long the action of a step should be active
• Codesys example:
• S: PumpOn1 : Action PumpOn1 := TRUE (in
step: StartPump) is executed until it receives a
reset
• R: PumpOn1 : Action PumpOn1 is deactivated
(in step: StopPumps)
• N: Pressure_ACT: Activity is active as long as
the step is active (in step: CtrlPressure)
06/09/2022
40
SFC Action Qualifiers
N Non-stored The action is active as long as the step is active.
SD Stored and Execution of this action begins only after the given delay time has elapsed
Transition 2 time Delayed following step activation. The action is executed until it receives a reset.
DS Delayed and Execution of this action begins only after the given delay time has elapsed
Stored following step activation and the step is still active. The action is executed
Step 3 until it receives a reset.
SL Stored and This action is executed this action as soon as the step is active. The
time Limited action is executed until the given time has elapsed or it receives a reset.
06/09/2022
41
SFC in CoDeSys – Actions Example
A. Step Actions
B. IEC Actions
A: entry action
A: active action
B: active action
06/09/2022
42
SFC - Step Timer
06/09/2022
43
SFC in CoDeSys – Branches
• CoDeSys: two types of branches
• Alternative branches: Only one of the
branches is active at the time
• Parallel branches: Both branches are
active at the same time
06/09/2022
44
SFC - Alternative Branch with Step (1)
06/09/2022
48
Ladder Diagram (LD)
● Originated from the graphical representation of relay logic used to
design electrical control systems using relays.
06/09/2022
49
Ladder Diagram (LD)
● Originated from the graphical representation
of relay logic used to design electrical control AIN BOUT
systems using relays.
● LD was developed to make program creation
Contact Coil
and maintenance easier:
o Computer based graphical representation of wiring
diagrams that was easy to understand
o Reduced training and support cost
o Still widely used AIN BOUT
Contact Coil
06/09/2022
50
Recall: Electric Circuit and Ladder Diagram
51
Structure of Ladder Diagram
An LD is composed of a left power rail, a right power rail and a
number of rungs/links in between:
o The state of the link element shall be denoted “ON” (1) or “OFF” (0)
o The state of the left rail shall be considered ON at all times
o No state is defined for the right rail
Rung/Link
Left
Power Rail A D E
Right
Power Rail
Associated B
Branch
Boolean
Variable
06/09/2022
52
LD: Basic Boolean Logic - Positive Contact
AIN BOUT
• Boolean input variable: AIN (positive contact)
• Boolean output variable: BOUT (positive coil)
FALSE FALSE
• The same logic as a Structured Text ST program AIN FALSE BOUT TRUE
IF NOT A = TRUE THEN
B := TRUE;
ELSE
B := FALSE;
END_IF;
Ladder Diagram Execution
● Rungs of LD are solved from left to right and top to bottom
● Branches within rungs are solved top left to bottom right
1. Left to Right
A D E
B
2. Top to Bottom
F G H
P S
I J K
R
06/09/2022
55
Series and Parallel Operations
A C D E
B AND F
Branches
OR
E := 1;
F := 1;
ELSE
E := 0; AND: Input instructions in series
F := 0;
END_IF;
OR: Input instruction in parallel
06/09/2022
56
Standard Contacts
● Positive Contact: Normally Open Contact -| |-
o Enables the rung to the right of the instruction if the rung to the left is enabled and
the associated Boolean variable is TRUE/ON (1)
● Negative Contact: Normally Closed Contact -|/|-
o Enables the rung to the right of the instruction if the rung to the left is enabled and
the associated Boolean variable is FALSE/OFF (0)
● Positive transition contact -|P|-
o Enables the right side of the rung for one scan when the rung on left side of the
instruction is true and an OFFà ON transition of the associated variable is sensed
● Negative transition contact -|N|-
o Enables the right side of the rung for one scan when the rung on left side of the
instruction is true and an ONàOFF transition of the associated variable is sensed
06/09/2022
57
Standard Coils
● The referenced bit is reset when processor power is cycled
o Coil -( )-: sets a bit when the rung is true (1) and resets the bit when the
rung is false (0)
o Negative coil -( / )-: sets a bit when the rung is false (0) and resets the bit
when the rung is true (1)
o Set (latch) coil -(S)-: sets a bit (1) when the rung is true and does nothing
when the rung is false. The bit remains set until reset by a Reset coil.
o Reset (unlatch) Coil -(R)-: resets a bit (0) when the rung is true and does
nothing when the rung is false. The bit remains reset until set by a Set coil.
06/09/2022
58
Standard Coils
● Positive transition-sensing coil -(P)-
o Sets the bit (1) when rung to the left of the instruction transitions from OFF
(0) to ON (1)
o The bit is left in this state in current cycle
● Negative transition-sensing coil -(N)-
o Sets the bit (1) when rung to the left of the instruction transitions from ON
(1) to OFF (0)
o The bit is left in this state in current cycle
06/09/2022
59
Unlatched vs Latched coils
Unlatched coils:
A C D E IF ((A OR B) AND (NOT C) AND D) THEN
E := 1;
F := 1;
B AND F ELSE
E := 0;
F := 0;
Branches END_IF;
OR
Latched coils:
A C D E
IF ((A OR B) AND (NOT C) AND D) THEN
E := 1;
S F := 1;
B AND F
END_IF;
S
Branches
OR
(S like ‘Set’)
LD with Function Blocks - Comparison Instructions in LD
T#200ms PT ET 178
Load_Cnt
CTU
IN Q
200 PV CV 178
06/09/2022
61
Note: Computer Exercises: Lab1 PLC Programming
● Dates
o (Group1 Tue 17/09 (08:15-10:00); if needed)
o Group2 Tue 17/09 (10:15-12:00);
o Group3 Thu 19/09 (14:15-16:00);
o Group4 Thu 19/09 (16:15-18:00);
o Group5 Fri 20/09 (08:15-10:00);
o Venue
o In the PC class (AS5, TUAS)
● Remember to:
o Enroll on MyCourses for one of the weekly sessions
o See through the Codesys tutorials 1,2,3 and 5 (Youtube) before coming to
the labs
06/09/2022
62
Notes:
• See the following Codesys tutorials (Youtube) before coming to the
labs:
• Codesys 01 Intro and Ladder (for Lab1)
• Codesys 02 Structured Text
• Codesys 03 Function Block Diagram
• Codesys 05 Sequential Function Chart
06/09/2022
63
Notes: Codesys Help Pages
• Menu Help/Contents
• Reference, Programming pages:
References
• Introduction to Codesys:
• http://files.beijerelektronik.com.tr/downloads/Soft_Control/Dokumanlar/CoDeSys
_Intro.pdf
• Codesys intro and tutorials 1,2,3 and 5 in YouTube
• Codesys 01 Intro and Ladder
• https://www.youtube.com/watch?v=2tX6gumm2zg&t=2s
• Codesys 02 Structured Text
• https://www.youtube.com/watch?v=GP_3n8GgjOE
• Codesys 03 Function Block Diagram
• https://www.youtube.com/watch?v=lirafGE6GxI&t=37s
• Codesys 05 Sequential Function Chart
• https://www.youtube.com/watch?v=JP9_hhc1gRM
• Codesys Help Pages
• Basics of ST (ST_Basics.pdf in materials)
06/09/2022
65
End of Lecture