Você está na página 1de 95

4 Programming

PLCs are intended to be used by engineers without any


great knowledge of programming.
So, ladder programming was developed.
This is a means of writing programs which can then be
converted into machine code by some software for use
by the PLC microprocessor.
1

Ladder Diagrams
The diagram shows the
circuit for switching on or off
an electric motor.
In a ladder diagram, we use
two vertical lines to
represent the input power
rails and stringing the rest of
the circuit between them.
Another example: Motor
start-stop
2

PLC Ladder Programming


A very commonly used method of
programming PLCs.
Writing a program is equivalent to
drawing a switching circuit.
The ladder diagram consists of two
vertical lines representing the power
rails. Circuits are connected as
horizontal lines,
i.e. the rungs of the ladder, between these
two verticals.
3

In drawing a ladder diagram, observe the


following:
1 The vertical lines of the diagram represent the
power rails between which circuits are connected.
2 Each rung defines one operation in the control
process.
3 A ladder diagram is read from left to right and
from top to bottom to the end and then promptly
resumes at the start.
4 Each rung must start with an input or inputs
and must end with at least one output.
5 Electrical devices are shown in their normal
condition.
A switch which is normally open until some object
closes it, is shown as open.
A switch that is normally closed is shown closed.

6 A particular device can appear in more than one


rung of a ladder.
We might have a relay which switches on one or
more devices. The same letters and/or numbers
are used to label the device in each situation.

7 The inputs and outputs are all identified by their


addresses, the notation used depending on the
PLC manufacturer.

Standard symbols that are


used for input and output
devices.
Inputs are represented by
normally open or normally
closed contacts. The action
of the input is equivalent to
opening or closing a switch.
Outputs are represented by
just one symbol, regardless
of the device connected to
the output.
Further symbols will be
introduced in later sessions.
5

Simple Example
Ex01Sw

ON-OFF SWITCH

Logic Functions
There are many control situations requiring
actions to be initiated when a certain
combination of conditions is realized.
The drill motor is to be activated when the limit
switches are activated that indicate the presence of
the workpiece and the drill position as being at the
surface of the workpiece.
Such a situation involves the AND logic function.
7

1 AND
An output is not energized
unless two, normally open,
switches are both closed.
Inputs
A
O
O
1
1

Output
B
0
1
0
1

0
0
0
1

Example: An interlock control


system tool so that it can
only be operated when the
safety guard is in position
and the power switched on.
8

2 OR
An output is energized when
switch A or B, both normally
open, is closed.
Inputs
A
O
O
1
1

Output
B
0
1
0
1

0
1
1
1

Example: A conveyor belt


transporting bottled products
to packaging where a
deflector plate is activated to
deflect bottles into a reject
bin if either the weight is not
within certain tolerances or
there is no cap on the bottle.

Simple Example
Ex02Sw

PUSH-BUTTON SWITCH

10

3 NOT
There is an output when there
is no input and no output when
there is an input. The gate is
sometimes referred to an
inverter.
Input
A
0
1

Output
1
0

Example: A light that comes on


when it becomes dark, i.e.
when there is no light input to
the light sensor there is an
output.
11

Simple Example
Ex03Sw

RUN-STOP MOTOR

12

The motor operation


(Example of LOAD, AND,
OR, OUT instructions)
When PB1 is pushed, a
motor will start to rotate with
a forward (clockwise)
direction.
When the PB2 is pushed, it
will start to rotate with a
reverse (counterclockwise)
direction
The PB0 is emergency stop
switch.

13

Ex04Motoroperation

Forward operation,
Set an interlock with
reverse operatin
(P41, P02)
Reverse operation,
Set an interlock with
forward operatin
(P44, P01)

14

4 NAND
Follow an AND gate with a
NOT gate, invert all the outputs
from the AND gate.
An alternative, which gives
exactly the same results, is to
put a NOT gate on each input
and then follow that with OR
Inputs
Output
A
B
O
0
1
O
1
1
1
0
1
1
1
0
Example: A warning light that
comes on if, with a machine
tool, the safety guard switch
has not been activated and the
limit switch signaling the
presence of the workpiece has
not been activated.
15

