Ladder Logic Tutorial – Part 2: Building Logic

This is the second part of my ladder logic tutorial. In part 1 of the ladder logic tutorial, I introduced the basics of ladder logic and some of the basic instructions. Here, we will build on that starting point.

In this second part, I will show you how to solve practical problems with ladder logic and use those solutions to build PLC programs. As we work through the examples, you will also learn several more ladder logic instructions:

We will pick up where part 1 ended: latches. In PLC programming, there are other ways to hold a coil’s state as well as using a latch.

Set / Reset Coils

We will look next at the set and reset instructions. Allen Bradley calls these latch and unlatch coils. These instructions let you work without contacts as latching. You can set an output in one place in the ladder logic, then use a separate instruction elsewhere in the program to reset that coil.

The symbols below show the two instructions:

SET Latch Coil Ladder Diagram Symbol
SET Latch Coil
RESET Latch Coil Ladder Diagram Symbol
RESET Latch Coil

The set / reset arrangement has two coils, one set coil and one reset coil. They are separate instructions, but they both control the same bit. This lets each coil change that bit from a different place.

Giving the set coil a pulse sets its bit. With a normal coil, changing the conditions before the coil would change the bit again. A set coil keeps the bit set even after those conditions change. The only way to reset it is to give a pulse to the reset coil.

As PLC programs grow, they can contain thousands of bits. Some of those bits might also be connected to a SCADA system. Make sure you know where in the program each bit is set and where it is reset, so you can follow what controls its state.

Set or Reset Priority

Using two separate coils for set / reset raises a question:

Which one of them has the highest priority?

When the setting and resetting conditions are both true, what is the final state of the coil?

Both conditions can become true at the same time, even though this is considered bad programming practice. It typically occurs during a fault. For safety reasons, you need to understand which result takes priority and what the program will do when that happens.

What the PLC executes last is will be the result.

If a network sets the coil and a network immediately below resets it under the same condition, the final result is a reset coil. This follows the order in which the PLC executes ladder logic: one instruction at a time, from the top of the program to the bottom.

When the set and reset are in different POUs, the result depends on which POU the PLC calls and executes last.

Set / Reset Function Blocks

You can also implement set / reset with function blocks. These work in ladder logic, function block diagram and structured text.

These function blocks are straightforward to use. They keep the set condition and reset condition together in the same place, so you can see both in one block. This also means there are two versions of the block for implementing set / reset, depending on how the conditions are handled.

The blocks differ in which condition takes priority, set or reset. We will look at the first block below:

SR Function Block

The first block is officially called a mono-flop with dominant set. You will also see it called SR or the set/reset function block. Set has the highest priority in this version. If the set and reset conditions are true at the same time, the resulting output will be set.

The SR function block looks like this:

Set/reset function block (set dominant)
Set/reset function block (set dominant)

It has a set input and a reset input, with the output on the opposite side.

RS Function Block

The other function block is called RS, or a mono-flop with dominant reset. It works the same way as the other set / reset function block, except for which condition takes priority when both are true.

Reset takes priority in this block. When both conditions are true together, the output ends up reset.

Reset/set function block (reset dominant)
Reset/set function block (reset dominant)

This block also has two inputs and one output.

Building Logic with Ladder Logic

With set and reset instructions covered, we can begin using the instructions to build logic.

Knowing the instructions is part of learning to program in ladder logic. You also need to use those instructions to solve problems. The examples below show how to work from a real-world problem towards a solution built with a ladder logic PLC program.

I like to approach a ladder logic problem by thinking in conditions. The formal name for this is combinatorial logic. You might have come across it already through logic gates such as AND, OR and XOR.

I explained how logic gates work in the article about combinatorial logic. Here, we will apply combinatorial logic to real-world problems instead of covering the gates again.

Logic Gates with PLC Instructions

Our first example is interlocking. Simple instructions can form the logic needed for an interlocking function.

Interlocking appears in many PLC applications. It prevents one thing from being active at the same time as another. The star/delta motor is a familiar example. If a PLC controls that motor, the star relay output must not be activated while the delta relay output is active.

Start / Stop Logic Example

Consider a simple interlocking example with two motors. Only one motor may run at a time. Each motor has a start button and a stop button, which the operator uses to start and stop that motor.

Start / Stop of 2 Motors

We can use either latching or the set / reset functions from earlier in the tutorial to start and stop the motors. In this example, I will use a latch made with the examine if closed (normally open) instruction.

I chose latching because these simple instructions let us put the required logic together quickly.

First, we need to describe the conditions for each motor to start and stop. The start and stop buttons supply those conditions. Since the motor has two possible states, we can write down what the conditions mean for both its active and inactive states:

