PLC is short for Programmable Logic Controller. At present, PLC has two external forms: integrated (compact) and modular. The integrated type is to combine the PLC power supply, CPU processor, memory, and a certain number of I/O together to form a whole, as shown in Fig. 9.1a. This type of PLC has low cost, fixed I/O addresses, and is easy to use, the disadvantage is that the program stock is small and the functions are few. Modular PLC is composed of modules with different functions. The types of modules include: power supply module, CPU module, digital input module, digital output module, analog input module, analog output module, communication module, positioning module, counting module, etc. According to the engineering needs, a certain number of modules are combined on a base plate (or rack) to form a flexible assembled PLC, as shown in Fig. 9.1b. This kind of PLC is more expensive than the integrated PLC, but the functions and scale of control are also more powerful. This chapter will take the modular PLC as an example to explain the basic usage of PLC.

Fig. 9.1.
2 illustrations of a programmable logic controller. A is a single P L C unit, while B has a set of P L C units with different input and output modules. They provide the physical design and modularity of P L C systems.

The appearance of PLC

9.1 Simple Way to Get Started with Modular PLC

  1. 1.

    First determine the signal module (SM) you are using, and find out the address arrangement method of the digital input (DI) module, digital output (DO) module, analog input (AI) module and analog output (AO) module.

Take the S7-300 PLC with four input and output modules as an example to illustrate the address assignment of the modules, as shown in Fig. 9.2.

Fig. 9.2
An illustration depicts the address allocation of input and output modules. It includes analog input, P I W, analog output, P Q W, digital output, Q, and digital input, I with corresponding addresses. The P L C components include a power supply, a central processing unit, and 4 signal modules.

Address allocation of input and output modules

Where PS is a power supply module, CPU is a central processing unit (control module), SM are input and output modules.

  • The address of the first SM module (the left-most) closest to the left CPU;

  • The address of the analog input (AI) module is PIW256, PIW258, PIW260–PIW270;

  • The address of the analog output (AO) module is PQW256, PQW258, PQW260–PQW270;

  • The address of the digital input (DI) module is I0.0, I0.1, I0.2–I3.7 (32 in total), or IB0, IB1, IB2, IB3, or IW0, IW2, or ID0.

  • The address of the digital output (DO) module is Q0.0, Q0.1, Q0.2–Q3.7 (32 in total), or QB0, QB1, QB2, QB3, or QW0, QW2, or QD0.

  • The addresses of SM modules at other locations can be deduced by analogy.

If the 16-way DI module is inserted into the first SM location, the address I0.0 represents the first digital input, IB0 represents upper 8 bits, IB1 represents lower 8 bits, IW0 represents all 16 bits.