5 NOR
Follow an OR gate by
a NOT gate, invert the
outputs of the OR gate.
An alternative, which
gives exactly the same
results, is to put a NOT
gate on each input and
then an AND gate for
the resulting inverted
inputs
Inputs
A
0
0
1
1

Output
B
0
1
0
1

1
0
0
0

16

Ex05DemorganLaw

The Circuits have the same function

17

6 Exclusive OR
(XOR)
Gives an output when
either of the inputs is 1
but not when both are
1.
Inputs
A
0
0
1
1

Outputs
B
0
1
0
1

0
1
1
0

18

Latching
There are often situations where it is necessary to hold
an output energized, even when the input ceases.
A motor which is started by pressing a push button switch.

It is a self-maintaining circuit in that, after being


energized, it maintains that state until another input is
received.

19

Ex06MotorOn-OffwithLamps

Example: a motor controlled by stop and start push


button switches and for which one signal light must be
illuminated when the power is applied to the motor and
another when it is not applied.

Motor output

Lamp for power


not applied
Lamp for power
applied

20

Multiple Outputs
With ladder diagrams, there can be more than one
output connected to a contact.
Outputs P40, P41 and P42 are switched on as the
contacts in the sequence given by the contacts P00,
P01 and P02 are being closed.
Until P00 is closed, none of the other outputs can be
switched on.
When P00 is closed, P40 is switched on.
When P01 is closed, P41 is switched on.
When P02 is closed, P42 is switched on.
Ex07MultipleOutput
21

Entering Ladder Programs


Whatever method is used to enter the
program into a programming terminal or
computer, the output to the memory of the
PLC has to be in a form that can be handled
by the microprocessor.
This is termed machine language and is just
binary code, e.g. 0010100001110001.

22

Ladder Symbols
There are several methods that can be used for
keying in programs.
a keypad having keys with symbols depicting the various
elements of the ladder diagram and keying them in so that
the ladder diagram appears on the screen of the
programming terminal.
Computers can be used to draw up a ladder program.
These involve loading the computer with the relevant
software and then selecting items from menus on the
screen.
23

Instruction Lists
Mnemonic codes
Another PLC programming method.
Each code corresponding to a ladder element.
The codes used may differ from manufacturer
to manufacturer, though a standard IEC 1131-3
has been proposed.

Step
0
1
2

Instruction
LD
X400
AND
X401
OUT
Y430

24

Branch codes

The Exclusive OR (XOR) gate shown in


Mitsubishi notation has two parallel arms with
an AND situation in each arm.
Mitsubishi uses an ORB instruction to indicate
OR together parallel branches.
The first instruction is for a normally open pair
of contacts X400.
The next instruction is for a series set of
normally closed contacts X401, hence ANI X401.
The third instruction starts a new line. It is
recognized as a new line because it starts with
LDI, all new lines starting with LD or LDI.
But the first line has not been ended by an
output. The PLC thus recognizes that a parallel
line is involved for the second line and reads
together the listed elements until the ORB
instruction is reached.
ORB (OR branches! blocks together) indicates to
the PLC that it should OR the results of steps 0
and 1 with that of the new branch with steps 2
and 3.
The list concludes with the output OUT Y430.

Step
0
1
2
3
4
5

Instruction
LD
X400
ANI
X401
LDI
X400
AND
X401
ORB
OUT
Y430
25

2 More than One Rung


With two rungs, we just write
the instructions for each line
in turn.
The instruction LD or LDI
indicates to the PLC that a
new rung is starting.
Step
0
1
2
3

Instruction
LD
OUT
LDI
OUT

26

Boolean Algebra
Instruction lists and ladder
programs can be derived from
Boolean expressions.
In Boolean algebra there are just
two digits 0 and 1.
AND
A.B=Q
OR A+B=Q
NOT
A- =Q

27

Ex ample 1
A+B.C= Q
X400 + X401.X402 = Y430

Ex ample 2
Fr om the l ogi c diag r am
( A.B + C) D - E F - = Q

28

Function block diagrams


A format sometimes
used for program.
A block is represented
with the function
name written in the
box.
Symbols are used for
the function names.
AND function is
represented by &.
OR function by >=1