START AND NOT STOP

Motor (active)

STOP

Motor (inactive)

Because the motor has only two states, we only need the conditions that activate it. When those conditions are false, the motor is inactive.

We can add latching by including the motor itself in the conditions:

START OR MOTOR AND NOT STOP

Motor (active)

We have taken a description of what the motor must do and broken it down into logic. Each button and the motor itself supplies a condition for a motor state. The words between those conditions describe how they relate logically.

We can put these relationships into a ladder logic program. Translating each logical connection into ladder logic lets us build the program from the conditions we have just written.

OR and AND Gates with Ladder Logic

OR connects the start button and the motor itself. Either one can make the motor active.

In ladder logic, OR logic uses a parallel connection of two instructions. Either path can satisfy the condition:

OR Logic implemented with Ladder Logic
OR Logic implemented with Ladder Logic as parallel connection.

For the motor to stay active, its stop button must remain unpressed. The combined start and motor condition therefore has an AND relationship with the stop button condition. The start condition must be true, and the stop button must also be unpressed for the motor to run.

In ladder logic, AND uses a serial connection of instructions:

AND Logic implemented with Ladder Logic
AND Logic implemented with Ladder Logic as serial connection.

One advantage of ladder logic is that you can build these relationships graphically. Follow a network from left to right and look at its lines. The way the lines connect the instructions shows the logical relationships between them.

OR always has two possible paths, so it has two lines. AND has a single path through the instructions and therefore one line.

NOT Logic Gate with Ladder Logic

We still need to express one relationship: the stop button must be unpressed for the motor to remain active. That requires NOT logic.

You can implement NOT logic, or negation, with a negated contact in the ladder logic or a negated physical contact. These are also called 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 explained in part 1 of this ladder logic tutorial, I will avoid using an examine if open PLC instruction for the stop button for safety reasons. Instead, I will use a normally closed contact as stop button to negate the logic. In other words, the physical normally closed contact provides the NOT relationship required for the stop button.

With these relationships in place, the ladder logic looks like this:

Interlocking with Ladder Logic

This ladder logic controls one motor. Our example has two, so the full program looks like this:

The remaining step is interlocking. We can express that as a logical relationship too.

Each motor needs one more condition to interlock it with the other motor. That condition must be negated: the motor must remain inactive while the other motor is active. For motor 1 to run, motor 2 must be inactive. The same relationship applies in reverse to the other motor:

START1 OR MOTOR1 AND NOT STOP1 AND NOT MOTOR2

Motor 1 (active)

START2 OR MOTOR2 AND NOT STOP2 AND NOT MOTOR1

Motor 2 (active)

The added condition has an AND relationship with the existing logic. We can implement it by connecting a negated contact in series:

This example shows how to build a ladder logic PLC program by breaking a problem into logical conditions. These relationships work with more than normally open and normally closed instructions. You can also use them with many other PLC instructions, including counters and timers.

Try applying the same approach in the ladder logic exercise below. You can work out a solution in ladder logic software, or sketch it on paper. Read the task, break it down into conditions and use the logical relationships covered in the tutorial to build your solution.


Ladder Logic Exercise 1: Interlocking

Function: Start / stop control for 3 motors, with only 2 allowed to run at once. If motor 2 and motor 3 are running, for example, motor 1 must not start.

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 the first exercise has helped you practise building this logic.

The exercise also raises a problem that brings us to the next instructions. It comes from the buttons connected to the inputs, rather than from the ladder logic itself.

One Shots for Signal Edges

Normally, you expect the operator to press each start or stop button once and release it after a short time. The operator might hold a button down for longer, or a pressed button might become stuck. This is a typical hardware problem that will affect our PLC program.

A stuck stop button prevents its motor from starting. This is a common problem when PLC inputs use momentary push buttons.

Ladder logic has instructions to address this problem, called one shots.

Rising Edge Detection

The next instruction is rising edge detection, also called R_TRIG or positive edge detection. Allen Bradley uses the name one-shot rising or OSR. This instruction responds at the moment a digital signal changes from 0 to 1. Detecting that change lets you control when the instruction responds.

The image below shows positive edges in a digital signal:

Positive Edges of a Digital Signal

The instruction responds to the edge, or change in signal state, regardless of the length of the signal. This makes rising edge detection well suited to the start and stop buttons in the previous example. The length of time a button stays pressed makes no difference to the edge.

Positive Edge Contact and Coil

You have already seen the normally open, or examine if closed, instruction. It evaluates the current state of a bit. If the bit has a state of 1 or true, the instruction returns a result of 1 or true as well.

