A Simple (But Complete) Guide : PLC Ladder Logic Programming(Basics)

Imagine drawing an electrical circuit on your computer then downloading it into a device that would transform your virtual circuit into a real electrical circuit. Welcome to the world of industrial automation where the above scenario is played out daily and everywhere in-between.

The device mentioned above is known as a Programmable Logic Controller or PLC for short and the software that enables this virtual circuit building is known as Ladder Logic or Ladder Diagram. PLC’s together with Ladder logic has transformed the industrial automation industry.

There are several PLC programming languages with PLC ladder logic being the most popular. Before PLC’s were commonplace electromechanical devices were used for machine control.

Engineers used ladder diagrams to design control circuits for machines that utilized electromechanical relays and timers.When the PLC was introduced traditional ladder diagrams were used as a basis of developing a programming language to make the transition from designing electromechanical relay circuits to programming virtual relays as seamless as possible.

This led to the modern PLC programming languages used throughout the world today. Siemens, Allen Bradley, Omron, GE Fanuc and Modicon are some of the biggest names in PLC control.

The various manufacturers offer a host of Programmable Logic Controllers that can be programmed with a personal computer and programming software.PLC manufacturers provide their own proprietary version of ladder programming software.

With so many versions of programming software available, standard ladder logic programming software is nonexistent. Siemens PLC’s are programmed and configured with software provided by Siemens.

Allen Bradley PLC’s are programmed and configured with software provided by Allen Bradley. Neither platform is compatible with the other.

A side by side comparison of different programming software (manufacturer dependent) exposes some of the differences. Instruction sets, symbols, memory addressing, hardware configuration and the user interface are all different.

The only commonality between manufacturers is how the ladder logic executes when compiled and downloaded. If you learn ladder logic programming using Allen Bradley software and suddenly find the need to program a Siemens PLC you already know the most important part, how to program.

Ladder logic is a visual programming language used to program PLC’s (Programmable Logic Controllers).

Ladder logic consists of horizontal Rungs and Instructions embedded between vertical Rails on either side. Rungs house instructions which are referenced by tags or variables.

The rails represent the opposing polarity of power rails as shown on an electrical schematic. The rungs and subsequent instructions represent the “load” between the rails.

Rungs and rails are graphical representations of electrical schematics. The real magic behind ladder logic is the instructions, and there are a bunch of them.

There are Boolean, Math, Timer, Counter, and Specialty instructions just to name a few. All instructions reference a tag or variable which is an address to a memory location. Got all that? Good.

Ladder Logic is a graphical based industrial programming language used to program and configure Programmable Logic Controllers, or PLC’s. Ladder programs consist of rungs that house instructions. Once compiled and downloaded to a PLC the ladder program is scanned like a book; from top-to-bottom, left-to-right.

The following illustration is a common “latching circuit” used to turn devices such as motors on and off. If you are familiar with electrical schematics you will notice the relay and coil symbols.

Ladder-diagram-latching-circuit

What Makes Up a Ladder Logic Program?

Coils, relays, timers as well as other instructions can be placed on rungs. Variables store data that is checked and manipulated by instructions. How and where these instructions are placed on rungs determine the output logic of a rung. The software has a programming instruction set that covers bit instructions, timers, counters, string instructions, and file instructions just to name a few.

Ladder instructions are unique. They have built in watch windows. For example let’s take a look at the Timer On or TON instruction. Notice the three variable fields:

Ladder-diagram-Timer

Timer Name of the timer

Preset Set time like a stop watch

Accum How much time has accumulated?

Now let’s look at the –(EN)- and –(DN)- symbols on the right side of the instruction. If the timer is timing the –(EN)- is highlighted green. If the timer is done timing the –(DN)- symbol is highlighted green.

This is atypical of programming languages. Most languages require a programmer to configure a watch-window to monitor variables at run time. Having feedback directly on the instructions makes for some quick and easy troubleshooting.

Why ladder logic? I mean really, what advantage is there to programming in ladder logic?

Take a look at the picture below.On the left is Structured Text and on the right is Ladder Logic. Both programs accomplish the same thing just with different languages. Notice the green vertical rails in the ladder logic example and the green vertical bar in the structured text example. The green lets you know that the logic is enabled and running

Ladder diagram rung

