Control Project with Ladder Logic Programming

Hey guys! hope you are all very well. Today I come to you with a new process to learn, program, and simulate for practicing ladder logic more and more. The process we are going to implement today is a very common process that could be there in many many industries which is a silo process that aims to automate the process of filling containers or bottles with a liquid. Figure 1 shows the complete scene of the process including the system components, switches, indicators, sensors, and actuators that are integrated to make the system operate. Briefly and before going into deep details, let’s state what the system does and how it operates. Well! The system automatically fills the boxes that are traveling on the conveyor which is driven by a motor. They are filled with a liquid stored in the silo shown in the middle of figure 1. The process operates continuously from the moment we hit the start button shown in the tail of the control panel on the left side of figure 1 until termination of the process is requested by pressing the stop push in the same panel. Many inputs and outputs are utilized to address the sensors, switches, push buttons, motors, and valves. So let’s get started on the project.

Project Logic Philosophy

As we have thought, the first step in the development of a control system is to sit with the operator or the client to get to comprehend how the system works including all details and scenarios. So the system starts by receiving the kickoff by hitting the start pushbutton. Unless there is no stop has been requested or faults, the box moves on the conveyor that is driven by a motor until it reaches the filling station which is at the silo. At that position, the box should stop for filling, and the flow of the liquid starts and goes on until the box is full. Then, it is time for the box to continue its journey to the end of the conveyor and another box commences a new trip for filling. At any time a stop is requested, the system should halt until a command to continue is presented. Operation status should be shown thanks to indicators showing the run, fill, and full status. Now, friends, that’s all the story we have gotten by sitting with the client or the operator who requested us to implement his system. have understood the requested silo operation? Well done! I know it is simple to understand but what is the next step? Yes, the next station in the project is to interpret the story narrated by our client into operation sequences and conditions and determine the list of inputs and outputs to get that project done. 

Logic sequence

The aims of the project we are going to implement is to implement the ladder logic program that execute the narrated logic of SILO by client. The logic can be resumed in the following sequence of operations: 

  • The batch process can be started or stopped at any point of execution 

  • Indicator lamps should be appropriately controlled to present the run, fill, and full statuses of the operation throughout the whole process.

  • All actuators should be stopped at any time a termination requested by the end user

  • The FULL lamp is switched on showing the full status of the box as long as the box at its filling position under the silo.

  • After converting the logic narrative received from the client into clear logic sequences, now the time comes to list the inputs and outputs to use to accomplish these logic sequences. In the next section, the complete list of inputs represented by sensors and switches and outputs represented by motors, valves, and light indicators. 

Project I/Os

Figure 2 shows the location and address of all inputs and outputs used in this project. Also, the table below lists all inputs and outputs of the project including descriptions and addresses. The control panel on the lift shows two inputs that enable an operator to start and stop the operation. Also, it shows indicators showing the status of the operation throughout the whole process. These status indicators are RUN. FILL, FULL to show the process is running, the box is filling, and when it is full. Also, it shows the motor that drives the conveyor and the proxy sensor that detects the position where the box is exactly under the silo and ready to be filled for initiating the filling process. Also, a level sensor is utilized to determine when the box is filled up to full. In addition, a valve is used to open and close the silo.

  Table 1: list of inputs and outputs of the project     

IO

type

Description

Address

RUN

Output

Indicator lamp to show the running status

Q2.2 for siemens

O:2/02 for AB

FILL

Output

An indicator lamp to show the filling process is in progress

Q2.3 for siemens

O:2/03 for AB

Full 

output

An indicator lamp to show the full status of the box

Q2.4 for siemens

O:2/04 for AB

MOTOR

Output 

The main motor drives the conveyor to take the boxes through their journey  

Q2.0 for siemens

O:2/00

Valve

Output

A solenoid valve to control the silo to open or close according to the logic

Q2.1 for siemens

O:2/01 for AB

Start

input

A pushbutton enables the operator to start the process

I1.0 for siemens

I:1/00 for AB

Stop 

Input 

A pushbutton enables the operator to stop the process

I1.1 for siemens

I:1/01 for AB

Level sensor

input

A level switch to detect the level of the liquid being filled into the box

I1.4 for siemens

I:1/04 

Proximity switch

Input 

A proximity switch to sense the position of the approaching box 

I1.3 for siemens

I:1/03 


Logic Flow Diagram

Guys, this is not an essential step to accomplish the project development. But this is one of the professional ways to represent graphically the design of the logic. Figure 3 depicts the logic of the silo project graphically. You can follow paths of logic to understand how we can chart the logic flow running in our head graphically to help in writing the correct and precise code. For example, to run the conveyor motor, the start button is pressed and checked if a stop is requested, if not the run lamp indicator will be energized and latched as long as no stop is requested. Then check if a fill process is in progress. If not then the motor of the conveyor will keep running. But the question is how to know if the fill is in progress or not. On the left side of the chart, you can see if the proxy switch is on and the level switch does not show full, then the filling process is in progress. Also, if the level switch shows not full then the valve will keep open to continue filling the boxes.

Ladder Logic Programming of the project

Now we have just arrived at what you are waiting for! I know you want to practice programming and then simulation. Figure 4 shows the program of the silo project while the left part shows the initial state of our simulation. The first rung is to energize the run indicator when the start pushbutton is hit and no stop is requested noting the latch as we have learned. In the second rung, knowing the run is the status of the operation and no filling process is in progress, then the motor is running. In the third rung, the filling status can be determined by the proximity switch showing the boss is at the position were ready to fill and full status is not there. While the full status can be determined in the last rung when the level switch is on showing level has reached the full limit of the box and pursuing the proximity switch tells the box is on the filling spot. Before that very last rung, the valve of the silo can be determined by having the fill status on and the full status is not there yet.

Simulating the program

After showing you the code of the design step by step based on the received narrative from the client, deciding the IOs, and charting the logic of the program. Now it is time to simulate the program to make sure the design and the code are correct. Figure 5 shows the running status of the operation. By hitting the start push button, the conveyor move by the driving motor and takes the box to the filling station. Guys, notice please the run indication lamp is on in the code highlighted in yellow and on the control panel on the right as well. Also, notice the motor is spinning.

Figure 6 shows the box stopped for filling at the silo thanks to the proximity switch. Notice the run lamp is on and the motor is de-energized for letting the box fill. Also, notice the fill status is on and the lamp indicator is energized. Also, see the valve is open to let the liquid fills the box.

Figure 7 shows when the box comes to be full. You can see the run operation status is still on. And the full status is indicated by the full lamp. Also, once the box is full, the motor starts spinning again taking the filled box out from the line to let other incoming boxes come.

And finally, after filling to full, the filled boxes keep going as shown in figure 8 to the end of the conveyor. You can see the run status is still energized and the motor is spinning. After that, the cycle will continuously be repeated until the stop is requested. So let us see the termination of the process.   

on the other hand, when the stop push button is pressed requesting termination of the process, the motor stops spinning and the whole process stopped until a proceeding instruction is issued by hitting start again. Guys, that is very common to enable the machine operator to stop for any emergence or do any repainting before baking to work again 

What is next???

First of all, I would like to thank you all for continue following the tutorial till this very point. As you see we are taking projects from real life. It might be small projects or processes but that is what you will see in real industry life. So next time we select one of the common processes in the real life and do the same design, coding, and without question practicing using our simulator. So please be ready, study hard, and let’s meet for our next project.

Design an Automatic Door with Ladder Logic Programming

Hi Guys! hope you are doing great today! We come this time with a complete project to work out together starting from the point at which we sit with the client and receive the logical narratives that represent what they want to implement. We are going to start with a simple project this time and continue increasing the scale and complexity of the requirements through the incoming tutorials. The project we are going to implement today is one of the most common tasks that we can find in every place in our real life which is the control of the garage door. That could be found in private property or commercial buildings or public garages. Too many things need to be controlled in garage doors and several scenarios could come to your mind. However, take it as a rule of thumb that we design our program based on the requirements specified by the customer not based on our thoughts. 

Project Introduction

  Figure 1 images the project with details. As you can see, it is a garage door that is required to be controlled to open, and shut down flexibly and safely. Flexible operation means it needs to open and shut down at any time exchangeable showing operation indicators when it is opening and when it is closing. And safe operation means taking care of the motor and system components when the change between open and shut down commands for not make any kind of hazard to the system components.

Control Logic Philosophy