The positive edge sensing contact uses the examine if closed symbol with a P added in the space:

Positive Transition-Sensing Contact

The positive edge instruction behaves differently. It evaluates a change in a bit’s state instead of evaluating the state itself. The change it detects is positive: specifically, the transition from 0 to 1.

Because the instruction detects a change, it evaluates to true for only one scan time. Consider a bit whose state is 0 during a scan. If its state is 1 in the next scan, the instruction detects that change and evaluates to true.

On the scan after that, the bit will probably still have a state of 1. Its state has not changed between scans, so the instruction evaluates to 0 or false this time.

That single response is why these instructions are called one shots. They evaluate to 1 or true only in the scan when the state changes. The animation below shows the process slowly, so you can follow what happens from one scan to the next:

Positive Edge Detection in Ladder Logic during PLC Scan Time

The instruction produces a pulse only during the scan in which the positive transition occurs.

Positive edge detection is also available as a coil instruction:

Positive Transition-Sensing Coil
Positive Transition-Sensing Coil

The behaviour is the same, but the instruction is a coil.

R_TRIG Function Block

You can also detect a positive or rising edge with the standard PLC function block R_TRIG. I have already described it in my article about function block diagram, so I will leave the detailed explanation there. It works in much the same way as the positive edge contact.

Falling Edge Detection

There are instructions for negative edges too. Their names include negative edge detection, falling edge detection and F_TRIG. Allen Bradley uses one-shot falling or OSF instruction.

Each of these instructions detects the same event: a signal changing from 1 to 0. The illustration below marks negative, or falling, edges so you can see where they occur in a digital signal:

Negative Edges of a Digital Signal

You can use falling edge detection to detect when something switches off.

Negative Edge Contact and Coil

You can use either a contact instruction or a coil instruction for negative edge detection. Both versions have the name one-shot because they are active only during the scan when the transition from 1 to 0 occurs.

Negative Transition-Sensing Contact
Negative Transition-Sensing Contact

Choose the contact or coil to suit the place in your ladder logic where you need to detect the negative edge.

Negative Transition-Sensing Coil

One use for this instruction is starting something when another thing stops. Consider a heating element and two fans. While the heating element runs, one fan must run with it. As soon as the heating element switches off, the second fan must start so that the element cools faster.

Try solving this example in ladder logic exercise 3, using negative edge detection to start the second fan.

F_TRIG Function Block

The F_TRIG function block also detects negative or falling edges. It works much like the negative edge contact. You can read more about F_TRIG here.

The next exercises give you a chance to use rising and falling edge detection. In my view, solving problems is the best way to learn how the instructions work. Before starting those exercises, though, there is one more detail you need to know.

A one-shot instruction is active for only one scan. If you use one to start something that must keep running for longer, you need a latch or set / reset to hold it on. Without that, you risk activating the output for only a single scan.

Begin by practising rising edge detection in this exercise:


Ladder Logic Exercise 2: Rising Edge

Function: Use a start button and a stop button to control a motor. Each button must start or stop the motor only 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

The next exercise uses falling edge detection:


Ladder Logic Exercise 3: Falling Edge

Function: Use a start button and stop button to turn a heating element and a fan on and off. When the heating element switches off, start the second fan. Stop the second fan as soon as the heating element and first fan switch 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 practise using one-shot instructions to prevent an action from happening more than once. You can use them to check your understanding of rising and falling edge detection.

There is one final exercise for this second part of the ladder logic tutorial. You have been learning to build logic from conditions. Now try going from the logic expression below to a ladder logic program for the specified outputs:


Ladder Logic Exercise 4: Logic

Function: Build the following logic for a valve output 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

Keep practising logic by solving more problems with ladder logic. Work from the description of a problem to the conditions it needs, then build the corresponding logic. Practising that process is how you become comfortable creating ladder logic solutions of your own.

Share this tutorial or leave a comment below to let me know what you think.

19 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?

  • Frida
    September 5, 2019 at 11:10 am

    Hi, really like this site and your tutorials.

    I am wondering about the problem described in Ladder logic exercise 2: rising edge. How can you use a stop button to stop a motor when affected by a rising edge. As you have described earlier the stop button should be an examine if closed, normally closed input. This seems confusing. I have managed to get the motor to start and keep running using a start button with rising edge.

    I have tried to solve the problem with the positive rising edge stop button in OpenPLC but have not managed to get it working correctly.

  • Muhammad Waqas
    March 27, 2020 at 6:30 pm

    I have studied your tutorials about PLC and found them very helpful to those who are interested in PLC programming, great job done by you.

Leave a Response