29

Programming examples
Example 1

Ex08SignalLamp

A signal lamp is required to be switched on if a pump is


running and the pressure is satisfactory, or if the lamp
test switch is closed.

Pump

Pressure

Lamp

Test

30

Example 2

Ex09SafetySw

A machine has four sensors to detect when safety features are


not active.
When there is an input to any one of these sensors, NC, it opens
and the machine must stop and an alarm sound.

31

Example 3

Ex10Level

A Level Application
We are controlling lubricating oil being
dispensed from a tank. This is possible by
using two sensors. We put one near the
bottom and one near the top.
The fill motor to pumps lubricating oil into
the tank until the high level sensor turns
on. At that point we want to turn off the
motor until the level falls below the low
level sensor. Then we should turn on the
fill motor and repeat the process.
We use 2 inputs (sensors) and 1 output
(the fill motor). 0500 is just an indicator
lamp.
How a Ladder is scanned (ANIMATED)

32

5 Internal Relays

33

Internal Relays
Do not exist in the real-world switching devices but are
merely bits in the storage memory that behave in the same
way as relays.
For programming, they can be treated in the same way as
an external relay output and input.
It has to be activated on one rung of a program and then its
output used to operate switching contacts on another rung,
or rungs, of the program.
To distinguish internal relay outputs from external relay
outputs, they are given different types of addresses.
Different manufacturers tend to use different terms for
internal relays.
34

Internal Relays in Programs


With ladder programs, an internal relay output is
represented using the symbol for an output device,
namely ( ) or 0, with an address which indicates
that it is an internal relay rather than an external
relay.

35

1 Programs with Multiple Input


Conditions
An illustration:
A system is to be activated when two different SETS
of input conditions are realized. We might just
program this as an AND logic gate system;
However, if a number of inputs have to be checked in
order that each of the input conditions can be
realized, it may be simpler to use an internal relay.
The first input conditions then are used to give an
output to an internal relay. This has associated
contacts which then become part of the input
conditions with the second input.
36

Automatic lifting of a barrier

Ex11AutomaticBarrierLifting

When someone approaches from either side,


Input 1 and input 3 are activated (photoelectric
sensors).
Input 2 is an enabling switch to enable the
system to be closed down.
Thus when input 1 or input 3, and input 2, are
activated, there is an output from the internal
relay 1.
If input 4, perhaps a limit switch, detects that
the barrier is closed then it is activated and the
limit switch closes. The result is then an output
from Out 1, a motor which lifts the barrier.
If the limit switch detects that the barrier is
already open, the person having passed
through it, then it opens and so output I is no
longer energized and a counterweight might
then close the barrier.

The internal relay has enabled two parts of


the program to be linked, one part being
the detection of the presence of a person
and the second part the detection of
whether the barrier is already up or down.

37

38

2 Latching Programs
It would be possible to devise a ladder diagram
which has individually latched controls for each
such output. However, a simpler method is to
use an internal relay.
Ex12MultipleOutputs
Example
The first rung has the latch for keeping the
internal relay IR 1 on when the start switch gives
a momentary input. The second rung will then
switch the power on. The third rung will also
switch on and give output Out 2 if input 2
contacts are closed. The third rung will also
switch on and give output Out 3 if input 3
contacts are closed.
Thus all the outputs can be switched on when
the start push button is activated.
All the outputs will be switched off if the stop
switch is opened.
Thus all the outputs are latched by IR 1.
39

Design example

Ex12A car park

Parking Lot (in and out) barriers, each opens when a sensor signals a
car in and closes when another sensor signals that the car has passed.

40

Battery-Backed Relays

Some internal relays have battery


back-up so that they can be used in
circuits to ensure a safe shutdown of
plant in the event of a power failure
and so enable it to restart in an
appropriate manner.
Battery-backed relays retain their
state of activation, even when the
power supply is off.
The relay is said to have been made
retentive.
Example
IR 1 is a battery-backed internal relay.
When input 1 contacts close, output IR 1 is
energized. This closes the IR 1 contacts,
latching so that IR 1 remains on even if
input 1 opens. The result is an output from
Out 1.
If there is a power failure, IR 1 still remains
energized and so the IR 1 contacts remain
closed and there is an output from Out 1.
41

