What is Ladder Logic?
PLCs have exploded in the controls market and are used throughout the world. Over time they have advanced to become more user friendly, efficient, smaller and less expensive. Different types of programming languages have also been developed for PLCs but the most frequently used is still Ladder Logic.
The Origins of Ladder Logic – Relay Logic
Imagine for a second it’s 1980. You’re cruising in your brand new Ford Pinto on the way to your job at the local Rubik’s Cube plant. You have a busy day ahead since the plant is being redesigned for the new Rubik’s Revenge model due out next year. The relay panels you work on need to be rewired to accommodate the change in production size, from the original 3x3x3 size to the new 4x4x4 model.
These relay panels consist of numerous electromechanical relays that are wired together to perform a certain function in the plant. The simple opening and closing of relay contacts on the panel gives the system the ON/OFF control it needs in the manufacturing process. For instance, when the cube’s mold is in position a switch will close. This switch energizes a relay coil, which in turn closes the normally open contact for the injection pump. The pump fills the mold with melted plastic and the cube begins to take shape.
Using this combination of switches, relays, coils and contacts is referred to as Relay Logic. Relay logic is a dependable controls method still in limited use today. But the cost associated with it in terms of time-consuming logic changes, mechanical failures over time and extensive wiring and space requirements has forced many industries to reconsider their control needs. What they discovered was the PLC.
The Structure of Ladder Logic
The structure behind ladder logic is based on the electrical ladder diagrams that were used with relay logic. These diagrams documented how connections between devices were made on relay panels; they are called “ladder” diagrams because they are constructed in a way that resembles a ladder with two vertical rails and rungs between them. The positive power rail (on the left) flows to the negative power rail (on the right) through the physical devices connected on the rung. The example below shows a ladder diagram with pushbuttons (PB), control relays (CR), a motor (M) and a light (L).
Similarities with Ladder Diagrams
Ladder logic was designed to have the same look and feel as electrical ladder diagrams, but with ladder logic, the physical contacts and coils are replaced with memory bits. Let’s take a look.
For this program, the relay logic’s ladder diagram is duplicated with ladder logic; no more hard-wired logic, but memory locations instead. Some of these memory locations are used internally and others are used with external inputs and outputs. To monitor and control real world devices, they will need to be wired to I/O modules.
For this particular PLC, these inputs and outputs are assigned to X and Y memory addresses like the X001 seen with PB1. This normally open contact’s state is read from the input on the I/O module where the physical pushbutton is connected. On the other hand, each Y bit will have an output device wired to it as seen with the light controlled by Y001. All of the other locations are assigned to internal bits that we can use as needed.
One side note, today’s PLC CPUs offer many types of functions, not just simple contacts and coils. Math, Shift Registers, Drum Sequencers, etc., are available to aid in programming.
Execution of Ladder Logic
Typically before starting to execute the logic, the CPU reads the physical inputs tied to the I/O modules to update their status in the CPU’s memory table. Then, starting at the top left of the program, the CPU works its way down the rail executing each rung or sub rung from left to right. So if PB1 is pressed, the CPU will turn ON CR1. Since CR1 has changed states, in rung 3 the CPU will activate CR3. CR3’s normally-closed state is used in rung 4, so the CPU will then turn OFF L1.
Even though we still refer to coils and contacts in ladder logic, remember that they are memory representations, not actual devices. Once the CPU reaches the last rung it will update the real world outputs, then loop back and run it all again. This process will continue as long as the CPU is powered and in the RUN mode.
The time it takes the CPU to execute one pass and loop back to the beginning is known as scan time. Scan time can be important to applications where timing is critical. Subroutines and special purpose I/O modules can be used to help reduce the scan time if needed.
The Logic Behind The Ladder
So what logic can ladder logic actually perform? With the increasing demand for functionality and ease of use, many of today’s PLCs incorporate function blocks with ladder logic. The structure of the program is still ladder with the more complex instructions being function blocks. So to answer the question, let’s look at a few examples:
- 1. Boolean Logic: The ON/OFF, TRUE/FALSE algebra of binary systems, the basics of which are AND, OR and NOT operators. To put it simply, rung 5 in our code needs CR1(C1) AND CR2(C2) to turn ON motor M1 (Y002).
- 2. Timing: Timer instructions are available to allow for on-delayed or off-delayed events. Once triggered, the timer will turn its associated output ON (on-delay) or OFF (off-delay) after the set time has elapsed.
- 3. Counting: Count-up and count-down functions increase or decrease the counter value on every transition of the input.
- 4. Comparisons: Compare instructions are available to determine if values are less than, equal to or greater than each other.
- 5. Math: These instructions not only allow for the simple addition and subtraction but also for more complex operations like tangents, square roots, etc.
- 6. Special functions: These include PID loops, communication instructions, shift registers, drum sequencers, ramp generators, etc.
Try a Ladder Logic Exercise
If you would like to try an exercise in ladder logic, AutomationDirect has created a beginner’s programming exercise. This exercise was actually created for the Boy Scouts of America to help teach ladder logic to future PLC programmers. It uses the simulator included with the Do-more Designer programming software for our Do-more PLC series.
The software is free and you do not need any hardware, so try it. Download the software here and watch the video below.
Learn More about Ladder Logic
There is a lot more to learn about Ladder Logic; we’ve only begun to scratch the surface. See how we can help you Learn More about PLCs and get PLC Training!