PLC Ladder Logic Programming Tutorial (Basics)

A PLC programming language I rate among the best visual languages is ladder logic, also called ladder diagram (LD).

Ladder logic is much more visual than most programming languages. That visual approach often makes the language easier for people to learn.

Ladder logic looks similar to electrical relay circuits. If you already have some experience with relay control and electrical circuits, that familiarity will likely help you learn it faster.

You don’t need that background to begin. When I first learned ladder logic, I didn’t understand relays either.

This tutorial covers what you need to get started with the ladder diagram PLC programming language. You’ll learn to begin writing real ladder logic programs in almost any PLC programming software.

Once you have finished this tutorial, I recommend continuing with part 2 of the course. To build your understanding further, you can also choose from the online PLC programming courses.

Let’s get started!

GO TO PART 2 OF LADDER LOGIC TUTORIAL ->

Ladder Logic PLC Programming Tutorial

What is Ladder Logic?

Ladder logic, also called ladder diagram or LD, is a language for programming a PLC (Programmable Logic Controller). This graphical PLC language uses symbols to express logic operations. Its logic is arranged in rungs that look like a ladder, which gives it the name ‘Ladder Logic’.

Although ladder logic is mainly used for bit logic operations, you can also use it to scale a PLC analog input. Simple bit logic remains useful in more advanced PLC programs and in SCADA system programming.

PLCOpen sets the standards for ladder logic. The language follows the IEC 61131-3 standard, giving it a globally recognized framework that makes it consistent across implementations.

Introduction to Ladder Logic

Understanding why ladder logic was invented makes the language easier to learn. Before you start programming, it helps to look at that background, particularly if you already know electrical circuits, relays or some boolean logic.

Invented for Technicians

In this graphical language, you program by combining graphic elements instead of writing text. Those elements are called symbols.

The ladder logic symbols resemble electrical symbols by design. The language was created for technicians and electricians, along with others from electrical backgrounds who were used to reading diagrams and schematics.

Look over the symbols to see which ones you recognize.

Like electrical diagrams, ladder logic uses symbols for contacts and relays, which it calls coils. Their appearance can differ slightly from the symbols in electrical schematics, but their functions are almost the same.

For technicians who work with PLCs regularly, or trainees who will do so, I recommend the Technician’s Guide to Programmable Controllers. It’s a book I like for building your understanding of PLCs and the knowledge you bring to the job.

Sale
Technician's Guide to Programmable Controllers
  • Programmable, Controllers, Technician, Guid
  • Borden, Terry (Author)
  • English (Publication Language)
  • 416 Pages - 01/27/2012 (Publication Date) - Cengage Learning (Publisher)
How to Read Ladder Logic

The drawing direction also differs. Electrical schematics often run horizontally, while ladder logic diagrams are drawn vertically.

Here are my reasons for drawing ladder logic vertically rather than horizontally:

1. Easier to read

A vertical diagram follows the way you read: from left to right, then down to the next line. The same eye movement helps you follow the ladder logic. This reading advantage applies to people in countries where text runs from left to right; that is the reading direction behind this explanation.

2. Drawn on computer

On a computer, you draw ladder logic one line at a time. These lines, called rungs, stack into a ladder shape as you add them. For a large diagram with many rungs, scrolling vertically is the easiest way to view the whole program. Each added rung forms another line of the program on your screen. As the diagram grows, you can move down through it instead of trying to view all the lines at once.

3. Order of execution

Drawing vertically also shows the order in which the PLC executes the instructions. The PLC starts at the top of the ladder logic and works downward. The position of each instruction therefore tells you when it will run in relation to the others. You can use that top-to-bottom arrangement to follow the sequence in which the PLC will execute each part of the program.

Relay Ladder Logic

A ladder diagram can resemble an electrical schematic drawn vertically. Many people learn by building it that way. I recommend a different approach, though, because the resemblance can hide differences in how the two systems work.

The approach below uses instructions to explain ladder logic.

A PLC and an electrical control system work differently. Compare how they handle their lines:

  • The PLC takes one ladder logic line (rung) and executes that and then goes to the next line
  • In electrical systems, multiple lines (current pathways) can be executed (activated) at the same time

These differences affect how the program runs, so keep them in view as you start learning ladder logic.

Ladder Logic Basics

The first thing you will see when you create a new piece of ladder logic is two vertical lines. It is in between these two lines your ladder logic goes. When you draw ladder logic, you will draw vertical connections between these two lines. Each of those is called a rung. Just like on a physical ladder.

Ladder Logic with Horizontal Lines called Rungs

Ladder Logic with Horizontal Lines called Rungs

