Simulating Advanced Logic Gates using Ladder Logic Programming

Hello friends! I hope you are all very well! I am so happy to meet you today to continue learning and practicing PLC ladder logic programming. In an earlier part, we already have gone through the very basic logic gates of “AND”. “OR”, and “NOT”. Today we are going to resume the simulation of logic gates. We have started and gone through simulating the basic logic gates which are “AND”, “OR”, and “NOT” as they are the most important basic logic gates by which we can form other logic gates. However, because the logic of large-scale projects is getting more and more complicating, a lot of time we have to use the other functions to do tasks faster. For example, we have shown in the logic gates article that, XOR can be used to compare two inputs and check if they are equal or not. On the other hand, someone may say, oh we can do XOR by using the basic three gates of “AND”, “OR”, and “NOT”. That’s correct. So why do we need to go learning other logic gates when we can implement them by using that three basic logic gates. Well! That is a very good question.

Working in ladder logic programs is getting further complicated especially for large-scale projects. So, it is very beneficial to know shortcuts for writing ladder logic simpler, more readable, and easier for others to develop. Using this variety of logic gates, enrich the logic and fluency of writing the logic in different situations. For example, using XOR logic is very common for comparing two inputs to decide if they are equal or different. Therefore, we found that it is crucial to go through all logic gates and do simulations for them all in this tutorial. for coherently we get the knowledge to use them fluently in ladder logic programming to solve different logical problems and scenarios. There are seven basic logic gates which are: “AND”, “OR”, “NOT”, “NAND”, “NOR”, “XOR”, and “XNOR”. We have gone through “AND”, “OR”, and “NOT” logic gates including simulation work. So let us go through the other four gates for learning and practicing all basic logic gates.

The "NAND" Logic Gate

The “NAND” logic gate is the invert of the “AND” gate like you invert the output of an “AND” logic gate as shown in Fig. 12. Table 5 lists all combinations of the inputs and the output of the “NAND” logic gate.

Fig. 12: NAND symbol

Table 5: the truth table of the “NAND” logic gate

Input A Input B Output
0 0 1
0 1 1
1 0 1
1 1 0
 

In addition, the timing diagram of a “NAND” logic gate is shown in fig. 13, it shows the output goes low when both of the inputs A and B are true which is the inverse of the “AND” logic gate. Also, Fig. 14 shows a ladder logic of a “NAND” logic.

Fig. 13: The timing diagram of the “NAND” logic gate

Fig. 14: Ladder logic sample of a “NAND” logic

NAND Logic in PLC simulator

The NAND logic gate can be considered as the invert of the “AND” logic gate. So as listed in table 1, the truth table of AND and NAND logic gates shows how the NAND gate is the reverse of the AND gate. Also, it shows the NAND gate should come out to your mind when you want the output always low except when both inputs A and B are high.

Table 1: the truth table of the AND versus NAND logic gate

The NAND gate can be implemented by connecting the negate of the output in series to the inputs A and B. Another way to implement NAND is by inverting both inputs and connecting them in parallel as shown in fig. 1. Rung 1 and rung 2 respectively.

Fig. 1: The NAND ladder logic in two ways

 

Let’s test our ladder logic in both methods. According to the truth table of NAND gate, we have four test cases. figure 2 shows the first test case when both inputs A and B are false. In this case, the output should be true as shown in fig. 1.

Fig. 2: NAND ladder logic when both inputs are false

figure 3 shows the second test case when inputs A goes true while B is false. In this case, the output should be true as shown in fig. 3.

Fig. 3: NAND ladder logic when input A is false and input B is true

Figure 4 shows the third test case when inputs B goes high while B is low. In this case, the output should be true as shown in fig. 4.

Fig. 4: NAND ladder logic when input A is true and input B is false

Figure 5 shows the last test case when both inputs A and B become high so the output goes false as shown in fig. 5.

Fig. 5: NAND ladder logic when both inputs are true

Thanks to performing the simulation of the NAND gate, we now can conclude that we should be looking for a NAND logic when we want to shut down an actuator i.e. motor whenever two inputs are in a true logical state simultaneously. For a practical example, when we have three pumps and we want to run them in the mode of two of three. There should be only two of the three pumps to run at any time. In that case scenario, the run condition of any motor can be a NAND of the status of the other two motors.

The “NOR” Logic Gate

The “NOR” logic gate receives two inputs and has one output. It is the same as the invert of the “OR” logic gate. Like you follow the output of an “OR” gate by a “NOT” logic gate. Fig. 15 shows the symbol of a “NOR” gate. In addition, the truth table is expressed in table 6. It shows the output becomes false only when one of the input A or input B or both goes high which is the reverse logic of the “OR” logic gate.

Fig. 15: The symbol of “NOR” gate

The “NOR” logic gate can be formed by connecting the “OR” logic gate to the inverter “NOT” logic gate. Or by inverting the inputs by using “NOT” logic gates and connecting them to the “AND” logic gate as shown in Fig. 16.

Fig. 16: structure of “NOR” logic gate

Table 6: the truth table of the “NOR” gate

Input A Input B Output
0 0 1
0 1 0
1 0 0
1 1 0
 

Figure 17 shows an example of a ladder program for implementing the “NOR” logic gate. It shows that the “NOR” gate can be implemented in ladder logic by connecting two contacts of type NC in series.

Fig. 17: A sample ladder for a “NOR” logic gate

On the other hand, the timing diagram of the “NOR” logic gate is depicted in Fig. 18. It shows the output is false as long as either input A or input B or both are true.

Fig. 18: The timing diagram of the “NOR” logic gate

NOR logic gate in PLC Simulator

This logic gate can be considered as the negate of OR as you can notice in the truth table as listed in table 2. You can now feel when we may need to use the NOR logic? Yes! Exactly you want it when you design for output which is all time off except when both inputs are false.

Table 2: the truth table of NOR versus OR

Input A Input B OR NOR
0 0 0 1
0 1 1 0
1 0 1 0
1 1 1 0
 

Figure 6 shows two ways to implement the NOR logic in ladder logic. To make that happen, we connect the invert of the two inputs in series to the output as shown in the top part of fig. 6. The other way is to connect the two inputs in parallel to form OR and then connect to negate the output as shown in the lower part of fig. 6.

Fig. 6: The ladder logic of NOR logic gate

Let’s practice simulation of the NOR gate, in fig. 7, the first test case is when both inputs are false, the output is true as shown in fig. 7.

Fig. 7: The Simulation of NOR ladder when both inputs are false

Figure 8 shows the second case when input A is true and input B is false, the output is false as shown in fig. 8.

Fig. 8: The Simulation of NOR ladder when input A is true and input B is false

Figure 9 shows the third test case when input B is true and input A is false, the output is false as shown in fig. 9.

Fig. 9: the Simulation of NOR ladder when input B is true and input A is false

Figure 10 shows the last test case of NOR ladder logic, it shows the output is false

Fig. 10: The Simulation of NOR ladder when both input A and B are true

One practical example of using the NOR logic is that, imagine friends we drive some machine with two motors. And it is required to have at least one of them or both are running all the time otherwise alarm should be energized. The NOR logic is the best to manage that alarm to get energized if and only if both motors are off.

The Exclusive OR "XOR" Logic Gate

Despite this logic gate having two inputs and one output like the “AND” and “OR” logic gates, this logic gate is a bit more complicated than the previous logic gates. Table 4 lists the truth table including all combinations of the inputs and the output. By noticing the truth table of the XOR logic gate, you can see the output becomes low when the two inputs are equal like both are high or both are low. But the output goes high when there is a difference in the state of the two inputs. Imagine my friend, how much this logic gate is very beneficial for comparing two signals.

Table 4: The truth table of XOR logic gate

Input A Input B Output
0 0 0
0 1 1
1 0 1
1 1 0
 

On the other hand, Figure 10 shows the symbol of the XOR logic gate and its schematic. See how the basic logic gates OR, AND, and NOT can be utilized to build the logic of XOR logic gate.

Fig. 10: The XOR logic gate symbol

Figure 11 shows a sample of a ladder logic program that implements XOR logic. In addition, it shows the timing diagram of the inputs and output, it shows the output goes high when there is a difference between the two inputs and becomes low when they are equal i.e. both are low or both are high.

Fig 11: Sample of the ladder logic for XOR logic and the timing diagram

XOR Logic Gate in PLC Simulator

The XOR is used to compare two signals if they are equal or different. Table 3 lists the truth table of the XOR. It shows that the output comes to true when inputs are different and becomes false when they are equal.

Table 3: The truth table of XOR logic