The control logic philosophy is a common term you will be used to hear when you meet with the clients who are going to discuss with you the control logic and all scenarios which will be interpreted by you to the control narratives. The following control narratives of the project were decided and designed based on the discussion with the customer who requested the control system and it is as follows:

  • There will be a control panel with a front panel that has push buttons, indicator lamps, and switches that enable the end user of operating the system including all functions of the control system. that includes one push button for commanding the door to open and one other push-button for instructing the system to shut down the door. In addition, one indicator to show the door is opening, one to show the door is closing, and one to show the door is in ajar status or open slightly. 

  • The system needs to have a safety emergency stop switch to stop the operation at any instant

  • The system also can switch between opening and shutting down at any time when it is allowed to do

  • The system needs to be safe and secure from any hazards that might occur due to wrong logic design for example requesting one operation which might cause any damage to the system components like motors 

  • Please note that when the customer lists the requirements he or she does not know about the deep technical details like the system components that the system needs including push buttons, indicating lamps, switches, sensors, and actuators because he or she is supposed to talk at a high level while telling you the requirements and you the one who should translate his requirements into logic narratives and deciding for the whole system components. 

Logic narratives and system components

Guys! now our work has just started by translating the above high-level requirements into detailed logic narratives and also deciding all system components and input-output lists. Figure 2 shows the garage door project with the system components that include actuators, sensors, switches, push buttons and indicator lamps.

System components

Table 1 lists the system components which include the main motor running up and down for opening and shutting down. Limit switches for preventing the door from opening and shutting down. Also, it includes the indicators and operation status while opening and closing and ajar status as well. Also, one emergency stop is utilized to stop the process at any time. The table includes the component name, description, the related IO type, and address. For example, the main motor of the door can be run in two directions to let it open or shut down. Therefore, two coils are connected through two digital outputs at addresses O:02/0 and O:02/01 which are the third output input card and the first and second channels. Also, two limit switches have been utilized to control the limits up and down to which the door can go. They are connected to two digital inputs at addresses I:01/03 and I:01/04 which are connected to the second digital input card at the 4th and 5th channels. In addition, two push buttons to handle the open and shutdown requests of the door have been connected at addresses I:01/00 and I:01/01 which are the first and second channels of the second digital input card. And the E-stop or the emergency stop has been connected to digital input at address I:01/03 which is the 4th channel of the second digital input card. Last but not least, three lamp indicators are connected via three digital outputs which are connected to addresses O:02/02, O:02/03, and O:02/04 which are the third digital output card in the rack and the third, fourth, and fifth channels respectively.

Table 1: Input-output list of the garage door project   

Component

description

IOs type

Address

Motor up 

Door motor in a clockwise direction 

Digital output

O:02/0

Motor Down

Door motor in Anticlockwise direction

Digital output

O:02/01

Limit switch

Limit switch to stop the door from over spinning

Digital input

I:01/03

Limit switch

Limit switch to stop door of over shutting down

Digital input

I:01/04

OPEN pushbutton

A pushbutton to request opening the door

Digital input

I:01/00

Shut down pushbutton

A pushbutton to request shutting the door down

Digital input

I:01/01

E-stop pushbutton

A pushbutton to request an emergency stop 

Digital input

I:01/02

Indicator lamp

Showing ajar door status

Digital output

O:02/02

Indicator lamp

Showing open door status

Digital output

O:02/03

Indicator lamp

Showing shutdown door status

Digital output

O:02/04


Project Logic Flow

Despite this is a simple and short project. And it is our first complete project. We are going systematically step by step to show you guys the right sequence to achieve a ladder logic project work. Again, we have to sit with the customer to get his requirements, and then we interpreted his requirements into a logical narrative and decided on the IOs list and all system components. Now we need to work professionally and create a flow chart that represents the program logic graphically as shown in figure 3. It shows when a request of opening the door has been requested, it checks for any emergency stop or a concurrent stop request is in progress, if no then it is clear and all set to energizing the coil of the contactors that drives the motor to run in Up direction for opening the door right away. Similarly, when a request to shut down the door. The emergency stop is checked and if is there an open door request in progress if the answer is no then it is all the settings to run the motor in the down direction to close the door by activating the related contactor to energize the motor in the direction to shut down the door.

Ladder Logic Program

Now guys after completing the design stage in which we have to design the logic and decide on the system components and the logic flow. It is time to program so let my friends open our program editor and write down the program rung by rung based on the flowchart we just have drawn in figure 3.  Looking into the ladder logic program, the first rung shows the opening handling request by energizing the output coils of the motor to run in the up direction and energizing the lamp indicator in case there is no emergency stop or a shutting down request in progress. Similarly, the second rung shows the shutdown request handling by activating the coils of the motor to run in the down direction and the indicator lamp to show the process of the shutdown of the door is in progress. That is true when there is no emergency stop is hit and no opening request is in progress. Also, you can see in both rungs for opening and shutting down the door of the garage that, the limit switches are used to prevent the door from opening or over closing for protecting the motors. At last, the third rung shows the indicator of ajar status as long as the door is not completely closed or when it is slightly opened.

Simulating the ladder logic program

Now guys! it is time to open our simulator to check if we already have written the correct logic or if there is something missed or wrong. Let me tell you something before testing your program you should hold and sit back and think about the test case scenarios for not to forget any case that might cause a failure to your program. For this very program we simulate, there are two main scenarios which are testing opening the door and testing closing the door. In addition to the main test case scenarios, there are edge test case scenarios which are testing requesting opposite operations like request opening while a shutdown process is in progress or requesting shutdown while an opening the door is in progress. Or testing requesting process while the E-stop is raised to check the safety of the operation. So let’s get started and take the test cases one by one to see if we have coded our program correctly or if there is an issue to fix.   

Testing opening the door

As you can see guys in Figure 5 in rung 2, the process of opening the door started by hitting the open push button, and because there is no emergency stop and there is no shutdown request in progress, the motor keeps spinning in the up direction and the opening lamp indicator keeps switched on. But up till when this operation will go on?

The process of opening the door is continued successfully until the LS1 is contacted at which the door has reached its final point for protecting the motor from overloading failure.

Also figure 7 shows the indicator of the door status on Ajar status whenever it is slightly opened or when it is not closed all the way.

Testing shutting down the door

Now let’s test the shutdown command by hitting the shutdown push button and notice the first rung in the program in figure 8. You can notice the door continues closing see the motor is spinning in the down direction and the indicator lamp showing closing is ON. But until when it will be stopped?

Figure 9 shows the end of the shutting down process of the door of the garage by having the limit switch LS2 reached that protects the motor from over spinning until it burned.

Testing requesting opposite operation while another is in progress

Figure 10 shows one of the edge test cases when the door was opening and all of a sudden operator requested to shut down the door. The test was successful and the operation can be reversed thanks at any time thanks to the good design by having the negative of each contact of the open and close in the other rung request as shown in rungs 1 and 2.

What’s next???

Guys! I would like to thank you all for sharing this tutorial up till this point hoping you all have enjoyed learning and practicing. And, be there with a larger project to take for the next time step by step learn, design, coding the ladder logic program, and enjoy simulating them as we are inside the factory.

Move Instruction in Ladder Logic Programming

Hi friends, today we are going to learn one of the most important instructions in the PLC ladder which is MOVE instruction by which we can move data between different memory storage including input, output, marker, and variables. Also, data of different data types and sizes can be transferred from source to destination and source memory locations. For example data types including char, string, integer, floating, time and date can be transferred between source and destination. Memory location like input, output, and marker memory area can be acting as source or destination. Furthermore, a mask can be utilized to customize and control the part of data to be transferred between source and destination. In that move with a mask, the instruction uses a source address, a destination address, and the mask as well. Guys, please do not worry if you are not understanding what is mentioned hundreds percent because you will get it in more detail and practice with the simulator as well. So let’s continue to learn and practice that MOVE and masked MOVE instruction. 

MOVE Instruction in Ladder Logic

Let’s start with the basic MOVE instruction. Figure 1 shows the typical block of a MOVE instruction in Siemens. It shows two sides for the input and output parameters of the block. Simply, the input EN input shows the contact that enables the block and triggers the movement of data between the source specified by the memory address given at input IN and the destination pointed by the memory address given at output OUT1. Also, output ENO denotes the status of the movement process to indicate the completion of the process.

Source and destination of the MOVE instruction 