Take a look at the rung on the right. Instantly you know the values of the tags are zero. How? That’s easy, because if either of them had a value of 1 they would be green just like the rails.

Now take a look at the code on the left. It’s impossible to determine the input and output tag values without bringing up a watch window and checking the value of the tags. Incidentally all values in both programs are set to 0 in this example.

The picture below shows the exact same logic, however something is different on the rung example.

The instructions are now highlighted in green the same way the rails are in the last example. Both tag values are now set to 1 as indicated by the green highlighting of the instructions

In the Structured Text program both values are 1, however the code is just that, code. The code does not provide feedback like the ladder logic example does.

It’s possible to open a watch window to see the value of the two variables, for that matter the same watch window could be opened for the ladder logic program as well. The major difference being is it’s unnecessary for the ladder program. Just a quick glance lets you know what is on and what is off

This kind of feedback is undoubtedly the major advantage of using ladder logic. Ladder logic places all the information you need where you need it. It makes troubleshooting quick and easy, and this functionality isn’t limited to just bit instructions.

Variables are displayed on instructions as well. Most ladder instructions will show the value of tags in real time. Take a look at the Equal To instruction below. If the value of Source A is equal to the value of Source B turn on the output bit. In this case they are not equal and the output bit is off.

Ladder-diagram-Example

Now imagine you have been tasked to find out why the variable “Output_Bit” is off. Just a quick glance at the ladder rung and it’s easy to determine why the bit is off.

The two variables in the comparison instruction are not equal therefore the “Output_Bit” if off. From there you can do a simple cross reference on the variables to discover why the variables are not equal.

Programming ladder logic entails dragging and dropping instructions, rungs and branches. Programming structured text entails knowing the correct syntax.

Take another look at the structured text examples above. Notice the Semicolons and Colons. This is representative of the Pascal programming language. Instructions are in Blue and tags are in Red. Setting a value is done with the := operator while comparing values is done with just the = operator

Ladder logic is scanned the same as a book is read, from top to bottom left to right.

So, if you had a program with 10 rungs the top rung would be scanned first starting with the left most instruction and proceeding to the right most instruction of the rung. All subsequent rungs would be scanned the same way, and upon completion the scan would start over at the top most rung again. I say typically because most ladder programs are scanned this way however they can be event or time driven as well.

Ladder logic is but one of five standard PLC programming languages as defined by IEC 61131-3 and are as follows.

  1. Ladder Logic
  2. Structured Text
  3. Sequential Function Chart
  4. Instruction List
  5. Function Block Diagram

The Studio 5000 platform accommodates all the standard programming languages with the exception of Instruction List in any combination. That is, it is possible for a single project to contain all of the different languages.

Making PLC Programs is Easy.

Most programs use a combination of just a few of the basic instructions. Master the following list of instructions and associated variables and you will have the basics mastered

  1. XIC Examine If Open
  2. XIO Examine If Closed
  3. OTE Output Energize
  4. OTL & OUT Output Latch & Unlatch
  5. MOV Move data into a variable
  6. COP Copy data into an array of variables

Mastering bit instructions is fundamental programming basics

Every PLC, regardless of manufacturer, makes use of bit (Boolean) instructions. Instructions can be broken down into two types, input instructions and output instructions.

Instructions require arguments or tags in order to execute logic. An instruction defines a process while a tag defines the data to be processed.

Bit instructions are predefined instructions that can be placed on rungs and used to perform Boolean evaluations, or in English: IF this THEN that statements.

When a tag is defined as a Boolean type, a single bit of memory addressable by the defined tag name is allocated; the PLC allocates an address in memory to store one bit of data.

If a tag is defined as a DINT data type 32 bits of memory addressable by the defined tag name is allocated. In other words A DINT is a 32 bit data type. See more about Atomic Data Types here.

The name of the tag is used to access the defined data. Input instructions evaluate data, while output instructions alter data.

Take a look at the following rung of logic. There are two instructions, an input instruction and an output instruction. The input instruction references data addressed by the tag name XIC.

The output instruction references data addressed by the tag name OTE. The logic is defined by the two instructions while the output of the logic is dependent on the data addressed by the input instruction.

The rung is evaluated from left to right. The input instruction evaluates data and the output instruction sets or clears data

If the data addressed by XIC is equal to 0 then the data addressed by OTE is equal to 0.