Figure 11 shows the construction of XOR ladder logic. It shows that it is composed of, two parallel branches and each branch is forming AND logic of the two inputs in the opposite logical state.

Fig. 11: The ladder logic of XOR

Figure 12, shows the simulation results of XOR when input A and B are false, the output is false.

Fig 12: Simulation of XOR ladder when both inputs are false

Figure 13, shows the simulation results of XOR when input A is true and input B is false, the output is true. Fig 13: The Simulation of XOR ladder when input A is false and input B is high

Figure 14, shows the simulation results of XOR when input B is true and input A is false, the output is true.

Fig 14: The Simulation of the XOR ladder when input B is true and input A is false.

Figure 15, shows the simulation results of XOR when input A is true and input B is true, the output is false.

Fig 15: The Simulation of XOR ladder when both inputs are true

In a conclusion, the XOR logic in simulation shows that the output is low whenever both inputs are equal and goes high when the inputs are different in the logical state. A very good practical example for utilizing the XOR logic is that imagine friends we have a motor that is energized by two different destinations, and it should be requested by only one at a time. So we can get the run signal from the XOR of the two input switches. So, the only case to run the motor is by requesting from one source.

The “XNOR” Logic Gate

This logic gate is the invert of the XOR gate. So it is equivalent to applying an inverter to the “XOR” logic gate. Table 7 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.

Table 7: the truth table of the “XNOR” logic gate

Input A Input B Output
0 0 1
0 1 0
1 0 0
1 1 1

Fig. 20 shows the symbol of the “XNOR” logic gate, it shows clearly how it is the invert of the XOR logic gate. This logic gate is very useful to validate if two signals are equal or not.

Fig. 20: The symbol of the “XNOR” logic gate

On the other hand, Fig. 21 shows a sample ladder logic of an “XNOR” logic gate implementation. It shows that there are only two ways to have the output in the TRUE state which are by setting both inputs TRUE or set both FALSE.

Fig. 21: A sample ladder logic for “XNOR” logic

Fig. 22 depicts the timing diagram of the inputs and output of the “XNOR” logic gate and clearly shows the output goes high when both inputs have the same state.

Fig. 22: the timing diagram of the “XNOR” logic gate

XNOR Logic in PLC Simulator

The XNOR is the invert of the XOR and it is used to compare two input signals. Table 4 lists the cases of the truth table of XNOR logic. It shows the output goes high when both inputs are equal i.e. both are high or both of them are low.

Table 4: the truth table of XNOR logic

Figure 16 shows the construction of XNOR ladder logic. It shows that it is composed of, two parallel branches and each branch is forming AND logic of the two inputs in the same logical state.

Fig 16: XNOR ladder logic

Figure 17, shows the simulation results of XNOR when input A and B are equal i.e. both are false, the output is high.

Fig. 17: The Simulation of XNOR ladder when both inputs are false

Figure 18, shows the simulation results of XNOR when input A is true and input B is false i.e. they are different. So the output goes false.

Fig. 18: The Simulation of XNOR ladder when input A is false and input B is high

Figure 19, shows the simulation results of XNOR when input A is false and input B is true i.e. they are different. So the output goes false.

Fig. 19: The Simulation of the XNOR ladder when input B is true and input A is false.

In the last case when both inputs A and B are high as shown in Fig. 20, the output becomes true.

Fig. 20: The Simulation of XNOR ladder when both inputs are true

We can conclude that the XNOR is marked by the true status of its output whenever both inputs are equal and vice versa. One of the most common scenarios for the best practice of XNOR is the protection of the operator's hands in the cutting machine. In that machine, the command for running the knife driving motor is an XNOR of two switches on the left and right hand of the operator. In that way, it is guaranteed that to run the motor, the operator should use both hands at the same time.

What’s next

I am very pleased to see you up to this point of our tutorial, Now you are familiar with all logic gates and practiced their logic on the simulator. In addition, you can feel the importance of mastering all these logic gates to ease your programming and enrich your programming skills. In the next tutorial, we are going to go deeply through the edge signal including rising and falling edge. We are going to introduce the benefits of these edge signals and how they can be utilized in ladder logic programming to solve a lot of problems. So be ready for more learning and practice with simulation in ladder logic series.

Designing Logic Gates in PLC Simulator

Hello friends, I hope you all are doing great. In today's tutorial, we are going to design logic gates in PLC Simulator. It's our 4th tutorial in Ladder Logic Programming Series. We come today to elaborate the logic gates with comprehensive details for their importance in PLC programming. you can consider logic gates as the building blocks of ladder logic programming. Like every time we start with telling what you guys are going to have after completing this session? For those who like to buy their time and calculate for feasibility, I’d like to say by completing this article, you are going to know everything about what types of logic gates, how they are designed and how they work, how you can translate the logic in your head into the logic gate and some about logic calculation which is so-called logic algebra, and for sure the connection with examples between logic gates and the Ladder logic programming. In our previous tutorial, we have Created First Project using Ladder Logic, where we designed a simple logic by using contact and coil. Today, we are going to extend that code and will design different logic gates in ladder logic.

We are discussing these logic gates because they are the main building block of complicated logic. Normally, complex logic is designed using multiple logic gates. So, today, we will simulate the basic logic gates i.e. AND, OR, and NOT, while in the next lecture, we will simulate NAND, NOR, XOR and XNOR in PLC Simulator. So, let's get started:

Logic gates

In very simple language, it is a Boolean decision that has one of only two values either “TRUE” or “FALSE”, not both. For instance, the decision to run or shut down a motor, open or close a valve etc. Well! For deciding such Boolean nature thing, there are two things, inputs and logic to apply on those inputs. On the other way, logic gates apply some sort of logic to the inputs to determine the state of the output.

Truth table

It’s a table that lists all possible combinations of the inputs and the state of the output for each record. For example, a gate with two inputs has four possible combinations of the inputs and four states of the output. inputs.

Basics of logic gate

There are seven basic logic gates. Some of them have only one input while others have two inputs. There are seven basic logic gates which are “AND”, “OR”, “NOT”, “NOR”, “XOR”, “XNOR”, and “NAND”. So let us enjoy a short journey with them having a fast stop at each one’s station. Our trip will include, how they work, design, timing diagram, and connection with ladder logic programming.

Simulating ANR, OR, and NOT logic

  • The AND, OR, and NOT logic are considered the basic building block logic for designing the complicated logic to decide the output status.
  • By using two switches A and B and one output representing lamp or MOTOR, we can design and program these logics and simulate them on the PLCSIM simulator.
  • Table 1 lists the truth table of the three logic AND, OR, and NOT.

Table 1: Truth table of the AND, OR, NOT logic

Switch A Switch B Motor
AND LOGIC
0 0 0
1 0 0
0 1 0
1 1 1
OR LOGIC
0 0 0
0 1 1
1 0 1
1 1 1
NOT LOGIC
Switch Output
0 1
1 0

The “AND” Logic Gate

The “AND” logic gate has two inputs and one output. Like its name, the only condition for having the output become true, is by having both inputs, input A and input B are true. Table 1 lists the truth table of the “AND” gate and Fig. 1 images the symbol of the “AND” gate. In addition, Fig. 2 shows a sample of ladder logic rung that uses “AND” gate logic. It decides the status of the motor based on two switches. The two switches must be in true status for running the motor. ‘to sum up, the logic of the “AND” gate, is that, the output comes to true when and only when both inputs A and B are true.

Table 1: the truth table of “AND” logic gate

Input A Input B Output
False False False
True False False
False True False
True True True

Fig. 1: symbol of “AND” logic gate [1]

In the ladder logic rung shown in Fig. 2, there are two contacts I1 and I2, they are of normally open (NO) type, these two contacts are connected in series, so the only way to set the output to true is that both contacts I1 and I2 must set to true. For full imagination, please notice the timing diagram of the inputs and output signals shown in Fig. 3. It shows the output is only high when both inputs are high.

Fig. 2: sample ladder logic rung for “AND” logic [2]

Fig. 3: The timing diagram of the “AND” logic gate

AND logic in PLC simulator

  • Let us once more enjoy learning further by validating and practicing on the simulator, here you can see in figure 19, on the right the AND logic has been programmed by connecting two switches A and B in series.
  • The motor status is the result of the AND logic between the two switches.
  • On the left, you can see the results of the simulation by setting the status of switches to simulate all truth table conditions and see the motor status changed accordingly.
  • In addition, you can see the truth table of the AND logic on the most right of the figure. So you can review and validate what is going on in the simulator.

Figure 19: Simulating AND logic

The “OR” Logic Gate

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.

Fig. 4: The symbol of “OR” logic gate [1]

Table 2 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.