As shown in the function block of the MOVE instruction shown in figure 1, there are source and destination which are an address of a memory area and can be expected to be byte, word, or double word DWORD or of any datatype like integer, character string, float, time, or date datatype. Some examples of memory size and datatype are listed in table 1 below:

Source (IN)

Destination (OUT1)

With IEC check

Without IEC check

BYTE

BYTE, WORD, DWORD

BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, TIME, DATE ,TOD, CHAR

WORD

WORD, DWORD

BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, TIME, DATE, TOD, CHAR

DWORD

DWORD

BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, REAL, TIME, DATE, TOD, CHAR

SINT

SINT

BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, TIME, DATE, TOD

USINT

USINT, UINT, UDINT

BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, TIME, DATE, TOD

INT

INT

BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, TIME, DATE, TOD

UINT

UINT, UDINT

BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, TIME, DATE, TOD

DINT

DINT

BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, TIME, DATE, TOD

UDINT

UDINT

BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, TIME, DATE, TOD

REAL

REAL

DWORD, REAL

LREAL

LREAL

LREAL

TIME

TIME

BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, TIME

DATE

DATE

BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, DATE

TOD

TOD

BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, TOD

DTL

DTL

DTL

CHAR

CHAR

BYTE, WORD, DWORD, CHAR, Character of a string1)

WCHAR

WCHAR

BYTE, WORD, DWORD, CHAR, WCHAR, character of a character string1)

MOVE instruction in ladder logic

Figure 2 shows one very basic ladder logic rung that demonstrates the usage of a MOVE instruction. It consists of a contact “trigger” to control activation of the move instruction, the MOVE instruction block which has source and destination memory address MW10 and MW12 respectively. The source and destination in this example represent integer data types.

Guys! Let's simulate this rung to learn together how the MOVE instruction works. Figure 3 shows the case when the MOVE instruction is not activated, in that case as shown in figure 3 the data is not transferred from source to destination.

But after activating the MOVE instruction by enabling the EN of the MOVE block using the contact M0.1, the data is moved from the sources to the destination.

It is important to let you guys know, this MOVE function block can be used to move different types of data types as listed above by providing the address of both source and destination.

Massive move instruction using ladder logic

Friends! Sometimes we need to move arrays of data. So do your think we are going to move one by one? The answer is yes it is possible but that is too boring to code and the program is getting very lengthy. So there is another version of MOVE instruction that enable us to move a massive amount of variables which is MOVE_BLK instruction. Figure 5 shows the function block of the massive move instruction in a very basic example ladder logic rung. As you see friends, similar to basic MOVE instruction, it has EN and ENO for enabling the instruction and returning its status showing the completion status. Also, it has an input source denoted by the address of the memory storage of the source and the address of the memory location of the destination as well. But you notice that time there is an array of variables or datatypes for source and destination. And there are other parameters called count which denotes the number of items to be moved from source to destination.  Let’s simulate this MOVE_BLK instruction to see how it works and the virtues of moving many data items in one command.

Figure 6 shows the execution having the block not activated. On the right part the memory of the source and the destination show array of 5 integers for each. You can notice guys there is no data moved.

Figure 7 shows the data is moved from source to destination after activation of the MOVE_BLK instruction. But wait for a second! Why not all the data been moved? Yes, you are correct friends, because we use count parameters to control how many variables or data items are to be transferred. And it has 3. So the first three data items have been moved only.

MOVE instruction in Allen Bradley

 Figure 8 shows a simple program that demonstrates the ladder logic rung that uses MOVE instruction. It shows the input contact I:3/0 triggers the move instruction block to activate it and enable data transfer from the 16bits input I:1 to the 16bits output O:2. On the left side, you can see guys the inputs are mirrored at the output by using the MOVE instruction. Now let’s see how we can control the data transfer using a mask to allow only selected bits to be transferred and prevent the others.

Masked move instruction

The idea behind the masked move instruction is that we utilize an array of bits to be used as a mask to filter the data that are being transferred. For example Figure 9 shows a very simple ladder logic rung that uses a masked MOVE instruction to customize the data transfer from source to destination. The MVM instruction has three parameters as shown in figure 9 which are: the source, destination, and mask. All of them are addresses of the memory area. For the very given example here, the source is the 16 bits input at I:1/0 and the output is the 16 bits output at O:2/0 while the mask is the 16bits input at I:3/0. Before we are going to simulate this example, you should expect that the data will be transferred from source to destination based on the status of every bit in the mask data.

Simulating masked move instruction

In the first test case shown in figure 10, the data to be transferred is ready at the source but nothing is transferred to the destination because the activating contact I:3/0 is not enabled. So let’s switch it on and see what is happening!

Now we enabled the activation switch at I:3/0 but still, data are not transferred from source to destination! Is there a problem in coding or logic? No, that is because the mask bits are set to off that is why bits of the source are not enabled to be transferred to the destination. Now, let us enable the masked bits and see what’s going on?

Yes! Now guys you can see all bits of the source have been transferred to the destination as long as their mates at the masked bits are enabled. But those who have their mates of bits at the mask are disabled and are still prohibited from being transferred. Like this, you can see friends how we can move data without control as when we use the MOVE instruction. In addition, you see how to control that data transfer by using mask bits as a filter. Do not forget, the move instruction can be utilized to move different data types, variables, memory sizes, and data blocks. So, it is a great tool for you as an experienced ladder logic programmer to flexibly move data between your data variables and memory location through long the program you are writing.

What’s next???

Guys! I would like to thank you for being here with me up till this point of our tutorial hoping you are really happy with it and seeing it is useful. Now guess what? I have a gift for you which is our next tutorial. It is not like every time we come up with another aspect in ladder logic to learn and enjoy practicing simulation. Now we come to the point that we should be counted as real ladder logic programmers because we have learned a lot and practiced more topics. So, it is time to practice a complete project string from reading the logic chart, understanding the logic narrative, and interpreting it to a complete successful ladder logic program. So next time will come up with a complete process with a logic diagram and requirements and practice together step by step. You can consider it as a great workshop in which you will apply all your knowledge base and what you have learned so far to take the responsibility as a PLC ladder logic programmer to convert the logic narrative and diagrams into a real ladder logic program. The work will include deciding the required logic components including switches, relays, inputs, outputs, digital and analog. Then will do a flow chart showing the logic flow based on which the program will be written. And lastly, the coded program will be applied to the plc simulator to verify the logic and make sure it is functioning correctly according to what has been requested at the beginning from the customer. So please be ready, make some revisions, and be there when we are going to learn by practicing a real project as in the real life and industry problems.

Bitwise Logic Operators in PLC Ladder Logic Programming

Hello friends, all of us know that PLCs are nothing but the smartest migration from relay logic control to programmable logic control. Also, you know clearly that, logic is the heart of any programming language, and the same is applied to ladder logic programming. Bitwise operators represent the logical operations including the basic logical operations like AND, OR, and NOT and the derived logical operations like NAND, NOR, and XOR. in most cases, for each bitwise operator, there are inputs based on which the output can be decided. Some of these bitwise operators have two inputs and some have only one input.  In this article, we are going to present how we can use these bitwise logical operators and their instructions with examples and practice using the PLC simulator.

Logic Gates basics and concepts

Despite we have talked about these, basics and concepts in one of our articles, we have seen it’s good to remind you briefly that basic is the ground for your foot to stand coding logical operations in ladder logic programming. for defining a logical operation, there is a truth table that shows the combinations of the inputs and the resulting output. For example, the bitwise operator that has only one input like “NOT”, has only two possible value for input which is high or low, True or False. And it has one output which is the negate of the input. So it is either TRUE or FALSE. To sum up, for every bitwise operator instruction, we are going to discuss its truth table showing the states of the possible combinations of its inputs and the resulting output as well. In addition, one example using ladder logic programming shows the operation practically. We come to see it is important to list the bitwise operators in table 1 below shows the inputs, output, and the symbol of the logical gate that is equivalent to that bitwise operator.

Table 1: the bitwise operator list 

The bitwise operator

Inputs

Output

AND

2

1

OR

2

1

NOT

1

1

NAND

2

1

NOR

2

1

XOR

2

1

AND bitwise operator

Guys, we can see the truth table of the AND instruction as tabulated in table 2. It shows the operator has two operands input A, and input B, and one output. The inputs can be switches, sensors i.e. limit switch for example while the output is a digital output to switch one actuator i.e. motor.

Input A

Input B

Output

False

False

False

True 

False

False

False

True

False

True

True

True