If the 32-way DI module is inserted into the first SM location, the address I0.0 represents the first digital input, IW0 represents upper 16 bits, IW2 represents lower 16 bits, ID0 represents all 32 bits.

  1. 2.

    Confirm the address programming method of PLC internal memory and data block;

    Take Siemens S7-300 PLC commonly used memory M and data block DB as an example.

    1. (1)

      When used in bits such as M0.0, M0.1, M127.7, DB1.DBX0.0, DB1.DBX 0.1, DB10.DBX 240.0, etc., DB1 represents data block 1.

    2. (2)

      When used by byte, such as MB0, MB1, MB64, DB1.DBB0, DB1.DBB1, DB15.DBB7, etc.

    3. (3)

      When used by word, such as MW0, MW2, MW64, DB1.DBW0, DB1.DBW2, DB3.DBW 64, etc.

    4. (4)

      When using double words, such as MD0, MD4, MD64, DB1.DBD0, DB1.DBD4, DB3.DBD 88, etc.

    5. (5)

      MD0 is composed of MW0 and MW2, MW0 is composed of MB0 and MB1, MB0 is composed of M0.0, M0.1, M0.2, M0.3, M0.4, M0.5, M0.6 and M0.7.

    6. (6)

      DB1.DBD0 is composed of DB1.DBW0 and DB1.DBW2, DB1.DBW0 is composed of DB1.DBB0 and DB1.DBB1, and DB1.DBB0 is composed of DB1.DBX0.0–DB1.DBX0.7.

    7. (7)

      By analogy, it should be noted that there should be no repetition during use. For example, if M0.0–M0.7 is used, MB0, MB1, MW0 and MD0 should not be used for other purposes.

  2. 3.

    Find and modify the address of the PLC programming port and communication port: plug in the programming cable (RJ45 network port or USB port) between the PLC and the programming PC, open the PLC programming software, find the PLC. Set all the default communication ports on the PLC to the address you arranged. If multiple PLCs are connected through fieldbus or industrial Ethernet, arrange that the communication addresses of each PLC should not overlap.

  3. 4.

    According to the module models and positions in the actual PLC, insert modules of the same model modules in the corresponding position in the programming software, so that the address of the module in the software is determined as mentioned above.

  4. 5.

    Now you can start programming.

  5. 6.

    It should be noted that:

    1. (1)

      There is a corresponding relationship between the value collected by the analog input module in the PLC and the value of the actual process parameter. For example, the pressure is 0–1 MPa, the corresponding pressure sensor outputs 0–10, and 0–10 V is input to the AI module of PLC. The corresponding value in PLC is 0–27,648, such as the PIW256 analog input value of S7-300. Some PLCs may be corresponding to 0–32,768. Beginners must pay attention to this.

    2. (2)

      Similarly, there is also a corresponding relationship between the output value of AO module and the actual controlled parameters. For example, frequency converter’s 0–60 Hz, the value corresponding to the AO module is 0–27,648, such as PQW256. Some PLCs may correspond to 0–32,768.

    3. (3)

      When performing double-word floating-point operations inside the PLC, the PLC value must be converted into the corresponding process value before performing the normal calculation of energy-saving control. For example, if the pressure is 0.5 MPa, the value of the AI module’s PIW256 is 13824. It converts to 0.5 and then performs floating point operations. For analog output, it should also be converted into the corresponding output value of PLC. For example, if the inverter outputs 30 Hz, then the AO module of PLC outputs 27,648/2 = 13,824.

9.2 Getting Started with PLC Programming—Ladder Diagram

The commonly used programming language for PLC is ladder diagram, and the expression method of ladder diagram is similar to that of electrical diagram. In order to let beginners quickly master it, we will use ladder diagram programming method to describe the PLC programming method below. Since the ladder diagram programming methods of most PLCs follow the provisions of the international electrotechnical standards, the programming forms are basically the same.

9.2.1 “AND”

Only if the two conditions “A” and “B” are met, C will have output. The program in the PLC is shown in Fig. 9.3, and the logic relationship of “AND” is shown in Table 9.1. A and B are both normally open contacts.

Fig. 9.3
A circuit diagram is labeled AND. The circuit includes two input contacts, A and B connected in series and an output coil, C. The AND logic requires both inputs to be active to energize the output.

“AND”

Table 9.1 “AND” logical table

9.2.2 “OR”

One condition of I1.0 “or” I1.1 is realised, then Q8.0 will output 1 (or closed). The ladder diagram of PLC is shown in Fig. 9.4, and the truth table of “or” is shown in Table 9.2. I1.0 and I1.1 are both normally open contacts. Logic tables and truth tables mean the same thing.

Fig. 9.4
A circuit diagram is labeled OR. The circuit includes two input contacts, I 1.0 and I 1.1 connected in parallel and an output coil, Q 8.0. The OR logic requires either input to be active to energize the output.

“Or”

Table 9.2 “Or” truth table

9.2.3 “Output”

() means output, output logic 1 (or high) if the conditions are satisfied, and output logic 0 (or low) if the conditions are not met, the ladder diagram in PLC is shown in Fig. 9.5. The logic relationship of the “output” is shown in Table 9.3.

Fig. 9.5
A circuit diagram is labeled output. It depicts an input contact I 1.0 connected to an output coil M 8.5. The output is energized when the input is active.

“Output”

Table 9.3 “Output” logical table

9.2.4 “Set”

Set bit (S) indicates that it will be set to 1 when the conditions occur; when the conditions do not occur, the original state will be maintained without any action. For example, when A “and” B occur, C is set to 1. The ladder diagram of PLC is shown in Fig. 9.6, and the logic relationship of “set” is shown in Table 9.4.

Fig. 9.6
A circuit diagram is labeled set. It includes two input contacts, A and B in series, and a set coil, S. The set coil is energized when both inputs are active, setting the output.