Table 2: The truth table of the “OR” gate

Input A Input B Output
False False False
True False True
False True True
True True True
 

Figure 5 shows an example of a ladder logic rung that implements the “OR” logic. We can implement this by connecting two inputs I1 and I2 in parallel branches and to the output. like this way of connection, the output can be set to true by simply setting I1 or I2 or both true. Once more, let us see the timing diagram in fig. 6, it is clearly shown that the output goes high as long as either one or both of the inputs are true.

Fig. 5: sample ladder logic rung for “OR” logic [2]

Fig. 6: the timing diagram of the “OR” logic gate

OR logic in PLC Simulator

  • You can see in figure 20, on the right the OR logic has been established and programmed by connecting two switches A and B in parallel.
  • The motor status is the result of the OR logic between the two switches.
  • On the left, you can see the results of the simulation by setting the status of switches to simulate all truth table conditions of the OR logic and see the motor status charged accordingly.
  • In addition, you can see the truth table on the most right of the figure. So you can review and validate what is going on in the simulator.

Figure 20: Simulating OR logic

The “NOT” logic gate

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

Fig. 7: The symbol of the “NOT” logic gate [1]

Table 3 lists the truth table rows of all possible combination of input and output.

Table 3: the truth table of the “NOT” logic gate

Input Output
True False
False True
 

Figure 8 depicts a very simple example of a ladder logic rung that shows the output Q1 is the reverse logic of the input I1. In addition, Fig. 9 shows the timing diagram of input and output of the “NOT” logic gate. It shows clearly that, the output is the reverse of the input.

Fig. 8: Sample of the ladder logic rung representing “NOT” logic [2]

Fig. 9: The timing diagram of the NOT logic gate

Before going further with the logic gates, I want to let you know the good news that, you can implement any logic by using the aforementioned three logic gates “AND”, “OR”, and “NOT”. However, for simplification, the other logic gates are designed based on using these three logic gates in different topologies to perform a specific logic functions.

Not logic in PLC Simulator

  • Also, the NOT logic is one of the primary logic functions, you can see in figure 21, on the right the NOT logic has been designed and programmed by connecting switches A in negative logic in series with the motor.
  • The motor status is the result of the NOT logic of switch A. On the left, you can see the results of the simulation by setting the status of the switch to simulate the two-state of the NOT logic truth table and see the motor status charged accordingly.
  • In addition, you can see the truth table on the most right of the figure. So you can review and validate what is going on in the simulator.

Figure 21: simulating Not logic

Now! I appreciate your follow-up to our PLC tutorial. I am very happy to feel that, by moving further in our plc tutorial our experience is getting increasing bit by bit. However, some questions may come to our mind like does the operator needs to keep pressing input like the push button to keep the motor running? What happens if he released it, does the motor stop? Well! By asking such questions, I can affirm you start your way to master PLC programming and its logic. And let me say the answer to your questions is yes the operator needs to keep pressing the input push-button until the motor has done its task. But that is not the best practice in the real life. There are other techniques to keep the motor running by one touch of the push button, thanks to latching, setting, and resetting techniques as we will show you in the next sections.

Latching output

  • Figure 22 depicts the latching technique that we simply use to keep the motor running by pressing the input push button and having it keep running even after releasing the button.
  • As you can see, I have used the Output as a Virtual Input and placed it in parallel with actual input.
Figure 22: Latching output
  • Table 2 lists the First three scan cycles to show the sequence of operations and how the latching process works when someone will press the Input.
  • In the first scan cycle, when the input gets HIGH, the plc will scan the input "Run (I0.0)" and will find it pressed/ON and thus will make the output "Motor (Q0.0)" HIGH.
  • In the second scan cycle, the input "Run (I0.0)" turned off after being released, but the motor contact is still ON from the previous scan cycle.
  • So, the compiler won't change the status of the OUTPUT and we can say it's latching the output.

Table 2: The first three scan cycles of latching operation

Scan cycle Run (I0.0) Motor status (Q0.0) Motor coil (Q0.0)
1 1 0 1
2 0 1 1
3 0 1 1
  • Now let’s add a way to terminate the latching and stop the motor as per request.
  • Well! Simply figure 23 shows a stop button is added for terminating the latching condition.
  • So in table 2, the RLO for letting the motor running will be unfulfilled by hitting the stop push button in the third scan cycle.
Figure 23: latching with stop button

Simulation of the latching in ladder logic

We may be sure of the logic we wrote for coding the ladder logic of the latching technique. However, at this point how about going to the simulation lab to work out our latch ladder logic program to enjoy validating our ladder code by putting it in the simulator and see how far it match what it is designed for.

Latching Ladder code simulation

  • Now let’s try our latching ladder program in the PLCSIM simulator, by entering our ladder logic and starting the simulator.
  • Figure 24 shows the first four scan cycles. Notice on the left we can set the inputs on and off and see the effects on the right part.
  • In the first scan, every single input and output is at its initial state, so the output is not energized.
  • In the next scan cycle, you can notice we switch on input at I0.0 which is the start push button.
  • Therefore, the motor has been started and running. In the third scan cycle, the start button is switched back off.
  • However, the motor still runs thanks to the latching technique. WOW, we can see our logic is working as we designed for.
  • In the last scan cycle, we tried to test stop latching by hitting the stop pushbutton and indeed it stopped latching and the motor stop running.

Figure 24: simulation result of the first ladder program

We will concentrate on moving forward with ladder coding which is our target. However, we just tried to show you at any time you can validate your ladder at any point to enjoy and confirm you are on the right track as long as you are working on your project.

Latching using set and reset

Let’s use another approach for latching which is based on using set and reset coil. Figure 25 shows the set and reset methods.

  • By hitting set_valve at address I0.2, the valve at Q0.0 will be set ON until a reset command is present by hitting the reset_valve pushbutton at I0.3.
  • It is very easy but you need to take extra care while using set and reset as the last set/reset command will overwrite the previous commands.
  • But wait, what’s if an operator keeps pressing the rest or set button for a long time or if the pushbuttons are the stuck and same thing for the stop button.

Well! The rational expectation is that the motor won’t be able to start. However, the good thing is there is a magic solution to differentiate between the situation of this is a normal stop request by the operator or the button is hold pressed unintentionally or due to an issue with the switches. The one-shot technique can magically recognize the event of pressing or releasing the pushbuttons. Therefore, when it is held for a long time or forever that is only one button press event and for triggering it needs to release and pressed once again. That’s amazing but how does it work? Well! Let’s go demonstrate the concept of how it works, implementation using ladder logic, and give an example to understand it consistently and enjoy the magic of one-shot action.

Figure 25: set and reset for easy latching output

The signal edges

Two edges happened when a pushbutton pressed and released which are falling edge and rising edge as shown in figure 26. It depicts the rising edge when the button is pressed and the falling edge when it has been released. Now, let's move to ladder logic, there are two equivalent rising and falling edge contacts that can be used to tell the PLC this is a one-shot signal. Figure 27 shows how the use of the rising edge of the reset pushbutton |P| at address I0.3. it shows that despite the reset being pressed, its effect in the moment of pressing and then it needs to be released and pressed again to reset the valve at Q0.1. in the next section, let’s get to business and work out one practical example which represents a real problem in the industry just to harvest the fruit of what we have learned so far.

Figure 26: The rising and falling edge [2]

Figure 27: The effects of one-shot technique in ladder logic

So, that was all for today. I hope you have enjoyed today's lecture. In the next tutorial, we will simulate Advance Logic Gates using Ladder Logic Programming. We will design NAND, NOR, XOR and XNOR gates in the next lecture. Thanks for reading.

Creating the First Ladder Logic Program in PLC Simulator

Hello friends, I hope you all are doing great. In today's tutorial, I am going to create the first Ladder Logic Program in PLC Simulator. It's 3rd tutorial in our Ladder Logic Programming Series. In our previous tutorial, we have installed PLC Simulator and now we can say our lab is ready to learn and practice. So let us get to work and get familiar with the ladder logic components.

After this article, you will have a complete understanding of PLC contact and coil including their types and possible causes. Because they are the building block of any rung of a ladder logic program. So let us start with ladder logic rung components.

Ladder Logic Contact/Input

  • In ladder logic programming, a contact represents the input of the system and it could be a button press by the operator or a signal from the sensor.
  • Examples of contacts are toggle switches, pushbuttons, limit switches, sensors like level, pressure, proximity switches et cetera.
  • There are two types of contacts normally used, which are:
    1. Normally Open Contact.
    2. Normally Closed Contact.