Also, Fig. 1 shows the symbol of the AND gate which is equivalent to AND bitwise operator. It shows two inputs A and B and only one output. Let us see that practically in ladder logic programming showing how to implement this with example.

Bitwise Operator AND in Ladder Logic

Figure 2 shows our first simple rung that codes the logic of AND bitwise logic. It shows two contacts A and B are connected in series to decide output AND-RES based on AND logic shown in truth table 2. So let us go .simulating this thing we just coded and see the logic in the application.

Simulating basic AND in ladder logic

We typically have 4 cases according to the truth table. Figure 3 shows the case when inputs A and B are low. The output shows low as shown by the coil AND-RES.

Now let us switch on input A  and keep the second operand B false as shown in fig. 4, output AND-RES output still off.

So let us try to switch the other input, B ON, and set input A off, as shown in fig. 5 output is off as represented by coil M0.2, tag AND-RES.

At last, when both inputs A and B are ON as in Fig. 6. Now only the output comes to turn ON.

Guys!, you know for sure have got to know that, with AND logic, for having the output turned high both inputs A and B must be high. 

Ladder logic in Siemens and most other brands offer the facility to perform AND between byte, word, and Double word memory space as shown in Fig. 7 shows the AND block.

Simulate AND block in ladder logic

Figure 8 shows the usage of AND block for byte data type and it is possible to do the same with a word or double word data type. The instruction block applies to byte, word, and double word data types. However, for showing one example, Fig. 8. Shows the process of byte datatype. It shows the inputs A and B represented by memory bytes MB1 and MB2 while the output is represented by MB3. The operation shown in Fig, 8, can show the AND logic between two bytes that hold values “10001110” and “00001111” so the output represented by MB3 shows the value “00001110”.

OR bitwise operator

This logic gate has two inputs and one output like the “AND” gate. Like its name, the output comes true when either input A or input B comes true as shown in Fig. 4.

Table 3 lists the truth table of the “OR” gate. It lists all possible combinations of inputs and the output status as well. It shows that the output comes to true when input A or input B comes to true.

Input A

Input B

Output

False

False

False

True 

False

True

False

True

True

True

True

True

OR bitwise in ladder logic

Figure 10 depicts the very simple rung of ladder logic that represents OR bitwise operation. It shows two inputs A and B connected in two parallel branches to give the output. So let us simulate that very OR code and apply the cases listed in the truth table in table  3.

Simulating  OR bitwise operation in ladder logic

Typically as listed in table 3, the output is low when both inputs are OFF as shown in Fig. 11.

Ohh, you can see output comes ON when input A is ON and input B is OFF as shown in Fig. 12.

Also, output comes ON when input B is ON and input A is OFF as shown in Fig. 13.

Also, you can see output comes ON when both inputs A and B are ON as shown in Fig. 14.

That concludes two things guys, in OR bitwise logic, for having output ON, there must be at least one of the inputs High. 

OR block in ladder logic

Ladder logic in Siemens and most other brands offer the facility to perform OR between byte, word, and Double word memory space as shown in Fig. 15 shows the AND block.

Simulate OR block in ladder logic

Figure 16 shows the usage of OR block for byte data type and it is possible to do the same with a word or double word data type. The instruction block applies to byte, word, and double word data types. However, for showing one example, Fig. 16. Show the process of byte datatype. It shows the inputs A and B represented by memory bytes MB1 and MB2 while the output is represented by MB3. The operation shown in Fig, 15, can show the OR logic between two bytes that hold values “00001111” and “11110000” so the output represented by MB3 shows the value “11111111”.

The bitwise “NOT”

This logic gate has only one input and one output. In a very simple language, the output is the inverted logic of the input. So when the input is true, the output would come to false and vice versa as shown in  Fig. 17.

Table 4 lists the truth table rows of all possible combinations of input and output.

Input

Output

True 

False

False

True


Figure 18 depicts a very simple example of a ladder logic rung that shows the output “NOT-A” is the negate logic of the input A.  

Simulating NOT bitwise in ladder logic

Now let's simulate the two cases listed in table 4 when inputs A is high and when it is low as well. Figure 19  and 20 show the output is the negate of the input using the NOT bitwise logic.

Now Guys we have gone through the basic bitwise logic. So how about the other bitwise logic that is formed by combining these basic bitwise operators like XNOR? let us simulate XNOR.

XNOR in ladder logic

XNOR is the negative logic of OR bitwise or you can name it as NOT-OR. Table 5 lists the combination of its two inputs and its output. It shows clearly that, the output becomes true when inputs are equal i.e. both inputs are true or both are false. Yes! Exactly, that’s why this bitwise operation is used when we need to compare two inputs if they are equal or not.

Input A

Input B

Output

0

0

1

0

1

0

1

0

0

1

1

1

Figure 21 shows the symbol of the “XNOR” logic gate showing inputs and output of the bitwise operation.

On the other hand, Fig. 22 shows a sample ladder logic of an “XNOR” logic gate implementation. It shows that there are only two ways to have the output in a TRUE state which are by set both inputs TRUE or setting both FALSE. So let us apply this to our simulator and test cases listed in table 5.

Simulating XNOR bitwise operator in ladder logic

Figure 23 shows the test of simulating XNOR when both inputs are low. You can notice friends that output comes to high.

The same thing when both inputs are high, outputs show high as shown in Fig. 24.

Ohh, you can see when one input is not matched with the other that leads to low logic of the output. That concludes the XNOR gives a high logic output when only both inputs have the same logic; otherwise, it gives a low if they are different.

What next???

Before naming the next tutorial, I would like to let you know that there are many other bitwise operators like XOR, NAND, NOR, etc. this bitwise logic can be programmed in the same scenario we demonstrated above. Now, let me tell you that the next tutorial will be about one of the most important instructions ever in the PLC that will ease the data transfer between memory locations including all data types bytes, word, Dword, etc. it is the MOVE instruction in ladder logic. So be there to learn and practice that good one.

Sequencer Output Instruction in PLC Ladder Logic Programming

Hi friends, today we are going to learn a good technique to run multi outputs in sequence. In another word, when we have some output that is repeatedly run in sequence. In the normal or conventional technique of programming we deal with them individually or one by one which takes more effort in programming and much space of memory. So instead we can use a new technique to trigger these outputs in sequence using one instruction which will save the effort of programming and space of memory. In this article, we are going to introduce how to implement sequencer output instruction. And practice some examples with the simulator as usual. Before starting the article, we need to mention that, some controllers like Allen Bradley have sequencer output instruction and some has not like Siemens. So we are going to give one example for each case showing how to code the equivalent to the sequencer output instruction in the PLCs that does not support this instruction.

Sequencer output instruction 

Figure one shows the block diagram of the process. The instruction takes the input data from the file, array, and data block and sequentially relays it to the outputs to trigger them sequentially.

Figure 2 shows the block of the sequencer output instruction showing input and output parameters. The file parameter is the first input parameter showing the address of the reference sequencer file. In addition, the mask input is to receive the address of the mask or the data block of which the instruction will move the data sequentially before relaying it to the output. Furthermore, the dest parameter is an output parameter that shows the address of the output to which the sequence bits will be applied. And the control parameter is the storage words for the status of the instruction, the file length, and the position of the pointer in the data file or array. Also, the length parameter holds the number of steps to move in the data file to the output destination sequentially. And position parameter holds the location of the pointer in the data file.


Block description and example of the sequencer output instruction

Figure 3 shows an instance of sequencer output instruction QSO. The QSO instruction steply moves through the input data file or array or data block and transfers sequentially the data bits from the data file to the output (destination word) through the mask word. The status of the instruction can be shown in the DONE (DN) bit. You should notice my friends that after the data transition is done the position is reset to the initial position or the first step.

Ladder Logic Example

Now guys, let us move to the ladder logic coding. So how does that sequencer output instruction work in ladder logic? Well!  Figure 4 shows a complete example of QSO instruction that is used in Allen Bradley to handle the sequencer output process, it shows one rung that has a start and stops push buttons from left to right at address I:0/0 and I:0/1 respectively to control the starting and stopping of the sequencer output processing. Next, you can see input I:0/2 which is used as a sequencer process flag to switch on or off the sequencer process. So, if the start PB is requested when no emergency stop and the sequencer on input is ON, the QSO is enabled and the data at address #B3:0 will be moved to dest at address O0:0 though the mask word at address 000Fh starting from position 0 with length 4.