You place ladder logic symbols on the rungs to build the logic you need. I have numbered each rung above to show how the PLC hardware executes the program. This relates to the PLC scan time, or scan cycle. Broadly, the PLC scans all its inputs first, then executes the program to set its outputs.

How does the PLC work through the ladder logic itself?

One rung at a time.

Each rung is processed separately, and the PLC handles its symbols one by one before moving to the next rung. Understanding this sequence helps you follow how the ladder logic operates.

Ladder Logic Programming with Instructions

Every ladder logic symbol is an instruction. If that seems confusing at first, the examples below will help you work through it. We’ll begin with a simple example using the first two ladder logic symbols. Following the example shows how those instructions work together in a program.

So what are these instructions or symbols?

These symbols are logic instructions. Combining them creates the logic that makes up your PLC program. The example below contains two instructions, each shown by its own symbol.

Two Instructions in One Ladder Logic Rung

Two Instructions in One Ladder Logic Rung

My video tutorial shows the basic PLC instructions in action. I recommend finishing the written tutorial too, because the video covers only the basics.

Examine if Closed

The symbol below is the first instruction, examine if closed:

Examine if Closed Instruction

Examine if Closed Instruction

You use this conditional instruction to test whether something is true, such as whether a bit is on.

Above the instruction symbol, you’ll see the name I0.0.

That name is the address of the bit the instruction examines. Here it is a digital input, although it could instead be an internal memory bit or an output.

The other name for examine if closed is normally open. Its operation resembles a normally open contact in an electrical circuit. The physical contact’s condition, however, is whether it is activated, such as by a finger pressing a button. It does not use a memory bit as its condition.

You must assign a PLC address to each instruction.

Inputs and outputs also have bits in PLC memory. Here the examine if closed instruction uses memory address I0.0 as its condition. That address belongs to the PLC’s first input.

Here’s how that works:

  • When the PLC scan cycle starts, the PLC will check the states of all its inputs.
  • It will then write in memory the boolean value for these states (0 or 1).
  • If an input is LOW the bit will be set to 0.
  • And if the input is HIGH the memory bit will be set to 1.

Output Coil

The instruction also has a place in PLC memory where its result is stored. The next instruction shows what the PLC does with that result:

Ladder Logic Output Coil

Ladder Logic Output Coil

An output coil turns a bit on or off.

The output coil symbol sits on the right of the rung. Instructions before it on that rung determine its condition. In this example, the condition comes from the result of the examination if closed instruction.

Follow the possible results of the instruction to see how it works:

  1. PLC scan | Inputs -> I0 byte
  2. Program runs | I0.0 -> XiC result
Ladder Logic Inputs and Outputs with PLC scan

How ladder logic instructions work with the PLC scan cycle.

The animation first shows the PLC scanning its inputs and saving their states in a memory byte. A memory byte consists of 8 adjacent bits. You don’t need the details yet, but arranging the bits next to one another is useful. I’ll explain that later. For now, focus on the input states being saved before the instructions run.

Once the input states have been saved, the PLC begins running the program. Examination if closed, or normally open, executes first. Its result matches the state of the memory bit it examines.

The name normally open describes the instruction’s normal state: with the memory bit at 0, the contact is open and the result is 0. With the bit at 1, the contact closes and gives a result of 1.

At last, let’s look at the output rung:

  1. XiC result -> Output coil
  2. Output coil -> Output byte

If the animation does not display properly, try the YouTube video below. That version usually works well:

The output coil takes the previous instruction’s result as its condition. This is the RLO (Result of Logic Operation), stored in a special area of PLC memory. On Siemens S7 PLCs, that area is called the status word.

In PLC terminology, a word contains 16 adjacent bits, or 2 bytes.

The output coil sets its bit to match the value of its condition, the RLO.

Each digital output has an assigned bit in PLC memory. Here the output byte is Q0, containing bits Q0.0 – Q0.7. The result from the output coil goes into memory bit Q0.0.

After running the full program, the PLC sets its outputs. Each physical output takes the state stored in its corresponding output bit.

If you overlook the scan cycle when writing ladder logic, your program may behave unexpectedly. The next example shows why the sequence matters and introduces 3 more ladder logic instructions.

Output Latch

The previous example read a digital input and set a digital output to match it. Suppose that input is a momentary pushbutton. A spring inside the button means it stays active only while you press it.

That program keeps the output active only while the input is active. You have to keep your finger on the button to maintain the output. If it controls a ventilation fan, holding the button down all the time would be inconvenient for the operator. We therefore need the output to stay active after the operator releases the pushbutton. The program must therefore keep that state without needing the operator’s finger on the button.

Ladder logic gives us two ways to keep it active:

Output latch in ladder logic

Output latch in ladder logic