“Set”

Table 9.4 “Set” logical table

The difference between the set (S) output and the immediate output () is that the immediate output () outputs low (0) when the previous conditions are not met; for the set output (S), when the previous conditions are not met, the output does not change state.

9.2.5 “Reset”

The reset output (R) resets to low when the previous conditions are met, and does not change state when the conditions are not met. For example, when I128.0 and Q12.0 meet the conditions, Q12.7 is reset to the low position, the PLC ladder diagram is shown in Fig. 9.7, and the “reset” logic table is shown in Table 9.5.

Fig. 9.7
A circuit diagram is labeled reset. It includes two input contacts, I 128.0 and Q 12.0 in series, and a reset coil Q 12.7. The reset coil is energized when both inputs are active, resetting the output.

“Reset”

Table 9.5 Logical table of “reset”

9.2.6 Data Transfer “MOV”

If you want to put the analog quantity AIW256 into the memory MW0, the PLC ladder diagram is shown in Fig. 9.8.

Fig. 9.8
A circuit diagram is labeled data assignment. It connects a move instruction, M O V from the input register, A I W 256 to the output register, M W 0. This assigns the data from the input to the output.

Data assignment

9.2.7 “ADD”

If you want to add the values in memories MW2 and MW4 and put the result into DBW0 of data block DB1, the ladder diagram of the PLC is as shown in Fig. 9.9. If you want to use floating point numbers for addition, you can use MD2 plus MD6. But you cannot add MD4 with MD2, because MD2 occupies 4 words, including MW4.

Fig. 9.9
A circuit diagram is labeled data plus. It includes an add instruction, ADD that adds the value of the memory word, M W 2 to the value of the data block, D B 1 dot D W 0, storing the result in a specified register.

Data plus

9.2.8 “SUB”

If MW6 is subtracted from DB1.DBW2 and the result is stored in MW8, the PLC ladder diagram is as shown in Fig. 9.10. If you want to do floating point subtraction, use MD6 to subtract DB1.DBD2.

Fig. 9.10
A circuit diagram for data subtraction. It includes two data blocks labeled D B 1 dot D B W 2 and M W 6 feeding into a subtraction block labeled S U B, with the output directed to M W 8.

Data subtraction

9.2.9 “MUL”

If the data MW10 is multiplied by MW0 and the result is stored in MW2, the PLC ladder diagram is shown in Fig. 9.11. If you want to perform multiplication with floating point numbers, multiply MD0 by MD10.

Fig. 9.11
A circuit diagram for data multiplication. It includes two data blocks labeled M W 0 and M W 1 0 feeding into a multiplication block labeled M U L, with the output directed to M W 2.

Data multiplication

9.2.10 “DIV”

If the data DB1.DBW4 is divided by DB2.DBW4, and the result is put into DB3.DBW4, the ladder diagram of PLC is shown in Fig. 9.12. If you want to divide by floating-point numbers, divide DB2.DBD4 by DB1.DBD4.

Fig. 9.12
A circuit diagram for data division. It includes two data blocks labeled D B 1 dot D B W 4 and D B 2 dot D B W 4 feeding into a division block labeled D I V, with the output directed to D B 3 dot D B W 4.

Data division

9.2.11 Counter C (Counter)

If I0.0 has a rising edge pulse, the counter C2 adds 1, the counter is cleared when I0.1 is high, MW0 stores the current counter value, and the counter C2 is an up counter. Then the PLC ladder diagram is shown in Fig. 9.13.

Fig. 9.13
A circuit diagram of an up counter. It includes an up counter labeled C 2 with connections for the set, S, count up, C U, preset value, P V, current value, C V, and output, Q connected to M W 0. The input contacts are I 0.0 and I 0.1.

Up counter

If I0.0 has a rising edge, the counter C2 will be decremented by 1. When I0.1 is high, the counter will be cleared. When I0.2 is high, the number in MW2 will be put into the counter. MW0 will store the current counter value, and counter C2 will be a down counter. The PLC ladder diagram is shown in Fig. 9.14.

Fig. 9.14
A circuit diagram of a down counter. It includes a down counter labeled C 2 with connections for the set, S, count down, C D, preset value, P V, current value, C V, and output, Q connected to M W 0. The input contacts are I 0.0, I 0.1, and I 0.2.