1. Normally Open Contact

  • A normally open contact is Open/LOW by default and it gets Closed/HIGH by pressing or getting signal from any external source i.e. sensors.
  • As shown in the first row of figure 1, the contact is open or disconnected by default and then the operator turns it to closed or connected status, shown in the second row.

Figure 1: Normally Open (NO) contact [1]

  • Let's understand it with its equivalent electrical circuit, imagine you wire a switch in series to a lamp as in figure 2.
  • After you complete wiring and connect L1 to the hotline and L2 to the neutral.
  • See that at the start the lamp is off until you come and press the pushbutton then it is turned on.
  • So, here the switch is acting as a normally open switch.

Figure 2: Normally open contact or switch in a circuit [2]

 

2. Normally Closed Contact

  • A normally closed contact is at HIGH/Closed state by default and gets Low/Open if pressed by the operator.
  • Figure 3 shows the symbol of normally close contact.
  • So it flows current at the very beginning and disconnects the current flow by being pressed by the operator to become like an open circuit or contact.

Figure 3: Normally Closed (NC) contact

  • For elaborating the behavior, let us wire a circuit that is depicted in figure 4.
  • The contact is connected in series with a lamp to convey the current and let it turn on.
  • So initially, the lamp started in ON status when the contact is not activated by the user.
  • And, when the operator activates the contact it turns off.
  • So, the switch is acting as a normally closed switch.

Figure 4: Normally close contact or switch in a circuit [2]

 

Ladder Logic Coil/Output

  • The coil in ladder logic represents the actuator or the equipment we aim to turn on or off.
  • A good example of a coil is a lamp and motor.
  • Typically it is located at the most right side of the ladder logic rung.
  • Same as contact has two types based on the initial state and the next state after user activation, also the coil comes in two forms which are:
    1. Normally Active Coil
    2. Normally Inactive Coil as shown in figure 5.
  • An inactive coil is normally not energized until it gets connected by connecting the left side to the hot wire thanks to a contact.
  • In contrast, active or negated coil type comes initially On status or energized and turned off when the left side is connected to the hot wire.

Figure 6: active and inactive coil

 

Create First Ladder Logic program

To our fortune we no longer need wires and devices to practice what we have been learning together, thanks to the simulator, which we have installed in the previous lecture. Let's create a new project on TIA portal software and test it with the PLCSIM simulator.

Creating a new project on TIA Portal

As this is the first time to use our software to write and simulate a ladder logic code, let us go step by step creating our very first project on the TIA portal software.

  • You now get in the Lab by opening the TIA portal and hitting create a new project as shown in Figure 7.
  • On the right, you just need to name your project like “new project” and you may leave the default location of projects or alter the data to the project file location as you prefer.

Figure 7: Creating a new project on TIA portal software

  • You will have to select a PLC controller whom we are going to use. So you simply select one PLC controller as shown in figure 8 and click okay.

Figure 8: adding PLC controller

  • The wizard now goes on asking you to add a program block.
  • You can see in Figure 9, the default program block is the Main block which has the main program and other blocks are additional blocks.
  • So for now let us go with the essential requirements for our program which is the main block and you just double click on the Main block to go to the next step.

Figure 9: adding program block

I just want to say well done! And congratulate you that you are now all set to start writing your first ladder logic rung as shown in Figure 10. It shows on the left the project components including hardware i.e. devices and controllers, networking devices, configurations, program blocks etc. The most important thing you need to know for now is the program blocks which contain the only main block and other blocks as the project needs. Now! please stare your eye toward the right to see the icon bar that contains every ladder symbol. You can see the contact of normally open and normally closed. Furthermore, you should see the coil and more which we are going to go into detail later in our upcoming articles of PLC tutorial.

Figure 10: starting writing ladder code

Writing First program on the TIA Portal

  • WOW! You are a superb learner as I can see you can follow figure 11 and by dragging a contact and dropping it on the blue line, you added a start button of normally open (NO) contact type.
  • For identifying contacts and coils, the compiler assigns a unique name & address to each component and can recognize it anywhere in the program.
  • Therefore, you just set the address and name for every component you add to your rung.
  • The address of components has a specific format that is very logical and easy to understand.
  • For example, the contact address “I0.0”, the first character is “I” which denotes input and it is followed by the number of the input module in the rack that holds all inputs and outputs modules.
  • Then a number of the input channel as each input module has many channels.
  • For instance, an eight channels input module can have numbers from 0 to 7 while 16 channels input module can have numbers from 0 to 15.
  • A period is used to separate between the number of input modules and the channel number.
  • So by set address I0.0, this refers to the very first channel in the first input module in a PLC rack.
  • In addition, a name is used as a tag to easily identify the input i.e. “start” to refer to a start switch.
  • Similarly, you add a stop button of the type normally closed (NC) with address I0.1 which means the second input channel in the first input module.
  • Furthermore, you double-clicked the coil for the motor and set address Q0.0 which means the first output channel in the first module.
  • I know you wonder what is “Q”? Yes! “Q for denoting output like “I” is denoting an input. Well done!. Now let us enjoy simulating the very first code you just have done yourself.

Figure 11: writing the first ladder logic program

 

Compiling Ladder Logic Program

  • Like any programming language, the first thing to do after writing a program is to compile, to make sure it is free of error and ready to be downloaded into the PLC controller to run.
  • Figure 12 shows the very simple steps to compile your program by clicking the compile icon in the toolbar which is highlighted in yellow.
  • And you can notice in the lowest window below the results of compilation in blue showing that the code is free of error and warnings.

Figure 12: compiling ladder logic program

  • To let you imagine how the compiler can help you to find the error location and type, we have done one mistake in the code and compiled as shown in figure 13.
  • You can notice that compiler is telling the rung that has the issue which is network 1.
  • In addition, the message clarifies the error by telling, you missed the required data for operand which is the address of input is missing.

Figure 13: Example of an error in compilation

Simulating First ladder logic program

  • After compiling our program successfully, now the next step is to download it to the PLC controller.
  • Yes for sure our simulator will act as the plc controller.
  • So, by clicking the simulator button on the toolbar, the simulator window comes out and also another window to download the program to the controller as shown in figure 14.

Figure 14: calling simulator and downloading program

  • You simply hit the “start search” button to search for the connected PLC controller.
  • In our case, the simulator will appear in the search results.
  • So, you just select it and click load to proceed with the wizard of downloading your program as shown in figure 15.

Figure 15: the wizard of downloading the ladder program to plc controller

  • By reaching the last screen and clicking finished you have downloaded your first program to the simulator.
  • Well done! And let's move forward with simulating our first program to validate our code and enjoy tracing the logic behavior same as a real-time plc controller.

But wait! Will you continue pressing the push button for our motor to keep running? For sure No, there should be a way to let it keep running by just hitting the button thanks to the latching technique.

Simulating our first PLC Program

  • After downloading the program and pressing the run button on the very small window of the PLCSIM simulator, we can notice the run/stop indicator turned on in green showing the running status of the PLC as shown in figure 16.
  • Now, click on the monitor icon on the toolbar highlighted in yellow on the most right of figure 16, you can notice the rung shows every status of each contact and coil in our program.
  • I am very happy to reach this point at which you can see the normally closed contact is showing a green connection as we described above and the normally open contact showing disconnect status and can not wait until the operator press it down to connect and energize the output.
  • But how do we press the buttons or switches when we are simulating? There is no physical switches or button to press!!! No friends that are not the case. Let us see how that can happen thanks to the great simulator that we have between our hands.

Figure 16: Simulating the first PLC code

Simulating the operator behavior

  • This section is more than exciting, it shows you how the simulator not only does imitate the PLC controller but also it has the facility to imitate devices, switches, push buttons besides showing outputs’ status and values.
  • In addition, we will go further in plc programming to show the series and parallel connections of contacts in branches and utilize simple logic AND, OR, NOT to form simple and complicated logics.
  • The first way to set inputs on and off is by right-clicking on any contact and modifying the status to 0 or 1 as shown in figure 17.

Figure 17: forcing the inputs on and off

  • The other way is to go to the expert mode of the full functional simulator, by hitting the which icon on the very small simulator window.
  • A full version of the simulator control window will open up, where you can add inputs and outputs on the right as you can see in figure 18(left side).
  • You can notice the inputs have an option in form of a check button to set it on or off.
  • As a result, the contact will be turned into the selected status and the program perform according to the new status and the designed logic of your program as shown in figure 18 on the right side.
  • It shows the output coil is turned to true status and highlighted in green.
  • At this point, I would like to thank you my friends to follow up on our PLC tutorial series and let us move forward to learn further and do more practice with our simulating lab.

Figure 18: operating using simulator full control window

 

What’s next

