0% found this document useful (0 votes)
111 views

Lecture 1. Recap On PLC Programming

This document provides an overview of PLC programming using IEC 61131-3 standards. It discusses the key features of IEC 61131-3 including its software model and operation principle. It also summarizes the main programming languages in IEC 61131-3 - Structured Text (ST), Function Block Diagram (FBD), Sequential Function Chart (SFC), and Ladder Diagram (LD). Examples of basic ST programming constructs like variables, conditional statements, and loops are also provided.

Uploaded by

Longphi Game
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views

Lecture 1. Recap On PLC Programming

This document provides an overview of PLC programming using IEC 61131-3 standards. It discusses the key features of IEC 61131-3 including its software model and operation principle. It also summarizes the main programming languages in IEC 61131-3 - Structured Text (ST), Function Block Diagram (FBD), Sequential Function Chart (SFC), and Ladder Diagram (LD). Examples of basic ST programming constructs like variables, conditional statements, and loops are also provided.

Uploaded by

Longphi Game
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

School of Electrical Engineering

Department of Electrical Engineering and Automation


ELEC 8102 Design of Distributed Intelligent Automation Systems

Lecture 1.
PLC Programming Re-cap

Valeriy Vyatkin, Udayanto Dwi Atmojo

2022
Plan

● IEC 61131-3 Standard


o Key features
o Software Model
o Basic operation sequence

● IEC 61131-3 PLC programming languages:


o Structured Text (ST)
o Function Block Diagram (FBD)
o Sequential Function Chart (SFC)
o Ladder Diagram (LD)

● Note: Lab1 PLC programming

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

Device = Resource = PLC

Figure. Structured Project in CoDeSys

• This total structure of a PLC software project is called as the


software configuration
Programming Organization Units (POU)
● Each POU has a defined interface with inputs and outputs
● For the purpose of modularization and structuring a well-defined portion of the
program
● may be called and executed several times.
● Functions (FUN)
o ADD, SQRT, SIN, COS, GT, MIN, MAX, AND, OR, etc.
● Function Blocks (FB)
o Standard, vender supplied, and user defined
● Programs (PRL)

06/09/2022
11
PLC operation principle – Cyclic execution !!!

Read Inputs Task 1: 20ms

CODE
exe cycle ---------- Task 2: 100ms
----------
----------

Task 3: 200ms
Set Outputs

Tasks with different


Figure 1 illustrates the operation principle of the cyclic PLC cycle rates
POU execution order

• 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

effects the input value for the next cycle

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)

• Global variable is needed when


• It is used in more than one POU as a shared variable in data exchange
• It is a physical in/output address
• it will be monitored by HMI or used in visualization
• Local variable can be used when
• It is used only in the POU where it is declared
• It is used as a local state variable

• 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

● The program code is composed of statements


separated by semicolons; and containing
expressions
● Numerous constructs can be used for
programming e.g. different loops, thus allowing
the development of complex algorithms

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

IF Switch1 = True THEN IF Switch1 = True THEN


Switch1 := False; Switch1 := False;
ELSE ELSIF PumpOn1 = True THEN
Switch1 := True; PumpOn1 = False;
END_IF; END_IF;

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.

//Searching a value from a data array:


i := StartValue;
WHILE Data[i] <> x DO
i := i + 1;
END_WHILE;
Index := i;

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:

TON: Timer On Delay FB

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

• Variables can be declared as:


• INPUT,
OUTPUT,
• IN_OUT,
EXTERNAL, and TEMP

31
Function Block Definition Example

FUNCTION_BLOCK HYSTERESIS
VAR_INPUT
Hysteresis
XIN1, XIN2 : REAL;
REAL XIN1 Q BOOL EPS : REAL; (* Hysterisis band *)

REAL XIN2 END_VAR