Down counter

9.2.12 Timer T (Timer)

If both M0.0 and DB1.DBX2.0 are high, the timer T4 (3 s) starts, and when T4 (3 s) expires, M0.1 is reset. The ladder diagram of the PLC is shown in Fig. 9.15.

Fig. 9.15
A circuit diagram for a timer. It includes T 4, M 0.0, D B 1 dot D B X 2.0, and M 0.1.

Timer

9.2.13 Greater Than or Equal to (≥)

If MW6 is greater than or equal to MW256, set M0.7, and the ladder diagram of PLC is shown in Fig. 9.16.

Fig. 9.16
A circuit diagram for a greater than or equal to the comparator. It includes two data blocks labeled M W 6 and M W 256 feeding into a comparator block labeled greater than or equal to, with the output directed to M 0.7.

Greater than or equal to

9.2.14 Equal to (=)

If the number in the internal data area VW128 is equal to VW2, put VW4 into VW100, and the PLC ladder diagram is shown in Fig. 9.17.

Fig. 9.17
A circuit diagram for an equal to the comparator. It includes two data blocks labeled V W 128 and V W 2 feeding into a comparator block labeled equal to, with the output directed to V W 100 from the M O V block.

Is equal to

9.2.15 Less Than (<)

If the number in MW200 is less than the number in MW240, set Q12.7, and the ladder diagram of PLC is shown in Fig. 9.18.

Fig. 9.18
A circuit diagram for a less-than-comparator. It includes two data blocks labeled M W 200 and M W 240 feeding into a comparator block labeled less than, with the output directed to Q 12.7.

Less than

9.2.16 Greater Than (>)

If MW128 is greater than MW0, subtract 1 from MW128 and put it back into MW128. The ladder diagram of PLC is shown in Fig. 9.19.

Fig. 9.19
A circuit diagram for a greater-than-comparator. It includes M W 128 and M W 0 entering the greater than block, followed by a SUB block with inputs M W 128 and 1, and produces M W 128.

Greater than

9.2.17 Less Than or Equal to (≤)

If MW64 is less than or equal to MW62, then MW8 is output to the analog quantity AQW256, and the ladder diagram of PLC is shown in Fig. 9.20.

Fig. 9.20
A circuit diagram for a less than or equal to. It includes M W 64 and M W 62 entering the less than or equal to block, followed by a M O V block with input M W 8, and produces A Q W 256.

Less than or equal to

9.2.18 Rising Edge Action (P)

If I0.0 changes from low to high (there is a rising edge), set Q0.7 to high, and the ladder diagram of PLC is shown in Fig. 9.21.

Fig. 9.21
A circuit diagram for a rising edge action. It includes an input labeled I 0.0 feeding into a rising edge detection block, with the output directed to Q 0.7.

Rising edge action

9.2.19 Falling Edge Action (N)

If M0.0 and M1.1 change from satisfying conditions to not satisfying conditions (falling edge action), then subtract 1 from MW8 and return to MW8. The ladder diagram of PLC is shown in Fig. 9.22.

Fig. 9.22
A circuit diagram depicts the falling edge action. It includes M 0.0, M 1.1, and N in series, M W 8, and 1 entering the SUB block to produce M W 8.

Falling edge action

9.2.20 Per Second Pulse Program

In some PLC instructions, there is a special per second pulse bit, but there are also no per second pulses in some PLCs. The following is a way to make M127.0 go high once per second, and only execute it once. The length of time can be changed by changing the timer time. The PLC ladder diagram is shown in Fig. 9.23.

Fig. 9.23
A circuit diagram depicts a per-second pulse program. It includes a normally open contact labeled M 127.0 connected to a timer labeled T 0. The timer has settings for 1 second, with parallel connections of M 127.0, R and M 127.0, S, indicating reset and set functions.

Per second pulse program

The working sequence of the PLC program is executed sequentially from top to bottom. The program is executed to the end, it returns to the top program. In the PLC working cycle from top to bottom, M127.0 becomes low again, and the T0 timer starts counting again; after 1 s, T0 becomes high, M127.0 becomes high, and the program executes downward until the end, and M127.0 is always high state, the program returns to the top and then executes from top to bottom. Since M127.0 is high, T0 stops timing. Then, repeat the above process.