Figure 5 shows the data file that the QSO uses to transfer sequence data bits to output. It shows the bits B3:0, B3:1, B3:2 & B3:3 are set to 1 for reference. So, when the sequencer ON input is set to high (I:0/2). The output Q:0/1 will be turned on based on the data in the data file shown in fig. 5. In that case, the length is 4 and the position is 1.

And when the sequencer flag I:0/2 is switched on next time, output O:0/2 will be switched ON. In that case, the length is 4 and the position is 2 as shown in Fig. 6.

In the third time, the sequencer flag is turned ON, the output O:0/3 will be turned ON and the length and position are updated to 4 and 3 respectively as shown in Fig. 7.

When it comes to the fourth time of switching the sequencer flag I:o/2, the output O:0/4 will be turned high and the position will be at 4 and length is 4 as shown in fig.8. At that time, the process is reset and position reset to 1.

The previous example shows how it is simple to control a bunch of outputs that are required to run in sequence with only one rung of the ladder program and using only one instruction which is QSO in Allen Bradley. This merit helps to save the memory space and time and efforts of programming and troubleshooting as well because the program will be shorter and more readable. However, still, some brands have not supported such instructions even the big names like siemens. That can not be counted as limitations but they are banking on there being a way to implement such logic. So, it is very beneficial for you guys to go implement together a piece of code (ladder logic) that is equivalent to such instruction for performing the function of sequencer output instruction in Siemens S7-1200 using our simulator. 

Ladder logic code for SQ0

As you guys see the sequencer output instruction is nothing but shifting the height value from right to left bit or right to left or even rotated shift for continuous operation. That drives our thinking to use the shift instructions in Siemens to perform this sequencer output instruction. 

Figure 9 shows the rungs of a ladder PLC simple program that implements the sequencer output process. See guys how lengthy the logic we have to code to do the same function of single instruction QSO in Allen Bradley. Again, that is a drawback or limitation thing but the program is more lengthy and takes more effort and also memory it is consuming that way. Moving to the logic listed in Fig. 9, the first thing is using a rotated shift instruction that shifts through the data block bit by bit and applies to the output QW0. At the same time increment instruction is used to move through the data. Also, one on delay timer is used to do some delay to show up the sequencing process of activating the output sequentially. And the end, a comparison instruction has been used to check if the pointer or the position reached the last output coil to reset to the first position and so on.

Simulating the sequencer output ladder code

Figure 10 shows the simulation of the sequencer output ladder code before activating the processes by using M0.0, it shows the position is at 1 and the output QW0 is all zeros. So let us activate the sequencer output process by set M0.0 to high and see the output.

Figure 11 shows the process after activating the sequencer program and starting to switch on outputs sequentially. The figure shows the process reached the sixth output coil and the position set to 7 to point at the next output. The process continues to tell reach the last one and then the position set the first step.

What’s Next???

I am glad to have you guys following tell that point and I hope you see the importance of the sequencer output technique in reducing the effort of programming and saving memory. Next time will take a tour with the bitwise logic operator and how they are utilized and how they work in the ladder program with given examples and for sure simulation to practice their usage. So let’s meet then and be ready.


Subroutine in Ladder Logic Programming

Hello friends, after completing that basic part of ladder logic programming, let us today go through one topic which is not essential to know to complete a PLC ladder program but it is important t have our code readable program and reusable pieces of code. That could happen by using what so-called a subroutine. So what is a subroutine?

Well, it is a piece of code that includes a few rungs to perform specific tasks. that piece of code can be reused numerous times through the program when we need to call it for performing that task. That subroutine enables us to structure our code like building blocks so that the program will be readable very easy and also reusable later in other projects. The idea of dividing the program into routines to apply the divide and conquer technique is very crucial to ease the coding of your program especially when it comes to the large-scale program which is the common cause in the best industry practice.

Each routine achieves one specific task and they are integrated to accomplish the whole mission. By programming in that way, the code is more readable and reusable meaning that one routine could be called many times instead of keeping repeating the lines of code or ladder rungs everywhere we need it. In this tutorial, we are going to be very familiar with subroutine-based programming including different ways to call a subroutine and the instructions that are used for handling subroutines. And yes we are going to practice examples that program that subroutines in ladder logic so let's get started.

Types of routines 

Routines can be classified into two main types: the calling routines that call subroutines. And routines that are being called. Figure 1 shows the very typical scenario of calling one subroutine. It shows the scenario when the calling routine reaches the calling instruction when the subroutine is being called with the value of the parameter passed by the calling routine and then the subroutine has a return instruction to take the execution to the following instruction of the calling instruction.  So you can imagine the calling instruction is in the calling routine which is mostly the main program and the called subroutine is responsible to return the control or the execution point to the instruction next to the calling instruction.

Passing parameters 

There are two main ways for passing parameters between the calling routine and the subroutine that is being called. The two ways are calling by value and calling by reference. In the calling by value method, the calling routine passes a copy of the original parameters so the called subroutine works on a copy. Therefore, there are no changes could happen to the original parameters in the calling routine. In contrast, in the second method which is called call by reference, the parameters are shareable between the calling routine and the subroutine that is being called. So, changes happened to the parameters but the called subroutine reflected on the original ones. Figure 2 depicts a case scenario of the two types of passing parameters. In that very example, the main routine calls subroutine “Call_Val” to apply an increment of operand “Op1” passing the parameter Op1 using the method “calling by value” while it calls “Call_ref” to do the same process but in that case, it passes parameter Op1 by using the method of “calling by reference”. As you can see, in the case it uses the “calling by value” method, it does not change the value of parameter Op1 because it uses a copy of the parameter and increments that copy not the original parameters. On the other hand, when it has used the other method, “calling by reference”, to increment the same parameter Op1, it does change the original operand because it uses a reference that points to the original parameter address. In another word, calling a subroutine based on a call by value works on a copy of the parameters and does not change the original variables while calling by reference works on the originally passed parameters so it does change the parameters. The advantage of calling by reference is saving the memory as it does not need an instance of the variables or a separate data block. On the other hand, calling by value needs to have a separate data block to include that copy of passing parameters. However, it can be called many times with a separate copy without any conflicts.

Program sequence:

Figure 3 shows the program sequence when using multilevel branching using subroutines. For example, the main module keeps carrying out the network net1, and net2, and it calls the subroutine “Sub x” in-network net3. Then subroutine “sub x ” starts executing from network net1 to net3 at which, it calls subroutine “Sub y”. Then subroutine “Sub y” carries out starting from the network “net 1”, and network “net 2” calls subroutine “Sub a”. You can notice subroutine “Sub a” returns to the calling routine “Sub y” which returns to subroutine to its calling routine “Sub x” which returns to its calling routine which is the main routine. So, you can notice how many levels of subroutine calls? Yes, they are a lot. But, it is recommended to be as much as needed for two reasons. First, many branching causes some headaches in processing in terms of memory and time. Memory is represented in the data block that is needed for each subroutine every single branching or subroutine calling and time is represented in extra instructions of calling and returning and stacking data related to the called routine and the returning addresses.

Subroutine instructions

There are two main instructions used for calling a subroutine and returning to the calling routine. Figure 4 shows the network rung that calls a subroutine called “Auto Mode”. It is going to call the subroutine at any time when the “System OK” memory contact MB180 is true. So I hope you can feel now how your program is more organized and readable when you go through the main program and easily can catch what the program does by reading the names of the called subroutines. Exactly, having a meaningful name for subroutines is very important to give the advantage of the readability of your program. So, my friends please make sure you chose a purposeful name for your subroutines.

Also fig. 5 shows the network rung that represents the returning command to the calling routine. It shows when the alarm is true represented by memory contact MB32 and pump status “Stop Pump” represented by memory bit64, the return command will be enabled for returning the execution point to the network just next to the calling network in the calling program. For example, if the calling network is at rung number 3, then the return command in the subroutine will return the execution at rung number 4 in the calling routine.  By the way, in most cases of ladder logic programming, you might not find a return instruction because it is inherently performed by completing the called subroutine but we just include it here as in fig. 5 to show up how the process of calling a subroutine and returning to the calling routine.

Figure 6 depicts the whole image of the scenario of calling one subroutine. In rung 3, a subroutine called “Auto mode” is being called by having the memory contact MB18 true. Then after that subroutine, “Auto mode” is completed it returns to the calling routine at the next rung to the calling rung which is in this case example rung number 4.  As we stated earlier, you can notice that there are now returning instructions but automatically, the called subroutine returns to the calling routine at the instruction just next to the calling instruction.