Now, how do you see your progress so far? I can see you have just completed the most basics of ladder logic programming. You are now very familiar with the ladder basic components, using the editor to write a ladder logic program, simulate your work for verifying your logic correctness. So you are doing progressively and that’s great to hear that. However, we still have a lot to learn to master ladder logic programming. For example, using blocks like timers, counters, mathematical blocks, data comparison etc. So we hope you have enjoyed what we have reached so far in our PLC tutorial and please get yourself ready for the next part. In the next part, you will learn about types of Timers and how you set their configuration and how you utilize them to achieve the timing-based tasks accurately.

Installing PLC Simulator for Ladder Logic Programming

Hello friends, I hope you are doing very well! In today's tutorial, we will set up a simulation environment for Ladder Logic Programming. It's our second tutorial in Ladder Logic Programming Series. In our previous tutorial, we have seen a detailed Introduction to Ladder Logic Programming and we have seen that this programming language is used for PLC controllers.

As PLC is an Industrial Controller, it comes with built-in relays/transistors(with protection circuitry) and thus is quite expensive as compared to microcontrollers/microprocessors i.e. Arduino, Raspberry Pi etc. Moreover, if you are working on a real PLC, you need to do some wiring in order to operate it. So, in order to avoid these PLC issues at the beginning, instead of buying a PLC one should work on a PLC Simulator. Using PLC Simulator, we can program our PLC controller and imitate its real behavior without having the hardware, saving both time and money as now we don't need to buy a new PLC and can start right away.

To sum up, by completing this article you will have a complete lab that includes the software you are going to use, the simulator that plays as the hardware, and certainly, you will be familiar with installing a PLC programming environment by which you can program, configure, moving the program to the PLC hardware, retrieving the program from the PLC to the software environment, and testing your program on the simulator. In addition, we are going to test our environment setup with a very basic program and take the chance to show you how to program, configure, upload, and test your program on the simulator.

Setup PLC Simulator

As I mentioned in the last tutorial, we are going to work on Siemens PLC throughout this tutorial, as it's one of the most common PLC controllers. So, we are going to install PLC Environment designed by Siemens and is called Total integrated automation (TIA). Along with this software, we will also need to install a PLC Simulator called S7 PLCSIM, again designed by Siemens. At the time of this writing, their most stable versions are 15.1, so download these two applications from below links:

Installing TIA Software

After downloading the TIA and the simulator, we extract the package by double click on the file we downloaded, and then it will be self-extracted and initiate the setup wizard as shown in the below figure. The image shows many steps. Moving our eyes from left to right, on the first part, the downloaded package has been extracted. In the next part of the picture in the middle, the setup wizard gets started by general settings in which you can set the preferred language and select the preferred installation location. The third part shows the setup goes on progressively and takes you to the end of the installation of the software IDE. Congratulation! You know have the programming software IDE installed on your computer and the good news is, all packages of Siemens go with the same scenario, you download the software package files. Click them to be extracted. And then, the installation wizard is launched by the end of file extraction which is a very systematic and easy way.

Figure 2: TIA portal version 15.1 setup wizard

 

Installing the PLC simulator

Well done so far! After having the programming software IDE completed, the next step is to install the simulator package which is PLCSIM version 15.1. Download PLCSim from the above link and then double-click the downloaded file of the simulator package as shown in Figure 3 to start extracting the packed file. You will be asked for the language and the location you prefer to have the installation folder. So you can leave it as the default or go with your preferences.

Figure 3: PLC simulator PLCSIM version 15.1 package extraction

After file extraction has been completed, the setup wizard will start automatically as shown in Figure 4 with the general setting screen by which you can set the preferred language and the location to install the simulator software. So you can use the default setting or update with your preferred choices.

Figure 4: the simulator setup general settings

Figure 5 shows the simulator setup configurations screen which helps you to configure and customize your installation. In this configuration screen, you can go with the typical options of installation in which all software components are selected to be installed or you can customize your installation to select or deselect components of the package. And by hitting the next button of this window, the installation will go on as shown in figure 6 until the end of the installation. During the installation progress, Siemens show off the features you may find in the software and the facilities you will enjoy by using this software. At the end of the installation, the wizard will request you to restart your computer now or later for completing the setup wizard by saving settings and registry values related to the installed software as shown in figure 7. That’s great! As for now, you have everything is ready and you are all set to get started and enjoy practice and learning the ladder logic programming and simulating your work.

Figure 5: the simulator setup configurations

Figure 6: the simulator installation screen

Figure 7: Simulator setup completion screen

Checking the setup environment

Before going any further let us check the successfulness of the installation process of the software and PLC simulator. Simply go to start and open TIA portal 15 and S7-PLCSIM you will see the software opening with no problem as shown in Figures 8 and 9. In figure 8, you can see options to create a new project or open an existing project. Also, there is an option to migrate projects from one version to another version by upgrading or downgrading the version of the projects. In addition, you can enjoy the welcome tour to know about the software programming tools and be familiar with its components. In addition, there is an option to check the installed software to validate the packages you select to include within your installation. for any further information you can click help to search and inquiry about any doubts.

Figure 8: Opening the TIA 15 software for testing installation successfulness

Moving to the S7 PLCSIM simulator software, as you can see in Figure 9, it is a very smart and simple interface. It shows a power button by clicking it you can shutdown the PLC controller or turn it on. Also, all indicators like the real PLC controller are included. You can see the RUN/STOP indicator. In addition, the ERROR indicator blinks red for any faults with the CPU of the PLC. In addition, you can see the run and stop buttons to start and stop the controller. Also, the MRES button to reset the PLC to the default values at any time. In addition, there is a detailed interface of the S7 PLCSIM simulator as shown in Figure 10. You can launch the detailed or maximized interface of the simulator by hitting the top-right icon on the shortcut window version of the simulator.

Figure 9: Opening the PLC simulator PLCSIM 15 for testing installation successfulness

The shortcut or the small version of the simulator shows the basic functions of the simulator like starting and stopping the controller or resetting the PLC and showing the status of the controller i.e. Run, stop, in fault status. But, the maximized or the detailed window simulator interface shows more options and facilities of the simulator. For example, you can create a simulation project to link it with a PLC project. Showing status of all input and output channels on the Input and output modules. In addition, it enables you to set and reset any of the inputs as we will elaborate in detail later in the next articles.

Figure 10: Opening the PLC simulator PLCSIM 15 in detail mode

Validating the PLC Simulator

After completing the installation successfully of the programming tools software TIA version 15.1 and the simulator PLCSIM version 15.1, it should be validated to make sure all components are installed and working properly. Let us validate by going through the functions and wizard. You now get in the Lab by opening the TIA portal and hitting create a new project as shown in Figure 11. On the right, you just need to name your project like for example let it be “first_ladder_prog” and you may leave the default location of projects or alter the data to the project file location as you prefer.

Figure 11: Creating a new project on the TIA portal

By hitting create, the creating project wizard comes out as shown in figure 12. As you can see you have many options to do on this screen like configuring the hardware, designing visualizations by designing and programming a human-machine interface (HMI) screen, motion control, or writing a ladder logic program. As for now do not worry about all these options as they are all not our scope in this series except those are relating to ladder logic programming like writing program option and configuring PLC device and network which we will come to them later in the next articles. For testing the installed software, you can simply select write a plc program for now.

Figure 12: creating project wizard

By choosing to write a PLC program, the wizard takes us to add the controller on which we are going to run the designed program as shown in Figure 13. If you are not familiar with the type of PLC controller models and hardware for now. That is not an issue because we are here to learn Ladder logic programming which is general for most PLC controllers of all brands i.e. Siemens, Schneider, Rockwell Automation, Allen Bradley, Beckhoff, WAGO, et cetera. So, for now, let us for testing purposes select S7-1200 which is one of Siemens PLC controllers to use in our project. By hitting the yellow small cross icon to add a device. You will see the list of the Siemens controllers that have appeared. For each controller, you can see many versions. Each version represents firmware for example, by selecting S7-1200 CPU 1211C AC/DC/RLY, you will see three versions. Each version represents a specific controller CPU in the market i.e. the selected one if of firmware ver 4.2 as shown in figure 14. By seeing this long list of CPUs and models, that means the software has been installed successfully and is ready to be used in our projects. So congratulation for successfully setting up the working environment for our Lab of ladder logic programming and being ready for utilizing this environment including programming software TIA 15.1 and simulator S7 PLCSIM version 15.1 in our learning and practice.

Figure 13: adding PLC controller wizard

Figure 14: selecting S7-1200 CPU v4.2

What’s next?