9.2.21 PID Closed-Loop Control

PID closed-loop control can be performed in PLC. The ladder diagram of PLC is shown in Fig. 9.24. When M0.0 in the figure is closed, PID control starts. The number of PIDs varies from PLC to PLC. When applying PID in PLC, define the input address, output address, and set value storage address, and then define the data block address corresponding to the P, I, and D parameters. So that the operator on the man–machine interface or the upper computer can modify the actual situation. Then set the positive and negative effects of PID control (such as heating and cooling control), sampling period, maximum output, minimum output and other parameters (different PLCs will be different).

Fig. 9.24
A circuit diagram depicts a P I D closed-loop control. The diagram includes a normally open contact labeled M 0.0 connected to a P I D controller labeled P D 1. The P I D controller has inputs labeled D B 1 dot D B D 50, 52, 54, 56, and 58, and an output labeled D B 1 dot D B W 60.

PID closed-loop control

PID can be programmed by yourself and use the scheduled interrupt program to calculate the PID output value. The method is as follows:

$$\begin{array}{c}{u}_{i}={u}_{i-1}+\Delta u\\ \Delta u=P\left({e}_{i}-{e}_{i-1}\right)+I{e}_{i}+D({e}_{i}-2{e}_{i-1}+{e}_{2})\end{array}$$
(9.1)

Among them, ui is the current PID output value, ui−1 is the last calculated PID output value, and Δu is the current PID correction value. ei is the error this time, it is equal to the set value SV of the controlled parameter minus the actual value PV, ei = SV − PV, ei−1 is the error last time, ei-2 is the error before ei−1. P is the proportional parameter, I is the integral parameter, and D is the differential parameter. The three parameters P, I, and D need to be adjusted to obtain the best control effect.

9.3 PLC Programming Software

9.3.1 Module Configuration

Take the module configuration in Fig. 9.2 as an example.

In Siemens STEP7 programming software, insert 5A power supply module “PS307 5A”, CPU314 module “CPU314”, 16-way digital input module “DI16 × DC24V”, 16-way relay output module “DO16 × Relay output”, 8-way analog input module “AI8 × 12-bit”, 4-channel analog output module “AO4 × 12-bit”. As shown in Fig. 9.25. The third slot is reserved for the interface module IM, which is used to connect expansion racks to increase the number of SM modules in a larger control system.

Fig. 9.25
A screenshot for the installation of the power supply, C P U, and I or O cards in a software interface. The window has a hierarchical tree view of the tools, a table with configuration settings, and various tabs and menus for navigation and setup.

Installing the power supply, CPU, and I/O cards

After selecting the hardware board, remember the I/O address of each board shown in Fig. 9.25.

The address of the 16-way digital input module SM321 is I0.0–I3.7, IB0–IB1, IW0.

The address of the 16-way digital output module SM322 is Q4.0–Q5.7, QB4–QB5, QW4.

The address of the 8-way analog input module SM331 is PIW288–PIW302.

The address of the 4-way analog output module SM332 is PQW304–PQW310.

The address of default MPI communication port is 2, as shown in Fig. 9.25.

The MPI port is a communication bus defined by Siemens itself. If there are more than one S7-300 stations in the same network, the address of the MPI port of “CPU314” needs to be modified.

After completing this step, the subsequent software program will know how many modules in this PLC system has, what modules they are, and where they are.

9.3.2 Software Programming

Open the instruction tree of STEP software on the left, drag the normally open contact , normally closed contact and set in the “bit logic” into segment 1, and input the corresponding addresses I0.0, I0.1 and Q4.0. As shown in Fig. 9.26. I0.0 is a normally open contact. I0.1 is a normally closed contact.

Fig. 9.26
A screenshot of program segment 1 in a software interface. The overlaid window has a ladder logic diagram with contacts and coils, a toolbar with programming tools, and a menu for editing and compiling the program. The segment includes labels for the components and their addresses.

Program segment 1

The meaning of this program is: if the digital input bit I0.0 is closed, and the digital input bit I0.1 has no action, then the digital output bit Q4.0 is set to close.

STEP7 complies with the international standard IEC 61,131-3.