Welcome to the second part of my ladder logic tutorial. In part 1 of the ladder logic tutorial, I introduced you to the very basics of ladder logic and to some basic instructions.

In this second part of the tutorial I will teach you how to solve actual problems with ladder logic and how to use it to build PLC programs. At the same time you will be introduced to several other ladder logic instructions:

So, let’s get started from where we left in part 1: latches. In PLC programming the latch is not the only way to hold the state of a coil.

Set / Reset Coils

The next two instructions we’re going to look at are called set and reset. Or in Allen Bradley terminology latch and unlatch coils. The whole point of these is to free you from using contacts as latching. Thereby you can set an output in one place in your ladder logic. You can then reset the coil in a totally different place in your ladder logic.

Let me illustrate how they work:

SET Latch Coil RESET Latch Coil

As you can see the set / reset consists of two coils. A set coil and a reset coil. But even though they are two separate coils, they control the same bit.

What this means is that when you give a pulse to the set coil, the bit will be set. In contrast to a normal coil the set coil will keep the bit set even though the conditions before the coil changes. The only way to reset the bit again is to give a pulse to the reset coil.

When you’re building larger PLC programs with thousands of bits, some even connected to a SCADA system, make sure you know where you’re setting the bits, and where you are resetting the bits.

Set or Reset Priority

Building a set / reset function with two different coils introduces a new problem:

Which one of them has the highest priority?

If both conditions for setting and for resetting is true, what will the state of the coil be in the end?

It is important to know about this because even though it’s considered bad programming practice, it can still happen that both conditions are true. Typically this happens under a fault condition and for safety reasons it is important to know what then happens.

What the PLC executes last is will be the result.

If you set the coil in one network and reset it with the same condition in a network just below, the coil will be reset. Remember, the PLC executes ladder logic one instruction at a time – from top to bottom.

If you put the set and reset in different POU’s then the result will come from the last POU the PLC calls and executes.

Set / Reset Function Blocks

Another way the set / reset functionality can be implemented is with function blocks. They can be used in not only ladder logic, but also in function block diagram and structured text.

They are pretty straight forward and easy to use. What is smart about these function blocks is that the set and reset condition are in the same place. But because of this there are actually two different function blocks that can implement the set / reset functionality.

Each of the two function blocks has different priority for the set and the reset. Here’s the first one:

SR Function Block

The first function block is officially called a mono-flop with dominant set, but is also referred to as SR or set/reset function block. This has set as the highest priority which means the output will be set if both set and reset conditions are true at the same time.

Here’s how the SR function block looks like:

Set/reset function block (set dominant)

As you can see it has one input for setting and one for resetting. On the other side it has the output.

RS Function Block

The other function block is called RS or a mono-flop with dominant reset. Basically it works in the exact same way as with the other set / reset function block with one crucial difference.

With this block you will have reset as the highest priority. If both conditions are true at the same time, the output will be reset as the final result.

Reset/set function block (reset dominant)

Again, this block has two inputs and one output.

Building Logic with Ladder Logic

Now, after you have learned about these new set and reset ladder logic instructions, I think it is time to start building some actual logic.

Because to be a good PLC programmer in ladder logic is not only to know about the instructions. It is to be able to solve problems using ladder logic. So let me show you some examples of real world problems solved with ladder logic PLC programs.

One thing I like to do when solving problems with ladder logic is to think in conditions. This is more formally known as combinatorial logic. You have probably even heard about it before. Or at least the logic gates – AND, OR XOR etc.

Since I already wrote about the logic gates and how they work in the article about combinatorial logic I will not go into how they work here. What I will rather do is to solve some real world problems using combinatorial logic.

Logic Gates with PLC Instructions

The first example i want to give is interlocking. Because with simple instructions you can build the logic required to make up an interlocking functionality.

Interlocking is used in many PLC applications because it prevents things from being active while other things are active. A well known example of this is the star/delta motor. If you’re controlling that with a PLC you have to make sure that the output for the star relay cannot be activated if the delta relay output is active.

Start / Stop Logic Example

Let’s take a look at a simple example of interlocking with two motors, where only one can run at a time. Each motor has a start and a stop button to start and stop the motor.