VAR_OUTPUT
REAL EPS Q : BOOL := 0
END_VAR
IF Q THEN
IF XIN1 < (XIN2-EPS) THEN
Q
Q := 0 (* XIN1 decreasing *)
1 END_IF;
ELSIF XIN1 > (XIN2 + EPS ) THEN
0 Q := 1; (* XIN1 increasing *)
END_IF;
EPS EPS END_FUNCTION_BLOCK
XIN2
FB: Timers Pulse (TP) Timing
● Three timer instructions in IEC 61131 IN
TP
o TP - Pulse timer Q
o TON - Timer On Delay ET
PT
|
0
o TOF - Timer Off Delay
● Timer operations On-Delay (TON) Timing
o IN = Input condition IN
TON
o Q = Comparison output results; varies with Q
timer types PT
ET |
o PT = Preset Time 0

o ET = Elapsed Time
Off-Delay (TOF) Timing
Pump_Tmr IN
TOF
TON
Q
IN Q PT
ET |
0
T#200ms PT ET 178

Pulse width time Delay time

06/09/2022
33
Sequential Function Chart (SFC)

06/09/2022
34
Sequential Function Chart (SFC)

• Stems from the Grafcet language


introduced by the company
Telemecanique (now a part of
Schneider Electric)
• Powerful graphical technique for Step 1 N FILL
describing the sequential behaviour Transition 1
of a control program
Step 2 S Empty
• Used to partition a control problem
• Shows overview, also suitable for Transition 2
rapid diagnostics
Step 3
• The basic elements are STEPS with
ACTION BLOCKS and TRANSITIONS
• Support for alternative and parallel
sequences

35
SFC in CoDeSys

Steps can be directly programmed in other PLC languages.


Steps can have three “implicit” associated actions for entry, exit and
main activity.
To create the “implicit” action Step_active just double click on the new
step and select the language.

06/09/2022
36
SFC in CoDeSys – Actions
• In CoDeSys: There are two main types of actions:
A. Step Actions
B. IEC Actions

• Difference between IEC actions and step actions:


• See Codesys help pages:
• https://help.codesys.com/api-
content/2/codesys/3.5.12.0/en/_cds_sfc_element_action/

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.

R0 overriding The action is deactivated.


Reset
S0 Set (Stored) This action is executed as soon as the step is active. The action is
executed until it receives a reset, even if the step has already been
Step 1 N FILL deactivated.
L time Limited This action is executed as soon as the step is active. The action is
executed until the step is deactivated or the given time interval has
Transition 1 elapsed.
D time Delayed Execution of this action begins only after the given delay time has elapsed
following step activation and the step is still active. The action is executed
until the step is deactivated.
Step 2 S Empty
P Pulse This action is executed one time as soon 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

• Each step has an implicit step timer


• The step timer is started when the step becomes active
• The value of timer can be used in the following transition guard
expression
• StepName.t > T#10s
Simulation..

Step active until


timer value > 5s.

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

Active step in alternative branches Active steps in parallel branches


during simulation during simulation

06/09/2022
44
SFC - Alternative Branch with Step (1)

• Add a new branch


• The new branch will be a parallel branch
• Change it to an alternative branch
• (Continue…)
SFC - Alternative Branch with Step (2)

• Delete old transitions


• Add four new transitions
• Alternative branch with a step is now ready
SFC - Alternative Branch with Jump

• Start from an alternative branch with a step


• Add a jump into one branch
• Delete the step under the jump
• Alternative branch with a jump is now ready
Ladder Diagram (LD)

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 BOUT


IF A = TRUE THEN
B := TRUE;
ELSE
B := FALSE; TRUE TRUE
END_IF;
LD: Basic Boolean Logic – Negative Contact

AIN TRUE BOUT FALSE


• Boolean input variable: AIN (negative contact)
• Boolean output variable: BOUT (positive coil)

• 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

IF ((A OR B) AND (NOT C) AND D) THEN

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

● Function blocks (FB) can be used in a ladder circuits


● For example: EN
EQ
ENO

● Comparison FBs Tank1_Level


100.000 IN1
Tank_max IN2
● Timer FBs 78.251
Pump_Tmr
● Counter FBs
TON
IN Q

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

You might also like