PLC Programming
PLC Programming
Chang
Chapter 7.
PROGRAMMABLE
LOGIC CONTROLLER
09/09/23 7- 1
©T.C. Chang
FUNCTIONS OF CONTROLLERS
1) on-off control,
2) sequential control,
3) feedback control, and
4) motion control.
09/09/23 7- 2
©T.C. Chang
CONTROL DEVICES
09/09/23 7- 3
©T.C. Chang
PROGRAMMABLE LOGIC
CONTROLLER
Invented in 1968 as a substitute for hardwired relay panels.
09/09/23 7- 4
©T.C. Chang
VENDOR
S
MODICOM - GOULD
ALLEN-BRADLEY
GE
SQUARE-D
etc.
09/09/23 7- 5
©T.C. Chang
PLC
Input
CPU Input
Module
Flag
System
Output
Output Module
User Ladder
Diagram
Working
memory
registers
09/09/23 7- 6
©T.C. Chang
Allen-Bradley PLC-3
09/09/23 7- 7
©T.C. Chang
09/09/23 7- 8
©T.C. Chang
SWITCHES
Non-locking Locking
SPDT
P1
DPST
P2
09/09/23 7- 9
©T.C. Chang
TERMS
Throw - number of states
Pole - number of connecting moving parts (number of individual
circuits).
A B
Output
DPST Input
Knob
How is this switch classified?
09/09/23 7- 10
©T.C. Chang
TYPES OF SWITCHES
• Selector switches RATING:
• Pushbutton switches
•24 Volts AC/DC
• Photoelectric switches •48 Volts AC/DC
• Limit Switches •120 Volts AC/DC
• Proximity switches •230 Volts AC/DC
•TTL level
• Level switches (Transistor-to-transistor
• Thumbwheel switches ±5V)
• •Isolated Input
Slide switches
09/09/23 7- 11
©T.C. Chang
RELAYS
A switch whose operation is activated by an electromagnet is called a "relay"
contact
coil
input
Relay coil
R1
Output contact R1
09/09/23 7- 12
©T.C. Chang
COUNTE
R
Digital counters output in the form of a relay contact when a preassigned
count value is reached.
input
Register 5
Accumulator
reset
contact
output
Input
Reset
Output
Count 0 12 3 4 5 0 1
09/09/23 7- 13
©T.C. Chang
TIMER
A timer consists of an internal clock, a count value register, and an
accumulator. It is used for or some timing purpose.
Clock
Accumulator
reset
Register
contact
Contact
Clock
output
Reset
Output
Count 0 1 2 3 4 5
Time 5 seconds.
09/09/23 7- 14
©T.C. Chang
R1
TIMER R2
R1 PB1
LS1 LS2
PR=5
TIMER
5
Motor
R2
R1
09/09/23 7- 15
©T.C. Chang
PLC
ARCHITECTURE
Programmable controllers
software programming.
replace most of the relay panel wiring by
PC
Program
Loader
Switches
Printer
I/O
Processor
Modules Machines
Cassette
Loader
Power
EPROM Memory Supply
Loader
A typical PLC
09/09/23 7- 16
©T.C. Chang
PLC COMPONENTS
1. Processor Microprocessor based, may allow arithmetic
operations, logic operators, block memory moves,
computer interface, local area network, functions, etc.
09/09/23 7- 17
©T.C. Chang
3. I/O
PLC COMPONENTS
Modular plug-in periphery
AC voltage input and output,
DC voltage input and output,
Low level analog input,
High level analog input and output,
Special purpose modules, e.g., high speed timers,
Discrete I/O
• AC Voltage I/O
• DC Voltage I/O
• numerical input and output
• special-purpose modules, for example, high-
speed timers, and stepping motor controllers
09/09/23 7- 19
©T.C. Chang
Analog I/O
Analog inputs:
• Flow sensors
• Humidity sensors
• Potentiometers
• Pressure sensors
• Temperature sensors
Analog outputs:
• Analog meters
• Analog valves and actuators
• DC and AC motor drives
09/09/23 7- 20
©T.C. Chang
• PID control
– The Proportional Integral Derivative is used for closed loop process
control.
• Network module
09/09/23 7- 21
©T.C. Chang
09/09/23 7- 22
©T.C. Chang
LADDER
DIAGRAM
A ladder diagram (also called contact symbology) is a means of
graphically representing the logic required in a relay logic system.
Rail
start emergency stop
PB1 PB2
R1
Rung R1
R1
09/09/23 7- 23
©T.C. Chang
External
switches
Stored program
09/09/23 7- 24
©T.C. Chang
Input Connections
AC DC TTL
09/09/23 7- 25
©T.C. Chang
Output Connections
AC DC TTL
09/09/23 7- 26
©T.C. Chang
SCA
Nall the outputs are updated based
A PLC resolves the logic of a ladder diagram (program) rung by rung,
from the top to the bottom. Usually,
on the status of the internal registers. Then the input states are checked
and the corresponding input registers are updated. Only after the I/Os
have been resolved, is the program then executed. This process is run
in a endless cycle. The time it takes to finish one cycle is called the
scan time. In some controllers the idle state is eliminated. In this case,
the scan time varies depends on the program length.
Output
Input
begin
Idle
Scan cycle
Resolve
logic
09/09/23 7- 27
©T.C. Chang
PLC Programming
09/09/23 7- 28
©T.C. Chang
1) Relay,
2) Timer and counter,
3) Program control,
4) Arithmetic,
5) Data manipulation,
6) Data transfer, and
7) Others, such as sequencers.
09/09/23 7- 29
©T.C. Chang
LOGIC STATES
ON : TRUE, contact closure, energize, etc.
OFF: FALSE, contact open , de-energize, etc.
Do not confuse the internal relay and program with the external
switch and relay. Internal symbols are used for programming.
External devices provide actual interface.
09/09/23 7- 30
©T.C. Chang
R1 = PB1.AND.PB2
PB1 R1
OR PB2
09/09/23 7- 31
©T.C. Chang
PB1 R1
PB2 pb3
09/09/23 7- 32
©T.C. Chang
RELA
Ycoil and the contact(s).
A Relay consists of two parts, the
Contacts:
a. Normally open -| |-
b. Normally closed -|/|-
c. Positive transition sensing -|P|-
d. Negative transition sensing -|N|-
()
Coil:
a. Coil -( )-
b. negative coil -(/)-
c. Set Coil -(S)-
d. Reset Coil -(R)-
09/09/23 7- 33
©T.C. Chang
Relay (continue)
Coil:
e. Retentive memory Coil -(M)-
f. Set retentive memory Coil -(SM)-
g. Reset retentive memory Coil -(RM)-
h. Positive Transition-sensing Coil -(P)-
h. Negative Transition-sensing Coil -(N)-
(set coil latches the state, reset coil deenergize the set coil.
retentive coil retain the state after power failure.)
09/09/23 7- 34
©T.C. Chang
TIMERS AND
COUNTERS Input
Timers: True False True
09/09/23 7- 35
©T.C. Chang
SEQUENCE
R
Sequencers are used with machines or processes involving
repeating operating cycles which can be segmented into
steps.
Output
Step A B C Dwell time
1 ON OFF OFF 5 sec.
2 ON ON OFF 10 sec.
3 OFF OFF ON 3 sec.
4 OFF ON OFF 9 sec.
09/09/23 7- 36
©T.C. Chang
A-B PLC
I/O points are numbered, they correspond to the I/O slot on the
PLC.
09/09/23 7- 37
©T.C. Chang
PROGRAMMING EXAMPLE 1
Bar code reader
microswitch Stopper
Part Conveyor
Robot
Machine
SOLUTION
Input
Output
MS1 R1
01 11
C1 02 12 R2
C2 R3
03 13
C3 R4
04 14
Programmable
C4 05 15
Controller
PLC
Rung 1. If part arrives and no
part is stopped, trigger the
bar code reader.
Rung 2. If it is a right part,
11
01 14 activate the stopper.
Rung 3. If the stopper is up,
02 14 the machine is not busy
and the robot is not busy,
12
load the part onto the
14 04 03
machine.
Rung 4. If the task is
05 03 13
completed and the robot is
not busy, unload the
machine.
09/09/23 7- 39
©T.C. Chang
09/09/23 7- 40
©T.C. Chang
Data Types
SINT short integer 1 byte BOOL boolean 1 bit
INT integer 2 bytes BYTE byte 1 byte
DINT double integer 4 bytes WORD 16 bit bit string 16 bits
LINT long integer 8 bytes DWORD 32 bit bit string 32 bits
USINT unsigned short integer 1 byte LWORD 64 bit bit string 64 bits
UINT unsigned integer 2 bytes
UDINT unsigned double integer 4 bytes
ULINT unsigned long integer 8 bytes
REAL real 4 bytes
LREAL long real 8 bytes
09/09/23 7- 41
©T.C. Chang
Derived Data
Types
TYPE (* user defined data types, this is a comment*)
pressure : REAL;
temp : REAL;
part_count : INT;
END_TYPE;
Structure:
TYPE data_packet:
STRUCT
input : BOOL;
t : TIME;
out : BOOL;
count : INT;
END_STRUCT;
END_TYPE;
09/09/23 7- 42
©T.C. Chang
Variable Declarations
Local variable:
09/09/23 7- 43
©T.C. Chang
09/09/23 7- 44
©T.C. Chang
Condition Statements
09/09/23 7- 45
©T.C. Chang
Iteration Statements
09/09/23 7- 46
©T.C. Chang
Functions
FUNCTION add_num : REAL
VAR_INPUT
I,J : REAL
END_VAR
add_num := I + J;
END_FUNCTION
Call a function:
x:= add_num(1.2, 5.6);
Built-in Functions:
ABS, SQRT, LN, LOG, EXP, SIN, COS, TAN, ASIn, ACOS,
ATAN, ADD, MUL, SUB, DIV, MOD, EXPT, MOVE), logic
functions (AND, OR, XOR, NOT), bit string functions (SHL,
SHR shift bit string left and right , ROR, ROL rotate bit
string), etc.
09/09/23 7- 47
©T.C. Chang
Programs
PROGRAM example7.1
VAR_INPUT R1 := MS1 AND (NOT R4);
MSI : BOOL; R2 := R4 AND (NOT C3) AND (NOT C2);
C1 : BOOL; R3 := C4 AND (NOT C3);
C2 : BOOL;
R4 := C1;
C3 : BOOL;
END_PROGRAM
C4 : BOOL;
END_VAR
VAR_OUTPUT
R1 : BOOL : FALSE;
R2 : BOOL : FALSE;
R3 : BOOL : FALSE;
R4 : BOOL : FALSE;
END_VAR
09/09/23 7- 48
©T.C. Chang
09/09/23 7- 49
©T.C. Chang
09/09/23 7- 50
©T.C. Chang
block diagram
SP set point
PV sensor feedback
Vout K p E Tr Edt Td dE
KP proportional error gain dt
TR integral gain
TD derivative gain control algorithm
AUTO calculate
XOUT output to process
XO manual output adjustment
cycle time between execution
09/09/23 7- 51
©T.C. Chang
e.g.
LD MS1
ST R1
loop ANDN C3
09/09/23 7- 52
©T.C. Chang
Operator
Operator Modifiers Description s GT
than
( greater
LD N load
operand into register
GE ( greater
STN store register
value into operand than and equal to
S set operand true EQ ( equal
R reset operand false NE ( not
AND N, ( Boolean equal
AND
& N, ( Boolean AND LE ( less than
OR N, ( Boolean and equal to
OR
LT ( less than
XOR N, ( Boolean
XOR JMP C, N jump to
ADD ( addition label
SUB ( CAL C, N call
subtraction
function block
MUL (
multiplication Modifier “N” means RET C,defers
negate. “(“ N the return
DIV ( division from function
operator. “C” is a condition modifier, or
the
operation is executed if the register value is true.
function block
09/09/23 ) execute last 7- 53
©T.C. Chang
PROGRAM example7.1
LD MS1
VAR_INPUT
ANDN R4
MSI : BOOL;
ST R1
C1 : BOOL;
C2 : BOOL;
LD R4
C3 : BOOL; ANDN C3
C4 : BOOL; ANDN C2
END_VAR ST R2
VAR_OUTPUT LD C4
R1 : BOOL : FALSE; ANDN C3
R2 : BOOL : FALSE; ST R3
R3 : BOOL : FALSE;
LD C1
R4 : BOOL : FALSE;
ST R4
END_VAR
END_PROGRAM
09/09/23 7- 54
©T.C. Chang
09/09/23 7- 55
©T.C. Chang
SFC
Action Qualifiers:
N non-stored, executes while the
step is active
R resets a store action
S sets an action active
L time limited action, terminates
after a given period
D time delayed action.
P a pulse action, executes once in
a step
SD stored and time delayed
DS time delayed and stored
SL stored and time limited
09/09/23 7- 56
©T.C. Chang
09/09/23 7- 57