Ex13BatteryBackedRelay
Turn-off the supply then turn it back on to test the Keep relay.

42

Ex17PowerFailure

43

One-Shot Operation

Provided by some PLC manufacturers.


An internal relay is programmed so that its
contacts are activated for just one cycle,
i.e. one scan through the ladder program.
Example
When X400 contacts close, the output
internal relay M100 is activated.
Under normal circumstances, M100
would remain on for as long as the
X400 contacts were closed.
If M100 has been programmed for
pulse operation, Ml00 only remains on
for a fixed period of time, one program
cycle. It then goes off, regardless of
X400 being on.
May be used to create pulses for the
resetting of counters and timers and
marking the start of cycles.
44

Ex14PulseOutputDInstruction

45

Ex15OnOffToggle

P0 turns M0 on for one


scan then P60 is activated
through step 09. After the
first scan, P60 remains on
by step 03.
The next P0 will turn on
M0 for one scan. So, step
03 turns off P60.

46

The previous example is repeated with Animation


1001 is just an internal relay

Ex16OneShotWWW

47

Set and Reset


The set instruction causes the relay to
self-hold, i.e. latch.
It then remains in that condition until the
reset instruction is received.
The term flip-flop is often used.

48

Ex18PandKrelsys

49

Example

Ex19SetRest-to-OneShot
set-reset function can be used to build the pulse (one-shot) function described
earlier.
An input P00 causes the internal relay M00 in the first rung to be activated.
So, second rung, in the setreset of P40 is being set. This setting action results in
M00 in the first rung opening and so, despite there being an input in the first rung,
M00
Because the rungs are scanned in sequence from top to bottom, a full cycle must
elapse before the internal relay in the first rung opens. A pulse of duration one cycle
has thus been produced.
The system is reset when P00 ceases.
One-pulse appears on P41

50

Master Control Relay


It is sometimes necessary for whole
sections of ladder diagrams to be
turned on or off when certain criteria
are realized.
This could be achieved by including the
contacts of the same internal relay in
each of the rungs so that its operation
affects all of them.
An alternative is to use a master control
relay.
51

With no input to input 0, the output internal relay


MC 0 is not energized and so its contacts are
open.
So, all the rungs between where it is designated
to operate and the rung on which its reset
MCSCLR or another master control relay is
located are switched off.
Ex20MCRsimple

52

Animated Example

Ex21MCRwww

53

Ex22MCRManualAutomaticDoor

Automatic Mode

Manual Mode

Common Part
P40 opens
P41 closes
(Automatic or Manual)

54

Jump
If the appropriate
conditions are met, this
function enables part of
a ladder program to be
jumped over.
Example
when there is an input to In 1, the program
jumps to Rung 4 and then proceeds with rungs
5,6, etc.
When there is no input to In 1, the jump relay is
not energized and the program then proceeds
to rungs 2,3, etc.

55

Ex23Jump

56

Jumps within jumps are possible.


Example
If the condition for the jump instruction 0 realized then the program jumps

to rung 8.
If the condition is not met then the program continues to rung 3.
If the condition for the jump instruction 1 is realized then the
program jumps to rung 6.
If the condition is not met then the program continues through the
rungs.

Ex24NestedJump
57

6 Timers

58

Types of Timers

Timers behave like relays with coils which


when energized result in the closure or
opening of contacts after some preset
time.
There are a number of different forms of
timers that can be found with PLCs.
On-delay timers. These are timers which
come on after a particular time delay
Off-delay timers are on for a fixed period of
time before turning off.
Pulse timer switches on or off for a fixed
period of time.
Integration timer
Monostable timer
Retrigerrable timer

The time duration for which a timer has


been set is termed the preset and is set in
multiples of the time base used.
Some times bases are typically 10 ms, 100
ms, 1 s, 10 s and 100 s.
Thus a preset value of 5 with a time base
of 100 ms is a time of 500 ms.

59

Programming Timers
All PLCs generally have
delay-on timers, small PLCs
possibly having only this type
of timer.
Figure 6.3 shows a ladder
rung diagram involving a
delay-on timer.
Whenever the input to the
TON timer is 0, the timer
resets to 0.