We are now all set to write our first ladder program on the TIA software and enjoy simulating our work on the S7 PLCSIM. In our next tutorial, we will write our first ladder logic program on the PLC simulator. Thanks for reading.

Introduction to Ladder Logic Programming Series

Hello friends, I hope you all are fine. Today, we are starting a new tutorials series on Ladder Logic Programming, used in PLC. It's our first tutorial in this series, so we are going to have a look at the detailed introduction to PLC and ladder logic. After welcoming every one of engineers, technicians, students, and hobbyists who have come to read this article willing to learn PLC programming, I would like to introduce one of the most used programming languages of PLC. The language we introduce here is a visualized language that connects and combines graphical symbols in logical flow same as the way we wire electrical circuits and that is the secret behind its simplicity not only in implementation but also in diagnosing problems.

Ladder Logic Programming has been derived from relay logic electrical circuits. This language of PLC programming is called ladder logic or LD as it looks like a ladder of many rungs. Each rung represents a line of logic by connecting inputs logically to form a condition on which the output is determined to be on or off. By completing this article, you will have enjoyed understanding the basics of the LD programming language. Consequently, you will have been able to read a ladder logic code and translate the logic in your mind or the electrical circuit between your hand into ladder rungs.

Relay Logic Control vs PLC

Relay logic control was the old fashion classic control in which input switches and sensors are connected between the hot voltage and relays’ coils to energize these coils and in turn, activate their contacts and thereby connect or disconnect the actuators i.e., motors, lamps, valves etc. You cannot imagine how complicated that control was besides its limitation in functionalities. In addition, it has a huge number of wires and components to achieve a simple logic. Furthermore, there are no chances to change the logic or the sequence of operation without destroying, rebuilding, and rewiring everything from scratch. When it comes to troubleshooting and maintenance, you should be very patient and generous for your time and efforts to pay to keep tracking hundreds of wires and checking up a bunch of components to figure out the problem.

To image the difference between old fashion classic control and PLC, figure 1 shows the case of a very simple process that contains four motors, and four sensors that are connected via four relays and a timer for performing a very simple logic. Let’s say we need to run motor 1 at the start and after a while, we plan to run motor 2 and 4, and at last run motor 3 considering one constraint that no more than two motors can run simultaneously. In addition, sensors will be used as protection to emergency stop motors at any time. Now on the left of the figure, you can see the relay logic control. you can imagine how many components and punch of wiring work for connecting sensors, timers, relays, and motors. On the other hand, PLC-based control shown on the right, you can notice only the PLC and input devices and output actuators are connected to the PLC. To sum up, the number of wires is reduced significantly, the effort of wiring is immensely reduced. In addition, when it comes to modification of logic or process sequence you need to destroy all old wiring and start over rewiring according to the new requirements while you can do this by modifying the program in PLC without touching the wiring. To sum up, PLC reduced the number of components, wires, time of implementation. In addition, the processing is faster thanks to PLC processing and modification becomes programmatically.

How PLC works?

Before opening the door to enter our tutorial on ladder logic programming, let’s have a brief idea about how PLC works and what ladder logic program has to do with PLC. Well! Let me briefly say that PLC has input modules, output modules, and a processor. The input modules are connected to input switches and sensors while the output modules are connected to actuators i.e., motors, valves, lamps. And for sure processor runs the ladder logic program that we going to learn here! In the below figure, I have tried to clear it visually.

The processor works in scan cycles, in each scan cycle, it gazes into all inputs and records them in its memory and then executes the ladder logic to determine the new status of outputs and update them and go to the next cycle, and so on. So now you can tell me what ladder logic has to do with this? That is great to hear you say that it is the logical connection between inputs to determine the output status. I really can not wait to go ahead and hit the nails on the head and open the door to let us get started with our tutorial about ladder logic programming. In the below figure, I have shown PLCs of different manufacturers:

Ladder Logic Programming

Each programming language has a structure and building blocks. The building block of a ladder program is a rung. Yes, rungs of a ladder go step by step to do the designed logic and repeat every scan cycle. Each rung forms a complete piece of logic like one complete circuit. so let us go to know how to form this rung and get to know what components of these rungs and how they are connected logically.

Understanding Rung in Ladder Logic

  • In the below figure, I have shown a single rung of ladder logic programming.
  • It's the simplest ladder logic rung and it has Input on the left side and Output on the right side.
  • The ladder logic rung is composed of two parts:
    • The Left Part represents the condition on which the output is determined to be true or false. This part is a combination of contacts or inputs in series for making AND logic or in parallel to perform OR logic.
    • The Right Part is the output coil or a trigger signal to start timer or counter as we going to elaborate later.
  • Ladder Logic works from left to right and thus according to this rung, if Input is LOW, the output will remain LOW, and if Input gets HIGH, the Output will also get HIGH.
  • So, you can say the above rung is actually representing a simple electrical switch i.e. if we turn ON the switch, Lamp(Output) will get lightened up and vice versa.
  • In this tutorial, I am just giving the overview, so if you are not getting all the things, don't worry, as, in the upcoming tutorial, we will design them on the simulator again & again.
  • Inputs in LAdder Logic PRogramming are normally coming from switches, pushbuttons, sensors etc.
  • Inputs come in two configurations:
    1. Normally Open (NO): shows the input initially at a LOW state and gets HIGH by pressing or activating.
    2. Normally Closed (NC): It's initially is at a HIGH state and gets LOW when it is pressed.

Multiple Rungs in Ladder Logic

In the above section, we have designed a simple ladder logic program, where we have used just a single input to control our output. These contacts/inputs in their two configurations can be connected in series (AND logic), parallel (OR logic), or negation (NOT) to form logical combinations. In the below figure, I have designed three rungs of Ladder Logic, let's understand them one by one:

  • 1st Rung: I have used two Normally Open Contacts in the first rung, so you can think of it as an electrical line having two switches in series. So, the output will get current, only if both switches are on/closed. So, it's actually an AND Gate in Ladder Logic i.e. output will get HIGH only if all the inputs are HIGH.
  • 2nd Rung: In the second rung, I have used both normally open and normally close contacts, so if Input1 gets pressed, the output will get HIGH, but if Input2 gets pressed(actually open/off) then Input1 won't be able to turn ON the Output. So, you can think of Input2 as a master switch.
  • 3rd Rung: Now you must have understood by now that I have designed an OR Gate in this rung, so if any of these inputs get HIGH, the Output will get HIGH as well, because Inputs are connected in parallel.
Note:
  • When two or more inputs are connected in parallel in order to design OR logic, it's called rung branches.

Slightly Complex Project in Ladder Logic

In the below figure, you can see a slightly complex Ladder Logic Program, having different components, let's discuss them one by one:

  • The output coil may have the character “S” to show the output going to be set to true until a reset signal is received.
  • Furthermore, it could have the character “R” to show the reset status of the output to false.
  • Character “P” in the middle of contact shows this is the rising edge of input when it changes from false status to true.
  • On the other hand, character “N” in the center of contact shows the falling edge of the input when it changes from true to false.
  • And the timer and counter blocks are shown in the last two rungs to show the initiation of timer and counter.
  • All these components will be described in detail when the tutorial train reaches its station. So do not worry about them at this point.

Logic Gates & Ladder Logic

If we define the ladder and its building blocks which are rungs how about the word “logic” the second word in ladder logic? Well! Table 1 reviews the logic gates and their truth table. For you to expect the incoming status of your output based on the status of the inputs and the logical combination pattern. to decide the results logic output ( RLO) which is the status of the logical combination of contacts that precede the output coil. For instance, the AND gate/logic function is applied between two or more inputs when all of the inputs should be true to get the output to be true. On the other hand, the OR gate/logic function between two or more inputs is used when we need at least one of the inputs to be true to get the output to be true.

Reading ladder logic rung

  • The reading starts from left to right to complete one rung and then from up to down rung by rung till the end of the program.
  • You can imagine that you read the ladder program instead of PLC.
  • First, you scanned the input and update the input memory and then scan each rung from most left to right to evaluate the RLO and set the new status of the output based on the determined RLO.
  • After completing one rung, plc move to the next rung from top to bottom till the last rung in the program at that point one scan cycle is done and move to the next scan cycle and so on.
  • You can compare Ladder Logic with electrical wiring logic, as both follow the same pattern.