Start / Stop of 2 Motors

The start and stop of the motors can be implemented with the latching or set / reset functions I talked about earlier in this tutorial. In this example I will use the latching using the examine if closed (normally open) instruction.

You might wonder why I chose the latching over set / reset coils. I did that because with these simple instructions we can quickly implement the logic.

The first conditions for the start and stop buttons for each motor is of course conditions for the motor to start or stop. We should look at the conditions for the motor. And since the motor can be in two states, we need to look at the conditions for both of these states:

START AND NOT STOP Motor (active)

STOP Motor (inactive)

Since the motor can only be in two different states we only have to look at the conditions for the motor to be activated. Because if these conditions are false the motor will be inactive.

We can also add the latching functionality which will be the motors themselves:

START OR MOTOR AND NOT STOP Motor (active)

What is important to notice here is that we have broken down a function description into logic. Each of the buttons and the motor represents conditions for the states of the motor. Between these are the logic relation between them.

These logic relationships can with ease be implemented with ladder logic. Because each of these logic relationships can be translated into ladder logic.

OR and AND Gates with Ladder Logic

The logic relationship between the start button and the motor itself is OR. Either the start button or the motor will set the motor active.

In ladder logic the OR logic is implemented with a parallel connection of two instructions:

OR Logic implemented with Ladder Logic as parallel connection.

On the other hand the stop button has to be not pressed for the motor be keep being activated. But the logic relationship between the start and motor, and the stop button is AND. Because for the motor to be activated other the start conditions and not the stop button had to bee true.

AND logic is implemented with ladder logic as a serial connection of instructions:

AND Logic implemented with Ladder Logic as serial connection.

One of the major advantages of ladder logic is that you graphically can build logic. Because if you follow the lines from left to right at each network, you can see the logic relationships between instructions.

OR always has two options, and thereby to lines. AND only has one option and therefore only one line.

NOT Logic Gate with Ladder Logic

There is still one logic relationship left between the conditions above. For the motor to be active the stop button has to not be pressed. For this we will use the NOT logic.

NOT logic or negation can be implemented with a negated contact either in the ladder logic or as a negated physical contact. These are also known as examine if open or normally closed instructions.

Logic NOT Gate made with Examine if Open in Ladder Logic

As I talked about in part 1 of this ladder logic tutorial, I will not implement the stop button with an examine if open PLC instruction for safety reasons. Instead, I will use a normally closed contact as stop button and thereby negating the logic. Said in another way, the physical normally closed contact will make up the NOT logic for the stop button.

Here’s is how the ladder logic will finally look like after the logic relationships are implemented:

Interlocking with Ladder Logic

Of course this piece of ladder logic is only the logic for one motor. In our example we had two motors. Therefore the full ladder logic will look like this:

At least we need to implement the interlocking. And believe it or not – interlocking can also be seen as logic.

In fact, it is quite simple to implement if you think about it as logic. Because the only thing out motors need to interlock them is one extra condition. That condition will be negated since we need the motor not to be active when the other is. So for motor 1 to be active, motor 2 needs not to be active and vice versa:

START1 OR MOTOR1 AND NOT STOP1 AND NOT MOTOR2 Motor 1 (active)

START2 OR MOTOR2 AND NOT STOP2 AND NOT MOTOR1 Motor 2 (active)

Since we have added one condition more in a logic AND relationship with the other logic, we can implement it in ladder logic with a serial connection of a negated contact:

This was a simple example of how to build PLC programs in ladder logic by breaking down the problem into actual logic. But these logic relationships can not only be used with normally open and normally closed instructions. They can be used with many different PLC instructions – even counters and timers.

Now, the whole point of this tutorial is that you will learn how to build some ladder logic. That’s why it is your turn now. Just below I made a ladder logic exercise for you to practice.

Go ahead and try to solve the problem. You can use any ladder logic software or you can just draw the ladder logic by hand. Try to break the exercise down into the logic you’ve learned about in this tutorial.

Ladder Logic Exercise 1: Interlocking

Function: Start / stop of 3 motors, but only 2 motors can run simultaneously. For example if motor 2 and motor 3 is running, you cannot start motor 1.