You may recognize this arrangement from electrical schematics. It is called a latch or self-hold.

In a self-hold, the coil keeps itself active. Follow the scan cycles to see how:

With the button pressed, the first run of this program activates the output, just as before. The difference appears on the second or third run. A momentary button is active only briefly. Depending on the PLC’s program execution time, the button may already be released by the second, third or fourth run. The output must behave differently from the button if the fan is to keep running after the operator lets go.

Now consider the first scan cycle after you release the button.

The output remains active from the last scan cycle, when the pushbutton was pressed. The PLC reads the inputs again and saves them in the memory byte. In memory bit I0.0, it now saves a “0”. The first examine if closed instruction uses I0.0 as its condition, so its result is false, or “0”.

The parallel examine if closed instruction uses the output memory bit as its condition. Because the output is still active, that instruction evaluates as true, or “1”. Keeping the output bit at “1” therefore keeps the output active: the output provides its own condition. The parallel path therefore keeps the coil active after the pushbutton path has become false.

Placing the self-holding instruction in parallel creates an OR condition, which I’ll explain later. For now, either I0.0 OR Q0.0 must be true to activate the output.

Examine if Open

Well, congratulations!

You have built a working ladder PLC program in which a pushbutton activates an output. In our example, that output connects to a contactor supplying a fan. Once activated, the output holds itself.

There is still a practical problem: the program needs a way to stop the fan.

A stop button is the simplest way to turn the output off again. Connect it to the second input, whose memory address is I0.1.

We need to choose the instruction for that stop button.

Its position in the ladder logic also matters.

For the instruction choice, consider examine if open:

The examine if open instruction uses the symbol shown below:

Examine if Open Instruction

Examine if Open Instruction

Examine if open inverts its condition, the opposite of examine if closed. For condition “0”, the result is “1”. With “1” as the condition, it returns “0”.

That is the behavior we need for the stop button here. To turn the output coil off, we need to give it a condition of “0”.

Next, consider where the instruction belongs.

Connect it in series after the self-holding instruction. If it were placed elsewhere, the latch would still supply a “1” condition to the output coil when you pressed stop.

Now, we end up with this ladder logic:

Output latch with XIO to break the latch

Output latch with XIO to break the latch

The instruction inverts the output coil’s condition and breaks the latch. You must press the start button again to reactivate it.

That example uses examine if open for the stop button.

This is not good practice!

For that instruction to stop the output when pressed, the physical button must have a normally open contact. My article on normally closed contact for stop buttons explains why this is a problem. A normally closed contact ensures the system stops if a wire to the button breaks.

Applying that practice gives us the following ladder logic:

Output latch with stop

Ladder logic output latch with stop.

The ladder still works the same way after the instruction changes because we also change how the physical stop button works.

You can now set an output and hold it until someone presses stop. Other approaches are possible too; latching is one way to do it.

Building Logic with Ladder

You’ve made it through the ladder logic examples. Whether you’re committed to learning them or simply had time to spare, you’ve reached the next step.

If you’re learning ladder diagram for the first time, don’t worry about finding some of these ideas confusing.

As you see the concepts again and practice using them, they become easier to recognize and understand.

Where can you continue learning about ladder logic and PLCs?

Well, you’ve got three great options:

  1. Enrol in an Online PLC Training Course
  2. Learn from a book on PLC programming
  3. Check out part 2 of this ladder logic tutorial

If you plan to work with PLCs in your career, I recommend using all three options.

Learning & experience help you take responsibility for coding and troubleshooting PLCs at work. The more you understand them, the more you can do yourself.

Courses and books teach you about PLCs and ladder logic so you can apply ladder diagrams at work or in exams with more confidence. Reading the explanations and learning from a course both help you build that knowledge before you apply it yourself.

For PLC technicians and those training for the job, I recommend the Technician’s Guide to Programmable Controllers.

Sale
Technician's Guide to Programmable Controllers
  • Programmable, Controllers, Technician, Guid
  • Borden, Terry (Author)
  • English (Publication Language)
  • 416 Pages - 01/27/2012 (Publication Date) - Cengage Learning (Publisher)

For hobbyists and students, my recommendation is Introduction to PLCs by Jay Hooper.

If you work just with RSLogix 5000 / Studio 5000 (Allen Bradley PLCs), I recommend Nathan Clark’s book on PLC Programming Using RSLogix 5000.

I’ve also written a part 2 to continue this PLC programming tutorial.

Part 2 shows you how to build real logic solutions with ladder logic. It covers implementing logic gates and detecting the rising and falling edges of a digital signal.

GO TO PART 2 OF LADDER LOGIC TUTORIAL ->

54 Comments
Leave a response

Leave a Response