Practicing Subroutine in ladder logic

My friends, let us go to our lab as usual and do some hands-on practicing subroutine using the simulator. But before going further in practice subroutine we want to elaborate on the different types of subroutines in Siemens programming. Table 1 compares the subroutine implementation based on function and function block. It shows that subroutine can be implemented by either function FC or function block FB. And both ways can use input, output, and in-out parameters. 

FC does not use static data or data block (DB) while FB uses DB and passing parameters by making an instance of the variables which is calling by value. FC passes the parameter by pointing to the address of the variable which calling by reference technique.   

Table 1: Comparison between FC and FB subroutine in Siemens

Characteristic

FC

FB

Can be used as a subroutine

Yes

Yes

Can use parameters for Inputs, Outputs, and Input

Yes

Yes

Can use temporary variables

Yes

Yes

Can use static variables (with remanence)

No

Yes

Need an auxiliary (instance) DB for each call

No

Yes

Parameters are passed as the address for internal use

Yes

No

Parameters are copied to/from a DB (instance) for internal use

No

Yes

Can call internally a FB or FC

Yes

Yes

Can be called for a FB or FC

Yes

Yes

Can call a FB as multi-instance

No

Yes

Can be called without filling all parameters

No

Yes

Subroutine in ladder logic programming 

Again, there are two ways of calling a subroutine in Siemens ladder programming as shown in fig. 7. We have developed a sample ladder program that calls a simple subroutine to add two input operands. In the first rung, it uses function “FC1” called “fun_A” and in the second rung, it utilizes a function block FB1 called “FB_A” as you can notice function block uses a data block instance for passing a copy of the operands while function FC does not use a data block and uses the address of the input and output parameters.

Figure 8 shows the function block FB_A. the function block has two input operands which are “op1” and ‘’op2” and one output operand which is “sum”.

Because the function block passes parameters as copies or instances, fig. 9 shows the related data block DB1 which contains the parameters of the function block. It is composed of many structures for holding input, output, input, and static data as well.

On the other hand, the function uses the addresses of the parameters. Therefore, it does not need a data block as shown in fig. 10. It uses many structures to pass input, output, input, temporary, and constant data types.

Simulation work

Now let’s check the work with the simulator. Figure 11 shows the results of simulating the calling subroutine based on function FC_A that adds two static operands showing the calling path from the main organization block to the function.

On the other hand, calling a subroutine based on a function block uses an instance of parameters in a data block as shown in fig. 12. This means another instance of data is used every time we called that subroutine while using the function it is referencing the same variables at the same addresses. 

What’s next

Thank you guys for following me up to this very point of our tutorial and being very confident by knowing the subroutine you now are all set to write an organized program and readable and reusable piece of code using ladder logic programming. the next time we are going to talk about one of the most important topics in PLC programming which is master reset control showing what is it about and its importance for PLC and control project and for sure will practice with our simulator lab. So be ready and let’s meet in the next tutorial to enjoy learning and practice the PLC ladder programming series.

Master Reset Control in Ladder Logic Programming

Introduction 

Hello friends, I hope you are doing very well. Today we are going to learn and practice the master control reset (MCR)! So what is that MCR? Well! This is a tool you might use to control a group of devices with one push button for performing fast emergency responses with one click for a group of devices in one zone. In another word, you divide the program into zones and put this zone between a master control to control their operation as one unit by one contact. This technique is useful for applying emergence stops and also protecting some equipment by applying a safety restriction to not operate when that condition is in effect. 

The concept of the master control reset (MCR)

Figure 1 shows the master control relay in a ladder logic showing a couple of rungs between the master control and master control reset to be controlled as one zone by master control. for example, input 1 enables the master control relay M100 which is the only way to relay the hotline of power to rungs 2 and 3 as shown in the figure. When input 1 is on, the master control relay is energized. Therefore, input 2 and input 3 can energize output 1 and output 2 respectively. But, if input 1 is off, the master control relay is off. Therefore, rungs 2 and 3 are disconnected from the power. Therefore, even if input 2 and input 3 are on, outputs 1 and 2 will not energize because of a missing connection to power via master control relay M100. To sum up, there is a zone that contains a couple of rungs, these rungs are not enabled without master control enabled. Also, fig. 1 shows the structure of the master control and master control reset to have one rung to enable the master control relay and one rung at the end to represent the master control reset and declare the end of the zone that is under master control. and the code or rungs that are located between the master control and the master control reset is the zone that we need to control its running based on master control. So, if the master control is not true, the code in that zone between the master control and the master control reset will be bypassed and the next instruction after the master control reset instruction will be executed. 

The concept looks very simple but it is crucial for safety and control techniques. Also, many master control and zones can have in our program as shown in fig. 2. It shows more than one zone and each one is controlled by its master control.

So we want to go further in demonstrating the master control reset by a practical example from real life. Figure 3 shows a practical example of real-life in the industry of which automatic bottle filling process. So what does master control have to do with such a process? Will! That is a good question because it tells me you understand master control reset and are with me on the same page. So, as you can see, there is the start and stop pushbuttons and we need to use master control to control starting and stopping the whole process regardless of the status of individual inputs and sensors. by having such control, we can stop the process in any emergency case or for doing maintenance. The sequence of the process is to start moving the conveyor by hitting the start push button. The conveyor keeps running until the proximity sensor comes ON. At that time, the valve will open for 5 seconds and then the conveyor continue moving again after 5 seconds and continue for 3 bottles repeating the same process. But if there is an emergency happens, there should be a way to stop the process including moving the conveyor, and opening the valve even if all conditions to do are met. Well done! You are correct, master control and master control reset should bracket the process to be enabled and disabled when that emergency comes to happen.

Master control in ladder logic

Master control and master control reset are the same concepts. However, a few differences you can notice in ladder logic from one plc brand to another. For example, Fig. 4 shows the ladder logic code of a master control reset in Mitsubishi PLC. You can notice the same concept has been applied. A zone of a couple of rungs is surrounded by master control and master control reset instructions based on master control relay M100. Input 1, X400 enables the master control relay M100. And rungs 2 and 3 are included in the zone under master control.

On the other hand, master control and master control reset look a little bit different in Allen-Bradley as shown in fig.5. However, you can notice the same concept is applied by having the zone that includes a couple of rungs between the master control relay and master control reset for enabling or disabling that zone based on the logic and situation.

Also, Siemens shows a few differences in ladder logic of master control as shown in Fig.6. however, the same concept is thereby enclosing the code to be controlled in a zone preceded by enabling to master control relay and followed by a master control reset to clear that master control and show the end of the controlled zone.

Practice Ladder logic example

Guys, it is now the time to enter our lab and enjoy practicing the master control and master control reset by using our simulator as usual for validating our understanding of what we have gone through in this tutorial on ladder logic programming. In the example simulated below and shown in fig. 7, we have designed simple master control and master control rest to have master control of running of Q0.0. you can notice that, despite input I0.1 being true, Q0.0 is not energized because master control is not enabled or in off status. So what happens if we enable master control by switching on input I0.0?

Yes, you are correct! The output Q0.0 will now work after enabling the master control by turning input I0.0 on as shown in Fig.8.

What’s next???

Let me thank you guys for following up until this point and because your knowledge of ladder logging is getting increase every single tutorial, I would like to announce that, the next tutorial will be about one of the very advanced levels of ladder logic programming which is for expert and I thought you are now. The sequencer output instruction in ladder logic is our topic for the next tutorial in which we will learn and practice how to, massively output data sequentially to outputs. Please do not worry if that is not clear for now and just be there to go through it step by step and enjoy practicing one topic for an expert ladder programmer.

JMP & LBL Instructions in Ladder Logic Programming

Hi friends, I hope you are very well; today in this tutorial, we will practice conditional jumping for performing some code at the occurrence of some conditions. Like any other programming language, jumping is one of the most common approaches to transfer the execution from its sequential mode to run different processes or instructions marked by a label and bypassing the lines of codes in between the last executed transaction before the jump instruction and the labeled instruction whom the program is going to move to. The good thing about this technique is shortening the scan cycle of the program due to not running the whole program. However, using jumping techniques in coding is very dangerous. It would help if you were careful of missing some open cases before going anywhere in the code. For example, let us say that we started one process. In the middle of the process’s sequence, one jump instruction is used to run some logic under some conditions, so jumping means leaving or skyping for some reason and going running some logic or code. Therefore, we should consider the skipped instructions’ effects and consequences on the whole process and program logic. In addition, because any jump instruction can target any label and many jump instructions can target the same label, you should pay great attention to make sure you name the correct label according to the designed logic to avoid losing the reason you plan your program to do. I know you might take it easy now and can not imagine someone right wrong label by mistake. But, when you will be writing a large-scale project which contains hundreds or even thousands of rungs and has couples of dozens of labels, in that case, you can quickly mistakenly write the wrong label.