Let’s see one simple example to understand the reading ladder logic program. Figure 3 shows a very simple complete ladder logic rung that connects a normally open pushbutton to start a motor and another normally closed pushbutton to stop the motor. You can see each component of the inputs and outputs should have an address to be uniquely identified by PLC. For instance:

  • The normally open push button named “Run” takes an address of I0.0 which is linked to the first input channel of the first input module in PLC.
  • The normally closed push button to stop the motor called “stop” takes the address I0.1 which is physically connected to channel 2 in the first input module of the PLC.
  • On the other hand, the output coil takes address Q0.0 which is the first output channel of the first output module in PLC.
  • So clearly when you press your push button "RUN" it will connect the motor output coil to the positive line and hence get energized.
  • But when press the "STOP" button, you will disconnect the motor coil from electricity and hence stop it.

What’s next ???

Now, how do you see your progress so far? I can see you are doing progressively and that’s great. However, we still have a lot to learn to master ladder logic programming. However, we need to set up the environment for simulating a Ladder logic program to be able to validate our programs and enjoy seeing its execution typically as if we have a PLC controller and to verify how far our designed ladder performance matches the real-time execution on the simulator.

PLC Projects

Hello friends, hope you all are having fun with your lives. Today, I am going to share links related to PLC projects. I have shared few PLC projects on my blog so here on the post I am gonna compile a list and will post all the PLC projects posted till now. I will keep on updating this list so stay tuned. All the PLC projects posted here are completely designed by our team so if you wanna copy them then you are most welcome but do mentioned the link of respective project as a favor. Its not a very big list rite now but I am gonna update more projects real soon and will update the list. So, let's get started with PLC projects:

PLC Projects

Here's the complete list of PLC projects posted till now on our blog:

Logical Gates in Ladder Logic for PLC

In the previous post Logical Gates in Ladder Logic for PLC, we had an overview of what is Ladder Logic programming and we have also implemented three basic Logical gates in Ladder Logic form. Today, we are gonna have a look at some complex Logical Gates in Ladder Logic for PLC. So, I hope till now you guys have basic knowledge of Ladder Logic and can implement complex logical gates in it. If you haven't read the previous post then must read because without that knowledge you won't understand this post.

In today's post we are gonna implement few complex logical gates. Its not gonna be much difficult if you have the basic concepts. I am just pointing out few important points here. While implementing any gate in ladder logic, always consider rung as an electrical line having HIGH voltage at one end and LOW voltage at the other, while the inputs are simple switches. Voltage will be supplied to the output only when switch is closed i.e. input is HIGH, otherwise the output will remain OFF. You should also have a look at Introduction to Logic Gates.

You have seen in previous post, while implementing OR gate we have used a second switch in parallel which ends at the first rung so overall its a single rung having two inputs in parallel so input can come either from first switch or from second one. So, now let's start implementing some complex logical gates in Ladder Logic for PLC. Today, we are gonna implement these logic gates:

  • NAND Logical gate in Ladder Logic for PLC
  • NOR Logical gate in Ladder Logic for PLC
  • XOR Logical gate in Ladder Logic for PLC
  • XNOR Logical gate in Ladder Logic for PLC

NAND Logical gate in Ladder Logic for PLC

  • NAND gate is another type of logical gate, which is normally used. NAND gate is nothing but a simple NOT of AND gate. In simple words, if we add a NOT gate in front of AND gate, we get NAND gate. The truth tablel of NAND gate is shown in the below figure:
  • It is quite obvious from the truth table of NAND gate that the output will be OFF only when both the inputs will be ON otherwise output will remain ON. So, now lets implement this gate in ladder logic programming.
  • The below image shows the implementation of NAND logical gate in Ladder Logic form:
  • Now, if you understand the above figure, then its quite obvious. We have used both inputs in normally closed form so when both inputs are OFF the output will be ON. If we get X0 ON then in still we will get the HIGH voltage from X1. If we make X1 ON then we get HIGH voltage from X0, but if we get both X0 and X1 ON then our Y0 will get OFF. Again we are using inputs in normally closed form so when our actual input is OFF then our X0 is closed. :)

NOR Logical gate in Ladder Logic for PLC

  • In NOR gate, we simple place a NOT gate in front of OR gate. Its truth table is shown in below figure:
  • From the truth taable of NOR gate, its quite obvious that its output will be ON when both of its input goes OFF otherwise the output will remain ON. Lets implement this NOR logical gate in Ladder Logic diagram.
  • The below figure shows the NOR logical gate in ladder logic diagram:
  • If you got the ladder logic form of NAND gate, then its not gonna be much problem. Simple two normally closed inputs are placed in series, so now when any of them gets ON, then output will get OFF.
Note:
  • If you have noticed, whenever NOT gate is involved somewhere, we use normally closed inputs.

XOR Logical gate in Ladder Logic for PLC

  • The truth table of XOR gate is shown in below figure:
  • From the truth table, we can get this thing that, output will ON only when the inputs are in opposite states and output will be OFF when inputs are in same state.
  • The ladder logic implementation of XOR gate is shown in below figure:
  • Now it has gone a little complex but lets understand how's its working. We have placed X0 and X1 in series and also in parallel, but in first string X0 is normally open and X1 is normally closed while in second string X0 is normally closed and X1 is normally open.
  • Now, what we need to do is if both inputs are in same state we need to turn OFF the output. That's what we are doing in above logic diagram. Let's say X0 and X1 both are OFF then the normally open switches will be OFF and they wont let the HIGH voltage pass and hence our Y0 will remain OFF. And if both are ON then the normally closed will be OFF and again Y0 will remain OFF.
  • Now if X0 is ON and X1 is OFF, then the first string will connect and our output will ON, and if X0 is OFF while X1 is ON then our second string will connect and will make our output ON. Quite simple and easy.

XNOR Logical gate in Ladder Logic for PLC

  • Last but not the least XNOR gate, if we add NOT gate in front of XOR gate we get XNOR gate, let's have a look at its truth table below:
  • So, in XNOR gate, we get our output ON when both inputs are in same state otherwise its OFF. Let's implement it in ladder logic form below:
  • Now in this ladder logic diagram, we are getting introduced with a new symbol, till now we have used normally open output but here for the first time, we are using normally closed output Y0.
  • It's exactly the same logic as we used for XOR gate, but the only difference is ere we are using normally closed output. So, simple when the output is gets ON, its actually OFF and when it gets OFF its actually ON ;)
  • I hope now you got the clear concept of How to do programming using Ladder Logic and what's its difference with Microcontrollers like Arduino or PIC Microcontroller etc.
That's all for today, hope I have conveyed some knowledge. If you are new to PLC programming then you won't get it in the first attempt so my suggestion is give it some time, read it again and again and you will feel better :) I am not gonna post more about Ladder Logic designing, instead in the next post we are gonna have n overview of PLC simulation software in which we design this ladder logic diagrams. Till then take care and have fun :)

Introduction to Ladder Logic for PLC

Hello everyone, I hope you all are doing great. In today's tutorial, I am going to share the detailed fIn the previous post, we have seen Introduction to PLC, which was quite simple and has the basic introduction to PLC. To day we are gonna have a look at Getting Started With Ladder Logic For PLC. Ladder Logic, also named as Ladder Logic Programming, is the programming language for PLCs. Its normally considered as the most difficult language among the engineers because of its complex structure, but if you ask me then I will say its the most interesting programming language.

Ladder Logic is different from the usual programming language of Microcontrollers like Arduino, PIC Microcontroller etc. Microcontrollers programming usually compiled from top to bottom i.e. the compiler first capture the first statement and then moves downward till it reaches the end line but that's not the case with Ladder Logic Programming for PLC. In ladder logic, the compiler moves from left to right and it gets all the lines at the same time. It seems bit difficult to understand at first but be with me and you will get it at the end. :)

Introduction to Ladder Logic

Ladder Logic is a programming language used for PLC as C for Microcontrollers. Ladder logic is a combination of rungs. Each rung is executed from left to right. For example, have a look at the below figure, a single rung of ladder logic is shown in it.

  • In the above figure, a single rung of ladder logic is shown. Now as I mentioned earlier, each rung is executed from left to right, so the above rung will also do the same behavior.
  • There are two symbols mentioned in the above rung, one is X0 and the other is Y0. X0 is placed on the left side while Y0 is placed on the right side.
  • We have seen in our previous tutorial Introduction to PLC, that X always indicates input and Y indicate output, so in short the above rung has input on the left side while output on the right side.
  • So combining all the above discussion, we come to the conclusion that input will be executed first and then output will be executed, as shown below. (I hope you got the basic theory now :) )
  • So now the thing is, if we only consider the above rung, the output Y0 will be ON only if input X0 will be ON. If X0 is OFF then Y0 will also be OFF. Consider this rung as a voltage wire as shown in below figure:
  • So, now the output will be ON only when it has HIGH and LOW but input is acting as a switch and in normal condition, its OFF so HIGH is not reaching to output so it will remain OFF, as we turn ON the input X0, it will be like the switch is closed and HIGH will pass through the X0 and will reach Y0 and Y0 will turn ON. I tried my best to explain it as simple as I can but still having confusion, ask in comments.
  • Now let's have a look on different logical gates i.e. AND gate, OR gate, NAND gate etc, we normally create in ladder logic.

