Ladder logic programming is like virtual circuit building
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 from Beijing to Johannesburg and every where 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 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 programing using Allen Bradley software and suddenly find the need to program a Siemens PLC you already know the most important part, how to program.
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.
The following rungs demonstrates the rung output when the XIC input instruction changes states:
Bit instructions are the most commonly used instructions when programming ladder logic.
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.
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.
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.
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.
Bit instructions can be used to create more complex logical functions such as logic gates. Using a truth table is helpful for evaluating and programing logical functions.
And-logic refers to a group of arguments that must be true in order for the rung output to be true.Example: Arugment_1 and Argument_2 and Argument_3 must be true. And logic refers to arguments in series (in line).
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.
- 0 0 = 1
- 1 0 = 0
- 0 1 = 0
- 0 0 = 0
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).
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.
- 0 0 = 0
- 1 0 = 1
- 0 1 = 1
- 1 1 = 1
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).
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.
- 0 0 = 1
- 1 0 = 0
- 0 1 = 0
- 1 1 = 0
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.
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.
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:
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.
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.
Example Ladder Logic Programming Tutorial
You can find more video programming tutorials here.
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.
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.
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.