If the data addressed by XIC is equal to 1 then the data addressed by OTE is equal to 1

Ladder-diagram-Boolean-Instruction

The following rungs demonstrates the rung output when the XIC input instruction changes states:

Ladder-diagram-Boolean-Instruction_

Bit instructions are the most commonly used instructions when programming ladder logic.

XIC Examine If Closed

The XIC instruction evaluates the data addressed by the associated Boolean tag. The evaluation is true if the data is one and false if the data is zero. This is an input instruction and must be followed by an output instruction.

Ladder-diagram-Boolean-Instruction

XIO Examine if open

The XIO instruction evaluates the data addressed by the associated Boolean tag. The evaluation is true if the data is zero and false if the data is one. This is an input instruction and must be followed by an output instruction.

Ladder diagram XIO

OTE Output Energize

The OTE instruction writes data to the memory location addressed by the associated Boolean tag. The data is set to one when the preceding rung logic is true and set to zero when false. This is an output instruction that can follow an input instruction, however one is not required.

Ladder-diagram-OTE

OTL Output Latch & OTU Output Unlatch

The OTL instruction writes data to the memory location addressed by the associated Boolean tag. The data is set to one when the preceding rung logic is true

When the preceding logic transitions from true to false the data addressed by the tag retains the data. It is latched on. To clear the data the OTU instruction is used.

Ladder-diagram-Boolean-Latch

Bit Instruction for logic gates

Bit instructions can be used to create more complex logical functions such as logic gates. Using a truth table is helpful for evaluating and programming logical functions.

And Ladder Logic

And-logic refers to a group of arguments that must be true in order for the rung output to be true.

Example: Argument_1 and Argument_2 and Argument_3 must be true. And logic refers to arguments in series (in line).

Pseudo Code:

If the data addressed by Switch_1 and Switch_2 are both equal to 1 then the data addressed by Green_Light equals 1 else the data addressed by Green_Light equals 0.

Rung example:

Ladder diagram AND gate

Truth table:

OR Ladder Logic

Or-logic refers to a group of arguments where only one argument is required to be true to make the rung output true. Example: Arugment_1 or Argument_2 or Argument_3 must be true. Or logic refers to arguments in parallel (branched).

Pseudo Code:

If the data addressed by Switch_1 or Switch_2 is equal to 1 then the data addressed by Yellow_Light equals 1 else the data addressed by Yellow_Light equals 0.

Rung example:

Ladder-diagram-OR-gate

Truth Table:

Not-logic gate

Not-logic refers to a group of arguments that must be false in order for the rung output to be true.

Example: Arugment_1 and Argument_2 and Argument_3 must be false. And logic refers to arguments in series (in line).

Pseudo Code:

If the data addressed by Switch_1 and Switch_2 are both equal to 0 then the data addressed by Red_Light equals 1 else the data addressed by Red_Light equals 0.

Rung example:

Ladder diagram NOT gate

Truth Table:

Together rungs, instructions and tags make of the base elements of a program.

The easiest way to create a logical rung is to start with the output first. What do you want the rung to do? If you want the rung to turn on a light then program the rung to turn on the light. Take the following rung as an example.

This is the simplest way to program an output. Theoretically the light would be wired directly to the output addressed in the OTE instruction on the rung. The light turns on and stays on, which is exactly what is expected.

Ladder Logic PLC Programming Instruction

Ladder Logic Tutorial

Below is a short tutorial that covers some basic ladder logic programming fundamentals.

A food processing plant in Oregon has a tank that needs to be filled automatically when the tank gets low on slurry. A pump delivers the slurry into the top of the tank. The pump is currently manually operated. Write a program that will automatically fill the tank.

Ladder Logic Basic Program layout

When the tank is low on slurry a Low_Sensor signal will transition to high and the pump will turn on. When the tank is full a High_Sensor signal will transition to high and the pump will turn off.

From the paragraph above one can derive that the conveyor needs to turn on and off. The simplest way to fill the tank is to turn the pump on. Here is what I came up with

Ladder-diagram-Example

When the rung is scanned the pump will turn on. No, this is not a complete program; however the hardest part is done, turning the pump on. Get into the habit of writing programs this way, starting with the output first. Now it’s time to add some conditional logic to start and stop the pump.