Ex25TimerSimple
60

Example

Ex35TimerWWW

Animated

61

1 Sequencing

Ex26sequencing
Example 1
When the input P01 is on, P40 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 0.5 s.
When this happens, output P41 is switched on.
Thus, following the input P01, P40 is switched on and followed
O.5 s later by P41.

62

Ex27Sequence3Motors
Example 2
Timers are used to start three outputs, e.g. three motors, in sequence following
a single start button being pressed.
When the start push button is pressed there is an output from internal relay M0.
This latches the start input. It also starts both the timers, T0 and T1, and motor
1.
When the preset time for T0 has elapsed then its contacts close and motor 2
starts.
When the preset time for timer T1 has elapsed then its contacts close and
motor 3 starts.
The three motors are all stopped by pressing the stop push button.

63

2 Cascaded Timers
Timers can be linked together,
the term cascaded is used, to
give longer delay times than
are possible with just one timer.
Example
Timer 1 has a delay time of 999 s. This timer
is started when there is an input to In 1.
When the 999 s time is up, the contacts for
timer 1 close.
This then starts timer 2. This has a delay of
100 s.
When this time is up, the timer 2 contacts
close and there is an output from Out 1.
Thus the output occurs 1099 s after the input
to In 1.
64

Ex28CascadedTimers

65

3 On-Off Cycle Timer


On-delay timers can be used to produce an on-off cycle timer.

When there is an input to P0, timer 1 starts.


Timer 1 is set for a delay of 1 s. T1 is 100 mSec. timer.
After 1 s, it switches on timer 2 and the output .
Timer 2 has a delay of 1 s. T2 is 100 mSec. timer.
After 1 s, the contacts for timer 2, which are normally closed, open.
So, T1, is switched off and its contacts in the second rung to open and
switch T2.
Now, T2 contacts resuming their normally closed state and P0 causes the
cycle to start all over again.
Ex29FlickerLampTimer

66

Off-Delay Timers

A delay-on timer can be


used to produce a delay-off
timer.
When there is a momentary
input to In 1, both the output
Out 1 and the timer are
switched on.
Because the input is latched
by the Out 1 contacts, the
output remains on.
After the preset timer time
delay, the timer contacts,
which are normally closed,
open and switch off the
output.
Thus the output starts as on
and remains on until the time
delay has elapsed.

Ex30OffDelayTimerEmulation

67

TON timer
Input High, the timer is set to 0 and starts counting. At full
count, it becomes 0 and remains 0 as long as its input is 0.
Input Low, the timer is set to 0 and resets.
TOFF timer
Input High, the timer is set to 1 and Loaded.
Input Low, starts counting. At full count, it becomes 0 and
remains 0 as long as its input is 0.
Ex31TOFFtimer

68

In this example:
Motor 1 needs a TOFF Timer.
Starts immediately with the
switch and turns off after a
delay when the switch is
off.
Motor 2 needs a TON Timer
(at turn-on) ORed with a TOFF
timer (at turn-off).
Starts after a delay with
the switch and turns off
after a delay when the
switch is off.
Motor 3 needs a TON Timer.
Starts after a delay with
the switch and turns off
immediately when the
switch is off.
69

Ex32ConveyerControlTOFF

70

One-Shot Timers
It is used to produce a fixed duration output from some
initiating input.

When there is an input to P1, T0 is off and starts counting.


So, there is an output from P40.
When the predetermined time has elapsed, the timer contacts closes.
This switches off the output.
Thus the output remains on for just the time specified by the timer.

Ex33OneShotTimerEmulation

71

Example
Ex34OnWhenOutputTimerCeases
When there is an input to P1, M1 is energized. The timer does not start at this
point because the normally closed P1 contacts are open.
The closing of the M1 contacts means that the M2 is energized. There is,
however, no output from P40 at this stage because, NC P1 contacts are open.
When P1 ceases, both the internal relays remain energized and the timer is
started. They latch each other. Also P40 becomes on.
After the set time, the timer contacts, which are normally closed, open and switch
off M2. This in turn switches off M1. It also, in the bottom rung, switches off the
output P40.
Thus the output is off for the duration of the input, then being switched on for a
predetermined length of time.