JMP and LBL instructions

To perform jumping in the ladder logic program, two instructions are together, as shown in Fig. 1. The first instruction is JMP which tells the PLC you need to jump from where the jump instruction is to where you find the LBL instruction.

Fig. 1: JMP and LBL instructions

Now how to know the label to whom you want the program execution to go or jump? Well, that is an excellent question, and the answer is that you should specify the label name as a parameter of jump instruction and the label instruction. Therefore, you can notice, my friends, if Fig. 2, the JMP and LBL instructions have a question mark denoting you should specify the label name, which is the next station to programme execution.

Fig. 2: Jump and Label instruction showing the label name above of them

Jump instruction ladder logic example

Now friends, let us see how jump and label instructions work together, as shown in fig. Three depicts a straightforward example of ladder logic in which JMP and LBL instruction work together, referring to the same label Q2:0. In this example, if input contact I1/0 is activated, JMP instruction will take the execution where the Q2:0 label “LBL” instruction is. As a result, the rung 001 has been bypassed.

Fig. 3: ladder logic example for jump and label instructions

How about going with a situation where we need to employ JMP and LBL instruction? Yes, in the example shown in fig. 4, You can notice here in this example. There are a couple of motors and for these motors. We need to. Use Siri. The command of the jump. And in combination with the uh label instruction. 2. Let some motors work in one scenario. And in another scenario, we will let some of these motors worworkd the others are not working. So you can imagine if you are working with Couples of pumps and want to run all pumps together in one scenario, so you do not activate the JMP command. But, in another scenario, if you’re running some of these pumps. So what we are going to do here in this example is to activate the JMP command to bypass some of these pumps, which are between the JMP and LBL commands.

Fig. 4: a real example of jump instruction

Figure 5 depicts the execution of the sample program that demonstrates JMP instruction. This case shows what happened in the first scenario example. If we do not activate jump instruction in rung 3, then all motors 1,2,3,4,5,6 and 7 will run based on the status of their firing contacts which are I:0 0 to 7, respectively. And the program execution continues running other motors after the JMP as it is not activated by touch I:0 3.

Fig. 5: test when JMP is not activated

On the other hand, Fig. 6 depicts the second scenario when activating JMP instruction by contact I0:3 at run number 3. By activating the jump command, We can notice only the motor before jump instructions are running based on their command contacts when the pumps in between jumps and label commands are bypassed, and the other engines after label instruction are running. So motors 1,2 and 3 are running, motors 4,5 and 6 have been ignored by activation of jump command instruction, while motor seven is running, which is at label command.

Fig. 6: test when JMP is activated

What’s next,

I want to thank you all for following me in this short and essential tutorial that can help you utilize the jump and label command instructions to control the logic flow of your program based on the situations and the logic design of your program. So now you can perform safety control to avoid running some equipment under some circumstances to protect the operator and equipment. In addition, you can run many logic scenarios in one program based on the status of input and output devices. In the following tutorial, we will take something neer to jump, which is subroutines, showing you how you can make your program modular and divided into subroutines to be organized and readable and easy to maintain, follow, and being modified. So be ready, and let us meet soon to learn and practice the new ladder logic programming tutorial.

Analog Input Scaling in Ladder Logic Programming

Hi friends and hope you are doing very well. Today we would like to take one tutorial which is very essential in the industry which is analog input processing for handling analog measurements of physical signals like temperature, humidity, pressure, distance, flow and level of liquids, etc. Typically, sensors produce two types of analog signals to represent the equivalent measured signal which is current and voltage signals. The currently produced signals would be within the range of 4-20 mAwhile voltage signals are in the range of 0-10 v. because, that output signals represent physical signals, the limits of output signals are 0 to 10 v for voltage based sensors and 4 to 20 mA for current-based sensors, these values should be scaled to represent the equivalent value as regards to physical signals. By completing this tutorial, we will have elaborated on the concept of handling analog signals and how we scale them to represent the physical signals. In addition, we will go through practical examples for handling analog inputs with ladder programming and then we will enjoy practicing these examples on the simulator to validate the written ladder code and show practically the analog processing in PLC.

In computer systems, plc, and microcontrollers all processing work that has been done inside is processed in digital which is 0 and 1 representation. So, one may ask how analog signals which change continuously within a specific range could be processed by computers, PLCs, or microcontrollers? Well, that is an exciting question and its answer will open the door to show the aim of this tutorial. Figure 1 shows an example of analog signals of the voltage that represents the temperature sensor reading.

Fig. 1: Output voltage of temperature sensor

As you see in fig. 1, the output of the temperature sensor would be either a voltage signal or a current signal based on the type of the sensor. And the output of sensors is applied to the analog input module that converts the analog signal to a digital signal to be processed digitally by the PLC.

Scaling analog inputs

The output signals of sensors either voltage or current signal should be scaled to represent the physical signal and at the same time, it has its equivalent digital values. For example, let the temperature that is being measured has a range between 0 to 100 °C, and the sensor that we use produces voltage output from 0 to 10 v. Therefore, each 0.1 v change in voltage is equivalent to a change of 1°C. Also, assuming the maximum digital value that can be received is 4000. So, 0 V is equivalent to 0 as a digital value and 10 voltage is equivalent to 4000 as a digital value. So, it is crucial to scale the output voltage to determine accurately the equivalent change in sensors’ reading in terms of voltage to represent the temperature in digital value and the real range of the physical signal.

Scaling with parameters

Again, we have two sides to be scaled from one to the other. Therefore, the parameters that are needed for achieving such scaling are as follows:

  • The input value: represents the physical signal that comes out from a sensor. And it typically could be from 0-10v for voltage-based sensors, or it could be 4-20mA for current sensors type.
  • The scaled value represents the digital equivalent of that value. And it depends on the word size or the number of bits that we use for representation in digital format. For example, for 16 bits size, the scaled value is up to which is +/- 32768.

So we can imagine together now the scaling function block in ladder logic should be as shown in fig. 2. It shows the input minimum and maximum which could be 0 and 10v or 4 and 20mA. In addition, there are scaled min and max which could be from 0 to 32768 for 16 bits size conversion.

Fig.2: the SCP block in Allen Bradley

Analog input processing in Ladder logic

We are going to show a complete example of analog input processing in Siemens and AB as well to present the merits of analog conversion in both brands. Figure 3 shows a primitive rung of the ladder logic program that processes the analog input reading. The ladder rung uses a scaling with parameters (SCP) block that includes an input minimum of 4mA and an input maximum of 20mA. And it uses a scaled range from 0 to 32767 because it utilizes 16-bits word for representing the digital data.

Fig. 3: Ladder logic rung for analog input processing

Figure 4 shows the run of the simple example which shows up the processing of analog inputs. It shows that, when the input measured value was 12mA, the output was 16884 which is pretty accurate. It is good to mention that, the output is limited to be within the range of the scaled min and max meaning that it should be from 0 to 32767.

Fig. 4: Testing analog processing by the simulator

Let us give another example but in siemens s7-1200 which is represented by Fig. 5. The ladder code is very simple and it consists of two runs as shown in fig. 5; the first one is for validating the reading to be within the range which is from 0 to 27648 and the second rung is the main one which performs the analog processing in two steps. Firstly it normalizes the input based on the aforementioned range and then it scales the output back to represent it in the output signal format. In this example, we measure a battery voltage that is typically located in the range of 0 to 12v. Therefore, the min and max parameters in the scale_X block should be 0 and 12 v respectively.

Fig. 5: Example of processing analog inputs in Siemens S7-1200

Figure 6 demonstrates the test of the analog processing showing the output reported 5.858941 when the reading was 13499 which is high accuracy.

Fig. 6: Simulation of processing analog inputs in Siemens S7-1200

What’s next

I am truly thrilled to have you shared with me such a very important tutorial about analog input processing and scaling with parameters because this is a very common operation you might see in every operation in the industry as there are hundreds of sensors that read analog physical signals and be processed by the controller for deciding the next stage of the operation. Next time we are going to talk about jumping and branching techniques in the ladder logic program. So please be ready to meet very soon and learn together and enjoy practicing plc ladder programming

