Ladder Logic Tutorial – Part 2: Building Logic

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 Ladder Diagram Symbol
SET Latch Coil
RESET Latch Coil Ladder Diagram Symbol
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)
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)
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
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
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 Ladder Logic
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:

FunctionHardwarePLC AddressTag Name
Start Motor 1Momentary Push Button (N.O.)I0.0START1
Start Motor 2Momentary Push Button (N.O.)I0.1START2
Start Motor 3Momentary Push Button (N.O.)I0.2START3
Stop Motor 1Momentary Push Button (N.C.) I0.3STOP1
Stop Motor 2Momentary Push Button (N.C.) I0.4STOP2
Stop Motor 3Momentary Push Button (N.C.) I0.5STOP3

Outputs:

FunctionHardwarePLC AddressTag Name
Motor 1Motor RelayQ0.0MOTOR1
Motor 2Motor RelayQ0.1MOTOR2
Motor 3Motor RelayQ0.2MOTOR3

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
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
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:

FunctionHardwarePLC AddressTag Name
Start ButtonMomentary Push Button (N.O.)I0.0START
Stop ButtonMomentary Push Button (N.C.)I0.1STOP

Outputs:

FunctionHardwarePLC AddressTag Name
MotorMotor RelayQ0.0MOTOR

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:

FunctionHardwarePLC AddressTag Name
Start ButtonMomentary Push Button (N.O.)I0.0START
Stop ButtonMomentary Push Button (N.O.)I0.1STOP

Outputs:

FunctionHardwarePLC AddressTag Name
HeatingHeating Element RelayQ0.0HEATING
Fan 1Fan Motor RelayQ0.1FAN1
Fan 2Fan Motor RelayQ0.2FAN2

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:

FunctionHardwarePLC AddressTag Name
Start Button ValveMomentary Push Button (N.O.)I0.0START_V
Stop Button ValveMomentary Push Button (N.C.)I0.1STOP_V
SensorDigital PNP SensorI0.2SENSOR1
Start 1 Button MotorMomentary Push Button (N.O.)I0.3START1_M
Start 2 Button MotorMomentary Push Button (N.O.)I0.4START2_M
Stop Button MotorMomentary Push Button (N.C.)I0.5STOP_M

Outputs:

FunctionHardwarePLC AddressTag Name
ValveElectrical ValveQ0.0VALVE
MotorMotor RelayQ0.1MOTOR

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!

16 Comments
Leave a response
  • Joe Welch
    February 20, 2019 at 7:28 pm

    Hi, my name is Joe Welch and I appreciate your course. On the three motor exercise, on paper, I made the same ladder you used for two motors except I added Motor3. I built the ladder and for each motor, I crossed through the Stop so the Motor1 was stopped, Motor2 and Motor3 was stopped, while the other motors were left open.

    Is this the solution?

    • peter
      February 21, 2019 at 8:56 am

      Hey Joe!
      Yes, this exercise is about interlocking. Each of the 3 motors need the 2 other motors as conditions. But negated conditions, so that each motor only can run when the 2 others are NOT running 🙂
      Peter

  • Jose
    March 1, 2019 at 9:17 pm

    Is the exercise about interlocking the 3 motors correct? I tought we were supposed to use a normally closed instruction for the stop button

    • peter
      March 3, 2019 at 4:35 pm

      Hey Jose! Thanks for your comment. Normally for stop buttons you will use normally closed contact wired to the input. Then you will have the negated logic and you can just use a normally open instruction in your PLC program. The reason is that if you use normally open for a stop button and a wire breaks, then the stop button will not work, and that could lead to a dangerous situation. Read more about it in my article about normally closed for stop buttons.

      • Saddam
        August 25, 2019 at 3:00 pm

        peter may i have your mail i.d. You made things very eady. You are great. Learning ladder was never so easy before.

  • Rob
    March 6, 2019 at 1:03 am

    Hey Peter,
    Thanks for the tutorial! I was wondering if you had solutions for the examples, I would love to double check my work and see if I have the correct solutions. Thanks!

  • kp kanziya
    April 5, 2019 at 5:33 am

    Hi PETER

    I am kp kanziya. Passing my 72nd of the life years Trying to learn PLC.
    Attempted several times to learn PLC.
    This is first time that YOUR Tutorial cleared off all my confusions about PLC programming.
    Thanks a lot Dear PETER.

    • peter
      April 10, 2019 at 6:39 pm

      Wow, I’m so impressed by you! And happy that I could teach you a thing or two about PLC programming. Please let me know here or contact me if you want to learn more about PLC’s and PLC programming 🙂

  • JD
    April 16, 2019 at 3:38 am

    Hi Peter,

    Thanks for the tutorial. I would like to ask if I can get the solutions to your exercises? I would love to double check my solutions if its right.
    Thanks!

  • Henry
    April 18, 2019 at 7:08 am

    Nice tutorial! Thank you very much!
    Is there a way to find the answers to the exercises?

  • Raghad Agha
    May 2, 2019 at 9:09 pm

    Hello Peter,
    Thank you so much for your great tutorial.
    Would you please post your examples solutions, so I can verify my work/solutions?

  • Younes Obad
    June 25, 2019 at 7:20 am

    Hi Peter. Thanks for clarifying on interlock, rising and falling edge. Please can you send me for the solutions for each example to double check my work.
    5hank you

  • Pedro
    July 5, 2019 at 1:42 pm

    Hi Peter, Love your work mate, i also would like your solutions to your examples cheers mate.

  • Nusret YILMAZ
    July 11, 2019 at 6:13 am

    Great ! Your style of teaching is quite basic, clear and understandable. Thanks !

  • Alfonso
    August 12, 2019 at 6:04 pm

    Can I get the solutions to the exercises?

  • Alfonso
    August 12, 2019 at 6:04 pm

    Is there a way you could share the exercises solutions?

Leave a Response