72

Integration Timer TMR

Ex36TMR

73

Ex37DrillAlarmTMR

74

Monostable Timer TMON

Ex38MONsimple

75

Ex39ChatteringPreventionTMON

Even if P0 is
chattering P41
remains on for
200 mSec
76

Retrigerrable Timer TRTG

Ex40TRTGsimple

77

Ex41CConveyerFaultDetectionTRTG

78

7 Counters

79

Forms of Counters
A counter is set to some preset number value and,
when this value of input pulses has been received,
it will operate its contacts. Thus normally open
contacts would be closed, normally closed contacts
opened.
There are three types of counter

Down-counters CTD
Up-counters CTU
Up-Down counters CTUD
Ring counter CTR

80

Programming
When there is a pulse input
P1, the counter is reset.
When there is an input to P0,
the counter starts counting.
If at any time during the
counting there is an input to
P1, the counter will be reset
and start all over again.

Ex42CounterSimple

81

Counter Application

The machine directs 6 tins along one path for packaging in a box and then 12 tins along
another path for packaging in another box.

A pulse input to P02, resets both the counters (push button switch used to start the conveyor moving).
The input which is counted is P01 (a photocell sensor which detects the presence of tins passing
along the conveyor).
C0 starts counting after P02 is momentarily closed.
When C0 has counted six items, it closes its contacts and so gives an output at P40 (a solenoid
which is used to activate a deflector to deflect items into one box or another).
When C0 stops counting it closes its contacts and so allows C1 to start counting.
C1 counts for 12 pulses to P01 and then closes its contacts.
This results in both counters being reset and the entire process can repeat itself.

Ex43aCansCounter

82

This has the same functions as the previous example.


Here, the C0 stops counting after it counts 6 (NC C0).
Ex43bCansCounter

83

Up and Down Counting

Ex44CTUDsimple

84

It is possible to program up- and downcounters together.


Example
Count the cars as they enter a multistorage parking lot and as they leave it.
An output is to be triggered if the number
of items/cars entering is some number
greater than the number leaving, i.e. the
number in the parking lot has reached a
saturation value. The output might be to
illuminate a No empty spaces sign.
We use the up-counter for items entering
and the count down for items leaving.
When an item enters it gives a pulse on
input In 1. This increases the count by
one.
When an item leaves it gives an input to
In 2. This reduces the number 1.
When the accumulated value reaches
the preset value, the output Out 1 is
switched on.

Ex46ParkingLotCounter

85

Ex46A ParkingLotCounter

86

Ex47MotorControl

87

Sequencers
The PLC sequencer consists of a master
counter that has a range of presets counts
corresponding to the different steps.
Each step in the count sequence relates
to a certain output or group of outputs.
The outputs are internal relays, these in turn
being used to control the external output
devices.
88

Example

Ex48Sequencer

One way of implementing the above


sequence is to use a timer which is
programmed to self-reset every 5 s
after being activated by an input
being closed.
It requires the compare function that
will be explained later on.

89

Ex48ATimeSequencer

90

Ring Counter CTR

Ex49CTRsimple

91

8 Shift Registers
The shift register is a number of
internal relays grouped together which
allow stored bits to be shifted from one
relay to another.
92

Shift Registers
A register is a number of internal relays grouped together,
normally 8, 16 or 32.
Each internal relay is either effectively open or closed,
these states being designated as 0 and 1. The term bit is
used for each such binary digit.
With the shift register it is possible to shift stored bits.
Shift registers require three inputs,
one to load data into the first location of the register,
one as the command to shift data along by one location and
one to reset or clear the register of data.

93

Ladder Programs

Example 1

Ex50ShiftRegister

P04 resets the shift register,


put all the values at 0.

P01 is used to input to the first internal relay in the


register.
P02 (Direction) input 1 shifts left and input 0 shifts
right.
P03 shifts the states of the internal relays along by
one.
P04 reset
The output is starting at relay P40 (4 relays).

94

Example

Ex51ShiftRegisterWWW

ANIMATED

95