Comparator Operators in Ladder Logic Programming

Hi friends. Today we are going to go through one of the most commonly used topics in writing ladder logic programming which is using comparator operations. This includes the logical and mathematical comparison between variables to decide where the logic goes.

There are many comparator operations like equal (==), not equal (<>), less than (<), greater than (>), less than or equal (<=), greater than or equal (>=). All these comparator operations might be used in different logic scenarios while writing a ladder logic program. In this tutorial, we are going to go over each operator showing the input operators and output as well. In addition, we will practice some examples with the simulator to familiarize how to use them flexibly while developing ladder logic programs.

Input and output parameters of Comparator operations

Because they are used for comparing two the value of input variables, there will be two operators which are being compared. these input variables could be of any data type i.e. integer, real, boolean, character, string et cetera. And the output will be a boolean data type which denotes true or false, or “0” or “1”. As shown in Fig. 1 a typical comparator operator has two operands and one output which is called the result of logic (RLO).

Fig. 1: Comparator operation bock diagram

Table 1 lists examples for one of the comparisons between two variables of different data types and their output. In table 1, a typical example of “==” comparator operation between two strings considering the case sensitivity. The first column shows the values of the first operand and the second column shows the values of the second operand while the result of a logical operation (RLO) is represented in the third column. Now let us go over the station of each comparator operation for elaborating their operators, result, and give an example with simulation.

Table 1: example of comparator operation’s operands and RLO

Equal operator (==)

The equal operator is used to check if two operands are equal or not. The input operands’ datatype could be any of the possible data types in the language you are using. For example, Table 2 shows the parameters of the Equal operator in siemens S7. As you can notice, the input operand’ datatype can be any of the listed data types in the third column. One thing we need to highlight here is that datatype could be an array or structure of elements of the basic datatypes. For example, it could be an array of integers. In this case, the comparison will be conducted between every single element in the array of both operands. And if any of these elements have been found not match their equivalent in the other operand, the RLO will be false. Furthermore, the comparison not only does it apply to variables but also could be conducted between memory areas as shown in the third column. It can be used to compare input, output, marker, counter, timer memory data.

Table 2: the parameters of the Equal comparator operator

Example of equal comparator operation

Figure 2 depicts an example of an Equal comparator operation. .it compares literal constant with the variable of type integer saved in marker memory MW4 which is the location of a memory word.

Fig. 2: Example of Equal comparator operator

Figure 3 shows the simulation result of an example of an equal comparator operator. In the case of operands are not equal as in the example shown by fig. 3. The first operand is equal to 5 while the second operand is “0”. So the RLO shows false or “0”.

Fig. 3: simulation result of equal comparator operation when its operands are not equal

Figure 4 shows the simulation of an equal comparator when its operands are equal. The RLO is true or “1”. So the output coil is TRUE.

Fig. 4: simulation result of an equal operator when operands are equal

Not equal comparator operator

This operator is used to compare two operands. When they are not equal it produces positive RLO with High logic and when they are equal it returns false or “0”. Figure 5 shows the test result on the simulator of the NOT operator when the operands are equal. It returns RLO with “0” or false.

Fig. 5: simulation result of Not equal operator when operands are equal

On the other hand, fig. 6 shows the result of the simulation of the NOT operator when its operands are not equal. It gave RLO with logic Tru or “1”.

Fig. 6: simulation result of an equal operator when operands are equal

Greater-than “>” comparator operation

The greater than comparator operator is used for checking if operand 1 is greater than operand 2 or not. Figure 7 shows the rung of a ladder logic program that utilizes greater than comparator operation in which two operands of the integer data type are compared using the greater-than operator. The first operand is at memory location MW4 and the second operand is at memory location MW2. And the RLO represents the retuned result of the greater than comparator operation.

Fig. 7” ladder logic rung example of usage of greater-than comparator operation

Figure 8 shows the results of greater than comparator operation when operand one B is not greater than operand A. on the left side of the figure, it shows the case when both operands are equal and the right side shows the case when operand one is less than operand 2. In both cases, greater than operator sees its condition is not fulfilled so it returns false or “0” at the RLO and hence the output is false.

Fig. 8: simulation results of a greater-than operator when operand 1 is not greater than operand 2

Less-than “<” comparator operation

The less than comparator operator is used for checking if operand 1 is less than operand 2. Figure 9 shows a rung of a ladder logic program that utilizes less than comparator operation “A<B” in which two operands of the real data type are compared using the less-than operator. The first operand is at memory location MD8 and the second operand is at memory location MD12. And the RLO represents the retuned result of the greater than comparator operation. Also, it shows that, when oper1 is not less than oper 2, the result of logic output RLO is false and output is not activated.

Fig. 9: ladder logic rung example of usage of less-than comparator operation

Figure 10 shows the results of less than comparator operation when operand “oper1” is less than operand “oper2” the returned RLO is high or “1” and the output is activated. Now, one may question the case if the two operands are equal? Well that is is a good question and the case of equality between the two operands is considered false for both less than and greater than comparator operations.

Fig. 10: simulation results of less than operator when operand “oper1” is less-than operand “oper2”

Please see fig. 11 which shows the less comparator operation returns false when the two operands are equal. The next section will show the case of greater than or equal comparator operators “>=” and the less-than or equal comparator operator “<=”. In those cases, the equality between the two operands is included and the returned RLO is True or “1”.

Fig. 11: the less-than operator returns false when the two operands are equal.

 

Greater-than or equal comparator operator “>=”

Figure 12 shows a run in a ladder logic program that uses greater-than or equal “>=” operator. As you can see, the output shows true when the two operands are equal.

Fig. 12: greater-than or equal “>=” when two operands are equal

Less-than or equal comparator operator “<=”

Figure 12 shows a run in a ladder logic program that uses less than or equal “<=” operator. As you can see, the output shows true when the two operands are equal.

Fig. 12: the less-than or equal “<=” when two operands are equal

In-range comparator operator

Do all comparator operations take two operands? The answer is almost yes. However, there are very few operators that take only one operand. For example, the in-range operator compares the input operand with upper and lower limits to check if it is located within a specific range or not. Figure 13 shows a ladder logic rung uses that comparator operator to check operand oper1 of type real to see if it is in the defined range which is between 0 to 10.0. because the value of the operand oper1 is 11.5 which is out of the defined range. The RLO shows false or zero. Therefore, the output is deactivated.

Fig. 13: in-range comparator operation for the real data type variable

Figure 14 shows the RLO is true when the operand’s value is located between the defined limits of the in-range block. Also, there is out range comparison operator that is the opposite of the in-range operator about the logic.

Fig.14: in-range comparator operation returns true when operand located in the range

 

Larger example

In this section, we want to show you how these comparator operators can be combined to achieve a logical expression in ladder logic programming. let us imagine a scenario that we have a garage with a full capacity of 100 parking spots and we utilize a counter to count up cars get in and count down the cars that get out of the garage. Figure 15 shows a very simple logic of the validation and comparison to decide if the garage has room for a further car or it is full and no further car is allowed at present. Assume that the output of the counter is an integer data type variable that is stored in memory location %MW20. Now you can see the logic is straightforward. In the first step, the counter output is validated to make sure that it plays in a valid range which is from 0 to 100 which denotes empty to full capacity of the garage. It applies the limit function to the counter in case it is out of range to reset it to be within the designed range. Then program checks in the second step the value if it is less than the full capacity of the garage which is designed to be one hundred cars, it states true indicated with green lamp output meaning there is still room for cars to get in the garage; otherwise, it checks if it is greater than or equal of the maximum limit meaning there is no room for further cars to enter, it shows false meaning garage is full at the current time by activating a red lamp.

Fig. 15: garage status ladder logic example

Figure 16 shows the case when the garage still has room for further cars and the green lamp is activated for incoming cars.

Fig. 16: when the garage has room for further cars

Figure 17 shows the case when the garage is full and has no room for further cars. So the red lamp is activated for telling users no further empty spots available at the moment.

Fig. 17: when the garage is full

What’s next

As usual, I want to express my delight in your following up our tutorial, and let me take this chance to announce one of the most important and exciting tutorials which are about processing the analog inputs and how to scale the analog inputs. Did I tell you my friends that will be the next chapter? So be ready for enjoying processing analog inputs with practicing real-life situations in the industry.

Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir