PLC Programming
PLC Programming
programming
7th. Lesson
MISCIT
Typically a PLC system has the basic functional components of processor
PLC system
unit, memory, power supply unit, input/output interface section,
communications interface and the programming device. Figure 1.4 shows
the basic arrangement.
Programming
device Program & data Communications
memory interface
Input Output
inter- Processor inter-
face face
Power supply
+
Input Input
module module
Input
Output load
(a) device (b)
+
Input Input Input
device module module
ystems There are two common types of mechanical design for PLC systems; a
single box, and the modular/rack types. The single box type (or, as
I/O Processing
Analogue, i.e. a signal whose size is related to
the size of the quanDty being sensed.
Discrete, i.e. essenDally just an ono signal.
Digital, i.e. a sequence of pulses.
Programs for microprocessor-based systems
have to be loaded into them in machine code,
this being a sequence of binary code numbers
to represent the program instrucDons.
Programming PLC
ladder diagram (LD)
instrucDon list (IL)
sequenDal funcDon chart (SFC)
structured text (ST)
funcDons block diagram (FBD)
Ladder diagrams Programming 81
with electrical power when the switch is closed. The circuit shown i
Figure 5.1(b) is termed a ladder diagram.
Switch L1 L2
L1 Motor M
d.c. input M Switch Motor
L2
Power rails
(a) (b)
With such a diagram the power supply for the circuits is always show
program being clearly denoted, and then promptly resumes at the start
(see Section 4.4). This procedure of going through all the rungs of the
program is termed a cycle. The end rung might be indicated by a
block with the word END or RET for return, since the program
promptly returns to its beginning.
Power flow
Left power rail Right power rail
Rung 1
Rung 2
Rung 3
Rung 4
Interconnection of horizontal
and vertical power flows
Output
(a)
Output
(b)
(a) (b)
(c) (d)
Figure 5.6 Notation: (a) Mitsubishi, (b) Siemens, (c) Allen-Bradley, (d)
Telemecanique
A B Input B
0 0 0 (a) Output
0 1 0
(b)
1 0 0
1 1 1 Figure 5.8 AND gate with a ladder diagram rung
A B 5.2.2 OR
Inputs
A Figure 5.9(a)Output
shows an electrical circuit where an output is
Logic
whengate
switch A or B, both normally open, are closed. This des
OR logic
control ANDgate (Figure 5.9(b)) in that input A or input B must
B there to be an output. The truth table is:
Applied voltage
(a) (b)
Inputs Output
A B (a) (b)
0 0 0 Input A
0 1 1
1 0 1 Input B
1 1 1
Output
(c)
A
Inputs
B
A Figure 5.10 OR gate
Output
Logic gate
control OR
B An example of an OR gate control system is a conveyor b
transporting bottled products to packaging where a deflector plate
(b) activated to deflect bottles into a reject bin if either the weight is
(a) Applied voltage
within certain tolerances or there is no cap on the bottle.
Figure 5.9 (a) OR electrical circuit, (b) OR logic gate
Input Output
A
0 1
1 0
A
Input A Output
(b)
Output
(c)
Figure 5.11 (a) NOT circuit, (b) NOT logic with a ladder rung, (c) high
A
NOT
A
AND NOT OR
B B
ogrammable Logic Controllers NOT
NAND
(a) (b)
A
NOT
A
Figure 5.12 NAND gate
AND NOT OR
Input Figure
A Input5.15
B shows
Outputa ladder diagram
Input A of a NOR system. When input A
and input B are both not activated, there is a 1 output. When either X400
or X401 are 1 there is a 0 output. Input B
Input B
Input A Input B
Output
Output
s With ladder diagrams, there can be more than one output connected to a
contact. Figure 5.20 shows a ladder program with two output coils. When
MulDple outputs
the input contacts close both the coils give outputs.
Input Output A
Input
Output B
For the ladder rung shown in Figure 5.21, output A occurs when input
A occurs. Output B only occurs when both input A and input B occur.
Input A Output A
Figure 5.20 Ladder rung with two outputs Input A
Input B
Output B
Output A
Input B Output B
Figure 5.21 Ladder rung with two inputs and two outputs
Sequenced outputs
output B is switched on. Finally, when input C is closed, output C is
switched on.
Input A Output A Input A Output A
X400 Y430 I0.0 Q2.0
(a) (b)
Input A
Input B
Input C
Output A
Output B
Output C
function block in the system shown above it, Timer1. Names of functio
block inputs are shown within the block at the appropriate input an
output points. Cross diagram connectors are used to indicate whe
Output
Inputs Function
Blocks with connections
3 Function block
Connectors AV_WEIGHT AV_WEIGHT
C 113-3 standard for drawing such blocks is shown in FigureAV_WEIGHT AV_WEIGHT
Logic gates
96 Programmable Logic Controllers
Input A Input A
Input A Input A
Output Output Input B
Input B & Input B &
Input C Input B
Input C Output
Output
AND function
Input A
Input A Input A Output
Input A Output
>1 Input B
>1 Input B Input B
Input B
Output Output
OR function
Input A
Input A Output Input A
Input A Output
Input B & Input B
>1 Input B
Input C Input B
Input C
Output
Output
NOR function
NAND function
Input A Input A
Input A Output Input A Output
XOR Input B XOR Input B
Input B Input B
Output Output
XOR function XOR function
Programming 97
Output
Input Input Output Input I0.0 Programming
Q2.0 97
I0.0 I0.1 Q2.0 =
Input I0.1 &
Output
Input Input Output Input I0.0 Q2.0
I0.0 I0.1 Q2.0 =
Figure 5.29 Ladder diagram and equivalentInput & diagram
I0.1 block
function
>1 Q
A
Output &
Figure 5.30
Q Ladder diagram and equivalent
B function block diagram
Exercice
A signal lamp is required to be switched on if a
pump is running and the pressure is
saDsfactory, or if the lamp test switch is
closed.
instruction may either use or change the value stored in a memory
register. For this, mnemonic codes are used, each code corresponding to
an operator/ladder element. The codes used differ to some extent from
InstrucDons List
manufacturer to manufacturer, though a standard IEC 1131-3 has been
proposed and is being widely adopted. Table 6.1 shows some of the codes
used by manufacturers, and the proposed standard, for instructions used in
this chapter (see later chapters for codes for other functions).
Transition condition
State/step Output
Transition condition
etc.
Start
IN 1 OUT 1 Step 0
IN 1 When IN 1 occurs, then
step 0 is realised and
Step 0 OUT 1 OUT 1 occurs
IN 2
Step 1
OUT 1 IN 2 OUT 3
When step 1 has been
Step 1 OUT 2 realised, i.e. OUT 1, and
the transfer condition
IN 3 IN 2, then step 1 is
END realised and OUT 2
End occurs
State 2
IN 1 IN 2 IN 3 OUT 0 IN 2 output
rollers Figure 6.15 Selective branching: the state that follows State 0 will
depend on whether transition IN1, IN2 or IN3 occur.
State 1 State 1
OUT 0 IN 1 output output
OUT 0 IN 1
State 0 OUT 0 OUT 0
State 0
State 2 IN 1 State 2
IN 1 IN 2 IN 3 OUT 0 IN 2 output output
State 0 OUT 0 IN 4 IN 5
State 3 Input
IN 1 State 2 output IN 5
output
convergence
when transition IN 1 occurs.
IN 4 IN 5
State 3 Input
output IN 5
State 4
IL, SFC and ST programming methods 1
Figure 6.17 Convergence: state 4 follows when either IN 4 or IN5 occur. State 2 Input State 4
State 2 State 3 output IN 4 output
State 3
IN 4 output
State 4
In 0 Out 0
ic Controllers
In 1
given using a ladder diagram, function block diagram, instruction list or
Figurestructured
6.20 Action
text. represented
Thus, where aby a ladder
ladder diagram
diagram is used, the behaviour of the
AcDons
action is shown by the ladder diagram being enclosed within the action
box. boxes
The action
Action are isgenerally
then activated when there
preceded is a powertoflow
by qualifiers into thethe
specify
action box. Figure 6.20 illustrates this.
conditions to exist for the action. In the absence of a qualifier or the
qualifier N, the action is not stored and executed continually while the
In 0
associate state is active. The qualifier P is used for a pulse action that
Out 0
executes only once when a stepInis0 activated. The qualifier D is used for a
time delayed action that only starts after a specified period. The qualifier
L is used for a time In 1
limited action which terminates after a specified
period (Figure 6.21).
Figure 6.20 Action represented by a ladder diagram
Structured text is not case sensitive, thus lower case or capital letters
can be used as is felt necessary to aid clarity. Likewise spaces are not
Gate2 :- Open;
End_IF;
IF (I:000/00 = 1) THEN
ollers O:001/00 :- 1;
IL, SFC and ST p
ELSE
AT is used to fix the memory location for a variable. Thus we might
O:000/01 = 0;
have: DO
End_IF; Output :- Input;
End_FOR;
CondiDonal statements
Input1 AT %IX100; (*Input1 is located at input memory bit 100*)
So if there is an input to I:000/00 to make it 1 then output O:001/00 is 1,
otherwise it 0. has the output decreasing by 1 each time the in
6.3.1 Conditional statements CASE is used to give the condition that selected statements are to be
decreases by 1.
executed if a particular integer value occurs else some otherWHILE
IF ... THEN ... ELSE is used when selected statements are to be executed selected... DO allows one or more stateme
when certain conditions occur. For example: statements. For example, for temperature control we might have:
particular Boolean expression remains true, e.g
ReseDng latch
Out 1
contacts are closed. Thus all the outputs can be switched on when the start Trigger Cycle Trigger
Output Out 1 controlled by the two Trigger
push button is activated. All the outputs will be switched off if the stop input control output
switch is opened. Thus all the outputs are latched by IR 1. internal relays input
Internal 1
relay 2 Trigger
Internal relay
Start Stop IR 1 IR 2 output
Trigger
input Cycle control
Internal relay
IR 1 2
of
Output 1
Internal relay
Out 1 Figure 7.4 Use of two internal relays
(a)
IR 1
Power onoff
Input 1 InternalFigure 7.9 1One-shot
relay 1Output (a) program, (b) facility in
Internal relay Output 2 In 1 IR 1 Out 1
Input for output 2 IR 1 Out 2
Output 2
If the power supply is cut off from a PLC while it is being used, all the
output relays and internal relays will be turned off. Thus when the power is
output Y430 to be reset, i.e. turned off and latched off. Thus the output
Y430 is on for the time between X400 being momentarily switched on
138 Programmable Logic Controllers
and X401 being momentarily switched on. Between the two rungs
indicated for the set and reset operations, there could be other rungs for
other activities to be carried out, the set rung switching on an output at the
beginning of the sequence and off atWiththe end.the Mitsubishi PLC, the o
activated when the trigger input, say
Input circumstances, M100 would remain
SET coil Y430
X400 were closed. However, if On M100 has b
X400
S Y430 M100 only remains on for Off a fixed p
Input
then goes off, regardless of On X400
X401
bei
X401 RESETt coil Y430 that would be used are LD OffX400, PL
SoubrouDnes
ne call with Mitsubishi PLC, (b) jump to subroutine call with Allen-Bradley PLC
IN2
9.2.1 Sequencing
As an illustration of the use of a timer, consider the ladder diagram shown
in Figure 9.5(a). When the input In 1 is on, the output Out 1 is switched
on. The contacts associated with this output then start the timer. The
contacts of the timer will close after the preset time delay, in this case 5.5
s. When this happens, output Out 2 is switched on. Thus, following the
input In 1, Out 1 is switched on and followed 5.5 s later by Out 2. This
illustrates how timed sequence of outputs can be achieved. Figure 9.5(b)
shows the same operation where the format used by the PLC manufacturer
Sequenced outputs
is for the timer to institute a signal delay.
In 1 Out 1 In 1 Out 1
Timer
Out 1 Timer Out 2
Preset Out 1
TON
to 5.5 IN Q
Timer Out 2
Timers can be linked together, the term cascaded is used, to give Time
longer
delay times than are possible with just one timer. Figure 9.7(a) shows the
Figureladder
9.8 On-off
diagramcycle timer
for such an arrangement. Thus we might have timer 1 with
a delay time of 999 s. This timer is started when there is an input to In 1.
after 10 s.
timers Pulse timers are used to produce a fixed duration output from some
initiating input. Figure 9.13(a) shows a ladder diagram for a system that
will give an output from Out 1 for a predetermined fixed length of time
when there is an input to In 1, the timer being one involving a coil. There
are two outputs for the input In 1. When there is an input to In 1, there is
an output from Out 1 and the timer starts. When the predetermined time
Pulse Dmer
has elapsed, the timer contacts open. This switches off the output. Thus
the output remains on for just the time specified by the timer.
In 1 Timer In 1
Time
Timer Out 1 Timer
Closed
contacts
Open
Time
Out 1
(a)
Time
Internal
In 1 Timer relay
TON
Internal
relay Out 1
(b)
Activated when Activated when Output when zero reached Output when set count
zero reached after counting set count reached after after counting down reached after counting
down from set value counting up from zero (b) from set value up from zero
(a)
Figure 10.1 Forms of representation of counters. In (a) RST is reset. In (b), the IEC 1131-3 representation, CD
is count down input, LD is for loading the input, PV is for the preset value, CV the current count value, CU is
count up input, and R is for the reset input.
When the X401 contacts close the output Y430 starts. Also the timer is
need to be of the same data type, e.g. REAL.
started. When the timer accumulated value reaches 5 s then the greater
than or equal to element switches on Y431. When the timer accumulated
value reaches >15 s Sthen the
D greater than or equal to element switches on
Y432. When the timer reaches 25 s then its contacts switch on Y433.
(a)
metic functions Most PLCs provide BCD-to-binary and binary-to-BCD conversions for
Data comparison
GRTuse when the input might be for a thumbwheel switch or the output to a
CMP>R
decimal display. Figure 12.7(a) shows the typical form of instructions for
T4.0.ACC
400 use in such situations and Figure 12.7(b) the form used by Siemens.
IN1
(b) (c) IN2
ArithmeDc foncDons Source
Data handling
BCD_I
Figure 12.4 Greater than comparison: (a) Mitsubishi, (b) Allen-Bradley,
203
BCD S D EN OUT
(c) Figure
Siemens 12.9 shows the basic form of the Siemens instuctions for
IN ENO
Destination
arithmetic functions. With integers the functions avaialble are ADD_1 for
addition, SUB_1 for subtraction,SourceMUL_1 for multiplication
I_BCD and DIV_1
for division with the quotient
BIN as S the Dresult. The arithmetic functions are
EN OUT
ArithmeDc operaDons
executed of there is a 1 at the enable EN input.
Destination
IN ENO
(a) (b)
ADD_1
Figure 12.7 Conversion: (a) BCD-to-binary and binary-to-BCD, (b)
ENinteger-to-BCD
BCD-to-integer and
IN1 OUT
12.3.1 Arithmetic operations
IN2 ENO
Some PLCs are equipped to carry out just the arithmetic operations of