From the description above one can derive that the pump should turn on when the Low_Sensor signal transitions to high. As you can see below the Low_Sensor signal has been added in the form of an XIC instruction. When the slurry level dips below the sensor the signal transitions to high and the motor is turned on.

Ladder-diagram-Example

As the level of the tank rises the Low_Sensor detects this and the signal transitions to off. What will this do to the motor? According to the rung logic the motor will turn off with the sensor, as seen in the rung below.

Something is needed that will keep the motor running when the slurry covers the sensor an the Low_Sensor signal transitions from high to low. Take a look at the rung below. Once the Pump_Motor is turned on the XIC instruction referencing the Pump_Motor output on the branch circuit will keep the motor running.

The circuit is just about complete. The last part of the circuit will turn the motor off when the High_Sensor signal transitions from low to high.

As you can see in the below rung an XIO instruction referenced to the High_Sensor signal causes the rung output logic to become false, turning off the Pump_Motor.

Below is the state of the rung when the tank is full. No path to the OTE instruction referenced to the Pump_Motor exists, which gives a rung output of false.

As the level of the tank lowers the High_Sensor signal transitions back to low allowing the motor to turn on when the tank is low enough that the Low_Sensor signal transitions back to high.

The circuit, or program is complete. Let’s walk through the process to see what is going on step by step.

The Low_Sensor signal goes high indicating the tank is empty. The High_Sensor signal remains low indicating the tank is not full. The conditional rung logic is true. The OTE instruction changes the data addressed by the tag Pump_Motor to high. Notice that the Pump_Motor tag is used on an OTE instruction and an XIC instruction.

As the pump fills the tank the Low_Sensor turned off indicating the tank was no longer empty, yet still not full. The XIC instruction addressed to the Pump_Motor placed in parallel with the Low_Sensor input creates a path to keep the Pump_Motor high, which keeps the motor running.

When the tank gets full enough to trigger the High_Sensor the path to the OTE instruction referencing the Pump_Motor is removed and the motor is stopped. As the tank empties, the Low_Sensor is off and the High_Sensor is on indicating the tank is not full and not empty. When the tank empties the Low_Sensor will trigger another cycle of filling the hopper, back to Step-1.

How to read ladder logic ?(Routine Scan characteristics)

Ladder logic routines are scanned from top-to-bottom left-to-right. The first rung in a routine is scanned then the next and so on until the entire routine is scanned. Rungs are also scanned from top-to-bottom,left-to-right.

This is demonstrated in the latch rung from the tank fill program. The top left most instructions are scanned before the instructions to the right. The scan flow never goes from right-to-left or bottom-to-top.

A rung or a block of rungs can be skipped using the JMP to LBL instructions, or with a RET instruction. When used conditionally the JMP instruction can be used to skip blocks of code to save scan time.

As an example a simple Boolean expression could be evaluated to determine if a more complex scan intensive block of code should be executed or skipped.

How to read ladder logic

Project Organization, Tasks, Programs & Routines

Most ladder logic programs are managed from a continuous task. A continuous task is one that runs from start to finish over and over again. The tank fill program is an example of a program that runs continuously. Instructions and rungs are placed inside routines. Routines are a way of organizing code logically, they can also be used to pass parameters or run conditional logic. A program can contain one routine or thousands (the amount of routines is limited by the PLC; size, memory, etc.).

Programs consist of routines filled with instructions and tags. Tasks are used to call programs. Tasks can be continuous, event driven or periodic. A PLC may have multiple tasks containing multiple programs.

PLC-Project-Organization-TasksPrograms-Routines

The Continuous Scan Cycle

Step-1 Inputs are scanned and the input file is updated.

Step-2 Routines are scanned and the logic is executed referencing the input file.

Step-3 Program overhead including communications, diagnostics and maintenance are performed.

Step-4 Outputs are updated according to the logic executed above. The process starts again.

Scan-Cycle

Online PLC Training For Dummies

This site is dedicated to teaching the novice, ladder logic for dummies, how to program. How do we accomplish that? We offer online training. Put your credit card away most of the material you will find here is completely free. Free doesn’t mean you won’t learn, quite the contrary. Take a look around the site and I think you will agree there is a bunch of useful material. Check out the main page and be sure to watch the tutorial you will learn more from this than a month of lectures.