Inputs:

Function Hardware PLC Address Tag Name Start Motor 1 Momentary Push Button (N.O.) I0.0 START1 Start Motor 2 Momentary Push Button (N.O.) I0.1 START2 Start Motor 3 Momentary Push Button (N.O.) I0.2 START3 Stop Motor 1 Momentary Push Button (N.C.) I0.3 STOP1 Stop Motor 2 Momentary Push Button (N.C.) I0.4 STOP2 Stop Motor 3 Momentary Push Button (N.C.) I0.5 STOP3

Outputs:

Function Hardware PLC Address Tag Name Motor 1 Motor Relay Q0.0 MOTOR1 Motor 2 Motor Relay Q0.1 MOTOR2 Motor 3 Motor Relay Q0.2 MOTOR3

I hope you enjoyed and learned something from the first exercise.

This exercise introduced another problem that will lead us to the next ladder logic instructions. The problem is not so much the ladder logic itself, but rather comes from the buttons connected to the inputs.

One Shots for Signal Edges

Normally you would expect the operator to press the start and stop buttons only once – and for a short time. But the operator can hold the buttons pressed for longer time or the button can simply be stuck when pressed. This is a typical hardware problem that will affect our PLC program.

If the stop button for one of the motors somehow gets stuck you will not be able to start the motor. In fact, this is a problem often encountered when you have momentary push buttons as inputs on the PLC.

But don’t worry. There is a solution to this problem. Luckily for us there are ladder logic instructions that can solve this problem. They are called one shots.

Rising Edge Detection

Now it is time for another ladder logic instruction. I will introduce you to the first type of one shot instruction – rising edge detection, R_TRIG or positive edge detection. On some platforms like Allen Bradley this instruction is known as one-shot rising or just OSR instruction.

As the names imply these ladder logic instructions are looking at the rising edge of a digital signal. This means that they will only look at the change in the digital signal from 0 to 1.

Here’s an illustration of the positive edges of a digital signal:

Positive Edges of a Digital Signal

The length of the signal is irrelevant, because we’re only looking at the edge or the change in the state of the signal. And for this reason, these ladder logic instruction for detecting rising edges are perfect for the start and stop button in the example before.

Positive Edge Contact and Coil

You’ve already learned about the normally open or examine if closed instruction. That instruction is evaluating the state of bit. If the state of the bit was 1 or true, then the result of the evaluation would also be 1 or true.

The positive edge sensing contact looks a lot like the examine if closed instruction. In fact, it has the same symbol except this one has a P in the white space:

Positive Transition-Sensing Contact

Also the functionality is a little different. Because this instruction is not evaluating the state of a bit, but rather the change in the state of a bit. A positive change or a change from 0 to 1 to be more specific.

Now, since this instruction is only looking at a change in the state of a bit, this instruction will only evaluate to true for one scan time. Because if the state of a bit is 0 and in the next scant time it is 1, then the instruction will evaluate to true. During the next scan time the state of this bit will probably still be 1. And since there is no change in the state of the bit, the instruction will evaluate to 0 or false.

This is exactly the reason these instructions are called one shots. They only evaluate to 1 or true in the same scan time as the state changes. Here’s how the instruction functions in slow motion:

Positive Edge Detection in Ladder Logic during PLC Scan Time

As you can see, this instruction only gives out a pulse in the scan time where the positive transition happens.

At last, you can also find the positive edge detection as a coil instead of a contact:

Positive Transition-Sensing Coil

It has the exact same functionality except that this is a coil.

R_TRIG Function Block

You can also do positive or rising edge detection with a function block. That one is called R_TRIG and is a standard PLC instruction. I will not go into details about how it works, since I already described it in my article about function block diagram. It works pretty much the same way as the positive edge contact.

Falling Edge Detection

Just like the positive edge you can also find instruction to detect a negative edge. These are naturally called negative edge detection, falling edge detection or F_TRIG. Allen Bradley calls it one-shot falling or just OSF instruction.

Common for all of these instructions is that they are looking for a change in the state of the signal from 1 to 0. Here’s an illustration of a negative or falling edge of a digital signal:

Negative Edges of a Digital Signal

Falling edge detection is a functionality that can be very useful. You can for example use them to detect when something turns off.

Negative Edge Contact and Coil

You will find the negative edge detection both as a contact and as a coil instruction. Again, they are called one-shots because they are only active in the same scan time as the transition from 1 to 0 happens.

Negative Transition-Sensing Contact

Depending on where in your ladder logic you need to detect a negative edge you can use either of them.

Negative Transition-Sensing Coil

A typical application for this instruction is when you need to start something when another thing stops. It could be a heating element and two fans for example. When the heating element is running one of the fans need also to run. But as soon as the heating element turns off, the second fan needs to run for faster cooling.

You can try to solve this example in ladder logic exercise 3, and see if you can use the negative edge detection to start the second fan.

F_TRIG Function Block

Again, you will also find a function block for negative or falling edge detection. It is called F_TRIG and it basically works like the negative edge contact. You can read all about F_TRIG here.

With all this information, I think it is time for some exercises. In my opinion, the best way to learn is to solve problems. In these exercises you will have to use the rising and falling edge detection. But before that, there is one last thing you should know.

Remember that the one-shot instruction only were active in one scan time. If you want to use them to start something that has to run for longer time, you will need either a latch or do it with set / reset. Otherwise you risk only activating the output for one scan time.

Here’s the first exercise to practice with the rising edge detection:

Ladder Logic Exercise 2: Rising Edge

Function: A start and a stop button is used for starting and stopping a motor. But make sure that the buttons can only start and stop the motor on a positive or rising edge.

Inputs:

Function Hardware PLC Address Tag Name Start Button Momentary Push Button (N.O.) I0.0 START Stop Button Momentary Push Button (N.C.) I0.1 STOP

Outputs:

Function Hardware PLC Address Tag Name Motor Motor Relay Q0.0 MOTOR

And for the falling edge detection, here’s another exercise:

Ladder Logic Exercise 3: Falling Edge

Function: A start and stop button turns on and off a heating element and a fan. When the heating element turns off, the second fan has to start. The second fan will turn off as soon as the heating element and fan turns on.

Inputs:

Function Hardware PLC Address Tag Name Start Button Momentary Push Button (N.O.) I0.0 START Stop Button Momentary Push Button (N.O.) I0.1 STOP

Outputs:

Function Hardware PLC Address Tag Name Heating Heating Element Relay Q0.0 HEATING Fan 1 Fan Motor Relay Q0.1 FAN1 Fan 2 Fan Motor Relay Q0.2 FAN2

These two exercises should have taught you how to use the one-shot instructions to prevent things from happening more than once.

Before the end of this second part of my ladder logic tutorial I would like to give you one last exercise. Because in this part you learned all about building logic. So let us see if you can translate a logic expression into ladder logic:

Ladder Logic Exercise 4: Logic

Function: Implement the following logic for a valve and a motor output:

VALVE:

(START_V AND NOT SENSOR1) OR VALVE AND NOT STOP_V AND NOT MOTOR

MOTOR:

START1_M OR MOTOR AND START2_M OR NOT VALVE AND NOT STOP_M

Inputs:

Function Hardware PLC Address Tag Name Start Button Valve Momentary Push Button (N.O.) I0.0 START_V Stop Button Valve Momentary Push Button (N.C.) I0.1 STOP_V Sensor Digital PNP Sensor I0.2 SENSOR1 Start 1 Button Motor Momentary Push Button (N.O.) I0.3 START1_M Start 2 Button Motor Momentary Push Button (N.O.) I0.4 START2_M Stop Button Motor Momentary Push Button (N.C.) I0.5 STOP_M

Outputs:

Function Hardware PLC Address Tag Name Valve Electrical Valve Q0.0 VALVE Motor Motor Relay Q0.1 MOTOR

That was all I had for this second part of my ladder logic tutorial. I hope you learned a bit about building logic with ladder logic. I will advice you to keep practicing logic and solving problems with ladder logic. Only in that way will it become natural for you to build logic solutions.

Please share this tutorial or leave a comment below and let me know what you think!