1. Logical AND in Ladder Logic for PLC

  • We all know about the Logical AND gate, in AND gate we get output only if both the inputs are HIGH, otherwise OUTPUT remain OFF.
  • The below figure shows the same logical AND gate designed in Ladder Logic diagram:
  • Now in the above figure Y0 will be ON when both X0 and X1 are ON, otherwise Y0 will be ON, again consider inputs as switches.
  • Its a simple 2 input AND logic, we can add as many inputs as we want in it. For example, below image shows a four input AND gate.

2. Logical OR in Ladder Logic for PLC

  • In Logical OR gate, output goes ON when any of the inputs is ON, lets implement it in our ladder logic form.
  • The below figure shows the ladder logic form for OR gate:
  • If you check the above figure, if X1 goes ON then our connection will connect and Y0 will be ON, similarly, if X0 is OFF and X1 is ON then again Y0 will be ON because now voltage is coming from X1. Again consider inputs as switch.

3. Logical NOT in Ladder Logic for PLC

  • In logical NOT gate, output is always opposite to input, if input is HIGH then output will be LOW and vice versa.
  • In order to implement NOT gate, we have to consider another type of input, the input which we are using till now is normally open input, means it is open (OFF) in its normal condition and gets closed (ON) when input is supplied, but in ladder logic there's another type of input also present named as normally closed input.
  • Normally closed input is closed (ON) in its normal condition and goes open (OFF) when it gets actual input.
  • So, using this normally closed input, we can quite easily implement this Logical NOT gate in ladder logic as shown in below figure:
  • In the above figure X0 will be ON in normal condition and hence, Y0 will also be ON and when we get actual input then X0 will get OFF and our Y0 will also be OFF.
So, that was all for today. As you have got the basic knowledge of Ladder Logic, so now its time to have a look at How to design Logical Gates in Ladder Logics for PLC. Thanks for reading. :)

Introduction to PLC

Hello friends, I hope you all are fine and enjoying good health. Today's tutorial, as the name shows, is on Introduction to PLC. PLC is an abbreviation of Programmable Logic Controller. Recently I worked on a project in which I have to design a Automated coffee Mixing Machine Using PLC. It worked quite good and I had a great time while working on it. After completing that project, it occurred to me that I haven't posted any tutorial on PLC. So I thought of starting this tutorial. This tutorial is not gonna cover in single post so my plan is to divide it in parts.

Today. I am gonna give an overview about PLC. We will have a look on basics i.e. what is PLC? Why we use PLC instead of microcontroller like Arduino or PIC Microcontroller? What's its advantages and disadvantages? I will try to cover all about the basics. After reading this tutorial, you must have a look at Introduction to Ladder Logic for P L C, Ladder Logic is programming language for PLCs.

There are different types of PLCs available in the market manufactured by different companies, so its impossible to cover all of them. In this tutorial, I am gonna discuss Fatek PLC as I have worked on it during my project. The model I have used is Fatek PLC Fbs-20MA. The reason I used this model because it was cheap and has enough input/output ports sufficient for my project. That's why I preferred it as its engineers' task to optimize the cost as well. Let's get started with PLC.

What is PLC?

Its a basic question, which is normally asked by all the starters so I am gonna reply it first for the newbies.

  • PLC is nothing but an advanced form of Microcontroller. It is usually used in industries because of its flexibility and ease of use.
  • It can be attached quite easily with computer via serial port as well as usb port.
  • PLC is used when we need to automate anything just like microcontroller. We attach our sensors and actuators etc with PLC and then insert some programming code in it and let it do its job.
  • You have seen automated lifts, they all are operated with PLC.
  • We can use timers, counters, registers in PLC and can get any kind of output from it.
  • We can program PLC with different languages and the most commonly used language for PLC is named as Ladder Logic.

Internal Overview of PLC

What's inside PLC, which makes it so cool ? That's a good question and normally engineers wonder about it. PLC can be divided into 3 sections, which are as follows:

  • Power Supply - Thissection provides power to the PLC, in my case it is operated on 220V AC, so when I provide 220V AC to my PLC, it got activated and start performing functions.
  • Centeral Processing Unit (CPU) - Its the actual brain of PLC, it is further divided into several parts i.e. RAM, ROM, EEPROM, microcontroller etc. The programming code is uploaded in this CPU and according to that program, it performs its functions.
  • Input / Output Section - This section is the one from where PLC communicates with the external world. We can attach sensors to the inputs of PLC and can operate our motors, actuators etc from the outputs of PLC.

Types of PLCs

  • There are different types of PLCs available in the market manufactured by different companies.
  • Few famous PLC companies are Siemens, Mitsubishi, Fatek etc.
  • Moreover, they are also available in different sizes and functions. The one I used has 14 inputs and 8 outputs. It doesn't support analog inputs as I don't require them.
  • There are PLCs available with analog inputs or you can also buy cards which are interfaced with the PLC and make them capable to work on analog inputs.
  • Another function which is not available in my PLC is the Serial communication, but such models are available which supports serial communication.
  • So in short, there are several models of PLC available in the market and you have to consider your project demands while buying a PLC.

Why use PLC instead of microcontrollers?

  • Microcontroller is normally used in small products, where you need to control some sensors or some motors etc but when we talk about big automated plants in industries then PLC is always preferred over microcontroller.
  • The reason for preferring PLC over microcontroller in big projects is because of its flexibilty and ease of use. PLC can be programmed frequently with computer, suppose you have an automated system and you find some bug in it which you wanna remove, then what you need to do is simply attach a computer with the PLC of that plant and make changes in the code, which isn't possible with the microcontroller.
  • Moreover, PLC has lot of memory, you can add any size of data in it.
  • PLC is also long life as compared to microcontroller.
  • Last but not the least, PLC has built in cards to control heavy AC voltages, you can get any kind of voltage from PLC i.e. 220V AC etc but if you wanna get such voltages from microcontrollers then you have to add some extra circuitry.
  • In short, in all industrial automated plants, PLC is used.

Getting Started With PLC

I think now you have the idea about PLC, so now I am getting started with PLC. I am gonna explain the functioning of Fatek PLC as I have used that one but if you are using another model of PC then no need to panic as all PLCs have same functionality. So, it doesn't matter which one you are using. If you check the below image then you will see I have marked three sections in it.

  • Section 1 is indicating the status of input pins. If inputs are off then this section will remain as shown in above figure, but if any of the inputs get high, then the respective pin indicator will also glow into red, which indicates that this pin is ON. It helps while you are writing programming code for the PLC.
  • Section 2 indicates the status of PLC. If PLC is powered up then the POW led will go red, if you have uploaded the code in PLC and start it then the RUN led will go red and if your code has some error then ERR led will go red.
  • Section 3 indicates the status of output pins, it will tell you which output is currently ON.

In the below image, I have indicated Section 4 and 5, these are the input/ output section. If you have a look at it closely then you can see there are two rows of screws, where you plug your wires for inputs and outputs and above them, they are also labelled with white color. So, it goes like that, first row of labelling is for first row of screws and second row is for second row of screws.

  • Section 4 is the inputs pins section, so if you check above there are inputs from X0 to X13, which makes it overall 14 inputs. Moreover, there are two pins labelled as + 24V and - 24V, which PLC is providing us, so if you wanna give any input to this PLC you have to make sure that its 24V, otherwise PLC not gonna recognize it.
  • Section 5 is the output pins section. and you can see there are total 9 outputs starting from Y0 to Y8, now you are getting confused with C0 to C6. C pins are actually the voltage setter, let me explain, in projects there are different outputs are required like your motor is running at 12V DC while your solenoid valve is running at 220VAC. So, there's a need to set these voltages at the output. Here C pins are used. Suppose you need to give 12V output at Y0 and Y1 then give this 12V at C0 and when Y0 or Y1 is ON then they will give 12V at output. In short, when output gets ON it is actually connecting with their respective C pin.
  • Lastly, check the Port 0 in the above image, this is the port where you plug your serial wire inPLC and connect it with your computer in order to upload the programming code.

That's all for today. I hope you got the basic idea of Programmable logic controller and now its time to have a look at Introduction to Ladder Logic for P L C, ladder logic is programming language for PLC. Your feedback are warmly welcome. In the next tutorial, I am gonna cover about ladder logic and will show you how to program a PLC. Till then Take care and have fun.

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