How do you program a PLC? PLC Programming starts by identifying the problem, creating a sequence of operations based on binary logic, entering a program using a language, and simulating the program in your software.
This guide assumes that, because you know about the existence of Programmable Logic Controllers, you already know the most basic electrical principles that govern the actual PLC operations. The knowledge of Input Devices helps to imagine a multitude of solutions to a common problem, as well as knowing the different Output Devices that are controlled by the PLC to solve the problem in the first place.
- 1 Identifying the Problem
- 2 Fundamentals of Logic
- 3 IEC61131-3 Standard: PLC programming languages
- 3.1 What is a PLC programming language?
- 3.2 Textural Programming Languages
- 3.3 Visual Programming Languages
- 3.4 Ladder Logic Programming
- 4 Different PLC Relays
- 4.1 Internal Relays
- 4.2 Battery-Backed Relays
- 4.3 Set and Reset
- 4.4 Applications of Internal Relays
- 4.5 Equivalent Ladder Diagrams for Logical Functions
- 5 Before you start: PLC Safety Systems
- 6 Conclusion
Identifying the Problem
Before you start programming your PLC, or in fact any controller, you must begin with the end in mind. This means that the final outcomes must be determined clearly along with the conditions that determine the outcome.
Also, the input and output devices must be clearly identified.
For instance, you want a control system for your water tank. You want the tank to fill up until it is full and then automatically refill one the water level is near empty just to maintain the pressure. What do you do?
Because you have clearly identified what outcomes you want along with the conditions, you now have to think the appropriate types of sensors and output devices that you must use.
In this case, because there are water levels involved, you must use level sensors: One for the Full level and one for the Near Empty level. Lastly, you would want to control an input valve to regulate the flow of water into your tank.
I found this helpful video that actually shows a control problem to solve:
The next step is actually designing a program for your controller to execute the steps required to perform your automated control system.
But first, you have to know some logical fundamentals that will allow your system to create decisions on its own.
Fundamentals of Logic
PLC Programming was based on Relay Logic, and Relay Logic was based on—well, the fundamentals of logic. Logic circuits are digital, so they produce outputs that are discrete in nature. This means that it’s either “True or False”, “High or Low”, “Yes or No”, “0 or 1”. Nothing in between. This simplifies input-output relationships as compared to the analog side of things.
In analog, we consider an INFINITE number of values. Instead of 0 or 1 only, we have values in between. The “Maybe” in between the “Yes or No”, or the “Medium” in between “High or Low”.
The result may also vary by the tiniest amount and it is already considered a different output. Therefore, it is much simpler to use the Binary concept in Number Systems to represent the outputs.
What is Binary?
Binary is part of the M-ary system of representation when M is the total number of results. Confused? For a single digit, BI (two)-nary means two possible outputs. Quarter(four)nary means four possible outputs. The Decimal number system—which we are familiar with, has 10 (prefix Deci-) representations: 0, 1, 2, 3, up to 9.
Because you are an intelligent human being, you may have guessed that more digits can represent more values. In decimal, you use 10 if you have exceeded counting through all representations (0 to 9) once. You use 20 if you have exceeded twice, and so on.
Binary numbers are quite the same. 0, 1, 10, 11, 100, 101, 110, 111, 1000—you see the pattern? One digit is added to the mix every time you have “used up” all the possible representations.
How is this important in PLC programming? Well, because as I have mentioned earlier, PLC programs were based on Relay Logic which operates on Binary Logic. It’s either ON or OFF for them.
Using the proper sequence and arrangement of the Relays, the early programmers were able to set conditions for switching their output devices. The way they did this is by using the binary logical operations: NOT, AND, OR, XOR, NAND, NOR, and XNOR.
Since the invention of transistors, manufacturers are now able to produce Integrated Circuits that readily perform these logical operations. Such chips are called “Logic Gates”.
Whether you have relays or logic gates, these are the fundamental logical operations that you must know.
Note: For the examples that I will discuss below, the assumption is an output of 1 “Turns the circuit ON” and an output of 0 is a “Circuit OFF condition”.
The NOT operation performs negation of input. If you have “0” as an input, performing the NOT operation will output a “1”. Let’s look at how this is performed using Relays.
Relays can either be “Normally Open (NO)” or “Normally Closed (NC)”.
Normally Open relays do not allow current to pass unless it is energized.
Normally Closed relays do not allow current to pass ONCE energized.
Hence, the Normally Closed relays served as the primitive versions of NOT gates.
As an example, if you want a motor to turn off (Output: 0) when a button is pressed (Input: 1), then you should use a Normally Closed Relay. The table below summarizes the input-output relationship. By the way, that table is called a truth table.
This is the only logical operation that requires only 1 INPUT. The logical operators that I will discuss next require 2 inputs. Basically, they now COMPARE the inputs that you provide.
The AND gate outputs a “1” whenever BOTH inputs are “1”.
In everyday language, the AND operation requires that input 1 AND input 2 is ON.
How did they do it for Relays? By placing normally open relays in series.
Connecting the normally open relays in series would not allow current to pass through unless BOTH relays are energized. Simple as that.
The AND operation is commonly used in drilling machines where, for safety reasons, the operator must use two hands to turn on the drill. This decreases the possibility that the operator has his hand placed on the drilling platform while the drilling operation starts.
The OR operation outputs a “1” whenever ONE of the inputs is a “1”.
Basically, both Input 1 and Input 2 can control the switching of the circuit. In comparison with the AND operation, you can only turn on a circuit by turning BOTH switches ON.
In the AND operation, the relays were placed in series. In the OR operation, relays are connected in PARALLEL to each other.
XOR stands for “Exclusive OR”. This means that the input must be different from each other, hence the term exclusive.
Using XOR, only one input at a time can control the circuit switching.
XOR can be used as comparators. Comparators, as implied, compares two input signals and sends an output signal based on predetermined criteria or functions. In this case, the comparator outputs a HIGH signal when the inputs become different.
The NAND gate is basically the equivalent of a NOT AND gate.
The gate will turn OFF when BOTH inputs are ON. Hence, you can expect the truth table to be an inverted version of the AND truth table in terms of the output.
In practical applications, the NAND operation may be used for temperature regulation in a freezer, for example.
When a thermistor is connected to BOTH inputs of a NAND setup, it can act as a feedback sensor to turn a heat exchanger OFF.
The resistance of the thermistor decreases when it gets colder, hence decreasing the voltage drop to a low enough level to be detected as two LOW signals by the NAND operation.
The NOR gate is the equivalent of NOT OR.
The gate will turn OFF when at least 1 input is ON.
Lastly, XNOR is the equivalent of NOT XOR.
When you want to turn on a circuit when the states of both inputs are the same, XNOR is used.
This means that when both inputs are OFF or both inputs are ON, the XNOR operation turns the circuit ON.
Usually, the inverted versions of logical operations are not used in practical circuits as you can easily change inputs into a Normally Closed one instead of a Normally Open one (more on that later). But, it is still helpful to have a basic understanding of them.
In studying these logical operations, Boolean algebra is used. Boolean Algebra provides a way to simplify even the most complex series of logical operations imaginable.
What this means for us is a shorter program that performs the same operation, and that is highly desirable.
There are different laws that consist Boolean Algebra, and most of them are similar to the mathematical laws that we have studied in elementary school. The three basic rules are:
This law states that two inputs are added (OR) or multiplied (AND), the order is not important. This means that the result is the same for A+B and B+A.
Note: In A+B, when both inputs are 1, the equation turns into 1+1. The answer is equivalent to 1, not 2 because we still follow the Binary number system.
For A*B, the result is the same for B*A. Both are similar to Mathematical rules.
Hence, in PLC programming, A OR B is the same as B OR A, and A AND B is the same as B AND A.
Again, this law is just similar to the associative law in Mathematics. Either adding or multiplying three variables together in a grouped fashion does not affect the result.
To put it in simple terms, below are the equations:
( A + B ) + C = A + ( B + C )
( A * B ) * C = A * ( B * C )
This means that when the result of ( A OR B ) is OR’ed to C, the logical output is the same even if A is OR’ed to the result of (B OR C). Makes sense?
Same goes for the AND. When the result of A AND B is applied with C using the AND operation, the logical output is the same as that of A coupled with the result of B AND C.
The distributive law is critical in simplifying logical equations in Boolean Algebra. Long equations that use 3 logic operations can be simplified into 2, as evident in the equations below.
A * ( B + C ) = ( A * B ) + ( A * C )
A + (B * C ) = ( A + B ) * ( A + C )
So, when you see an equation that resembles the one on the right, it is actually more practical to use the equivalent equation on the left because that ultimately results in the same output using a shorter program.
How to convert Boolean Equation to Logic Gate (and vice versa)
Converting a Boolean Equation to a Logic Gate is simple as you only have to know the equivalent of each gate’s boolean equations.
As stated earlier, the AND gate is simply a “multiplication” and the OR gate is simply “addition”.
The NOT gate is noted as an apostrophe or a bar at the top of the variable(s).
The equation above summarizes it all, and it is read as [NOT (A AND B )] OR C.
We can further simplify this using the NAND gate, because we have learned earlier that the NAND gate is the equivalent of ‘NOT AND’.
Hence, the equation is simplified as:
(A NAND B) OR C
Now, if you wanted to take this logic gate representation back into a boolean equation, you would have to turn EVERY operation into a representation using just three operations:
This makes it easier to determine if the variables are added or multiplied or negated.
- Logic Gates by Electronics Tutorials
- Boolean Algebra by Electronics Tutorials
- Short Guide on Boolean Algebra
IEC61131-3 Standard: PLC programming languages
The IEC or the International Electrotechnical Commission is the international standards and conformity assessment body for all fields of electrotechnology, which essentially covers Programmable Logic Controllers.
The IEC has published the IEC 61131 standard for PLCs which should be followed by all users worldwide, and it has a section specifically for PLC programming languages, the IEC 61131-3.
What is a PLC programming language?
PLC programming language refers to the set of semantics or methods that allow the user to communicate information to the PLC. The IEC 61131-3 standard defined the five standard languages that are used in PLC programming.
The standard divides these 5 PLC programming languages into two categories, based on the nature of the language: Textural and Graphical/Visual.
Textural Programming Languages
Textural programming languages are those in the form of text and consists of commands that the user must all be familiar with in order to create a program.
The structured text is a high-level language developed for industrial control and is usually in C or PASCAL language. If you have a background in C programming, you will closely see its resemblance.
The structured text language are written as a series of statements terminated by semicolons (like in C). These statements are defined by the language libraries and makes it easier to set input-output relationships in the program.
Buzzer:=PushA OR ( PushB AND (NOT PushC) );
The example above directly shows the relationship between the variables for the input and the output.
Structured text, similar to the PEMDAS (Parenthesis Exponent Multiplication-Division Addition-Subtraction) principle in Mathematics, also have commands that are higher in priority.
For example, in the structured text above, all variables within the parentheses will be executed first. In that case, the most inner set of parentheses should be evaluated, and that contains “NOT PushC”.
The program negates PushC first, and then evaluating the logical value of PushB AND *the result* before evaluating it with PushA using the OR operation.
The instruction list is a more complicated one, as it uses lower level language.
In programming, lower level language means that it gets closer to machine language (the 1’s and 0’s) and the commands closely resemble that of the microprocessor’s programs. Higher level language is “closer” to the user because it is easier to understand due to the function names or graphics, for example.
Instruction lists are also considered a way of entering ladder programs, except by using text. It gives a series of instructions for each new line and uses mnemonics for each one.
Since different manufacturers use different mnemonics, I will only give the IEC61131-3 as an example.
This series of commands will create an equivalent ladder diagram as shown below:
Visual Programming Languages
PLC programming languages that are visual are also high-level languages. They are easily understood by humans because of the symbols and can already represent a more specific component of the program/system.
The function block diagram is a simple way of PLC programming where there are “Function blocks” (hence the name) are available in the programming software.
The image above shows a diagram that is interpreted as “[Pump AND Pressure] OR Test”.
Basically, when the Pump and Pressure inputs give a HIGH reading, the end output will be 1. OR it could be that only the Test input gave a HIGH reading.
Sequential Function Charts
Sequential Function charts, on the other hand, are just that–charts that represent each function in a PLC control system in a sequential fashion.
A sequential function chart is a visual representation of the system’s operation to display the sequence of actions involved in the operation.
Ladder Logic Diagram
The most commonly used PLC programming language is the Ladder Logic Diagram.
The reason for its popularity is that Relay Logic Diagrams were closely resembled by the Ladder Logic Diagrams.
When the PLC was invented, designers found a way to use the existing knowledge of the Relay Control System designers for programming the PLC.
Another reason is that the PLC programmers usually prefer to define the actions in terms of contacts, which again was the most commonly used control system before the PLC.
Since this is the most commonly used PLC programming language, this is the one that you will learn in this mini-course.
Ladder Logic Programming
PLC Programming has never been easier for the original Relay Control System designers, thanks to Ladder Logic.
Ladder Logic was derived from the Relay Logic Diagrams and hence uses almost the same context.
The only difference is that because the PLC was very flexible in terms of the Input and Output Devices, the symbols for the electrical devices are now exempted from the actual program.
For Ladder Logic Programming, Inputs are Inputs, and Outputs are Outputs. No need to memorize all the standard symbols for each separate device.
Parts of a ladder logic diagram and how it works
The ladder logic diagram consists of two fundamental parts, which you can see as the vertical and the horizontal lines. They are called, respectively, the rails and the rungs.
Now, you may have noticed in the ladder logic diagram example above, there are multiple inputs in the same rung. EXCEPT, they are not in series but rather connected in a parallel fashion.
The number on the left represents a new rung. In the example above, there are 4 rungs present. It may seem for total beginners that there should be 7 rungs, but each parallel connection only shows an OR relationship between the two inputs.
Back to the example, the two vertical rails represent the 24 Vdc source and the other one the 0 Vdc terminal. They are power rails. What this means is that for an output element (more on that later) to become powered up, you have to connect the two rails together by finding a way to connect them via your inputs.
To scan the program for continuity of rungs, the PLC uses a top to bottom, left to right sequence, thus checking continuity between the two rails for each scan.
Simply put, the devices on a rung must provide continuity from the left rail to the right rail.
In PLC Programming there are some fundamental notations that you must remember to create programs, and they are very simple. In fact, you have already seen them in the example above but this time, you are going to learn what those actually mean.
Normally open and Normally Closed Inputs
Two vertical bars represent an input device. It could be any input device connected to a PLC hence the symbol does not change for ALL PLC devices as it is the standard set by the IEC 61131.
There are two types of input device notations: one is the Normally Open (NO) inputs, and one is the Normally Closed (NC) inputs. They are classified that way because the Relay Logic has used the same principle in the Relay Logic Diagrams. In fact, this was inspired by electromechanical relays because they, too, can also be normally open or normally closed.
Normally open inputs are the ones that have the two vertical lines separated by a space in between. In PLC ladder logic programs, they are usually termed as “Make” inputs.
Normally open inputs, like normally open relays, do not by default create a connection between the two rails when connected. When activated, only then it will create the connection.
These inputs could be any input device or sensor that is used to conditionally activate an output device.
Normally closed inputs are the ones that have the two vertical lines with a forward slash that connects the two opposite ends.
In PLC ladder logic programs, they are usually termed as “Break” inputs and are an equivalent of the NOT operation.
Normally closed inputs, like normally closed relays, ALREADY creates a connection between the two rails when connected. When activated, only then it will BREAK the connection.
Normally closed inputs are most commonly used for STOP buttons or emergency shut down devices.
Shown above is the notation for an output device in a PLC ladder logic diagram. Again, this could be ANY output device that can be controlled by the PLC: Motor starters, Lamps, Buzzers, Solenoid Valves.
Different PLC Relays
PLCs have solved the problem brought by relays, and it was thanks to the software-based relays that were designed for the PLC.
These relays now only rely on the memory component of the PLC so instead of actually using relays to create a connection point, the memory of the PLC now only stores the states of the supposed relays.
Internal relays are elements that hold data to serve as relays for the system. This is what makes the PLC more cost-effective than the conventional relay systems that were used before.
These internal relays rely in bits on the memory of the PLC but can be treated the same way as an external relay (as what I’ve been saying since earlier).
For example, using an internal relay in conjunction with a push button allows you to turn on a solenoid valve in one rung and a motor in another rung using the same push button.
Furthermore, conditions for logical operations such as AND, OR, or NOT may be made using that same internal relay in a different rung.
When the PLC is suddenly turned off, whether due to a power outage or some connection breaks, the states of the internal relays become erased automatically.
Because of this, when you start the PLC after that scenario, the states of the internal relay will go back to the initial stage of the program rather than the LAST state that it had before the power outage happened.
This is where battery backed relays come into play. They retain their state of activation even when the power supply of the PLC is entirely off. Hence, they are also called retentive coils.
Set and Reset
Internal relays only retain their state if the input devices that are placed to activate them are also retaining their activation states.
For set and reset relays, however, you can actually retain their state indefinitely and then remove the state using another condition from an input device.
This type of internal relay is practically applied to a “set” condition with multiple input conditions and a “reset” condition ALSO with multiple input conditions (more on that later).
Applications of Internal Relays
The internal relays were designed for a specific purpose which is (obviously) to eliminate the need of external relays. This is desirable because not only you would not worry anymore about a relay getting some wear and tear, but also you would not have to worry about spending money on them.
Internal relays also make the PLC highly flexible because of the different applications that extend the PLC’s capabilities.
Multiple Input Conditions
In designing control systems, there are times when you would want more than one condition to turn something ON or OFF.
In this situation, using the input device notation over and over again would be impractical and messy as a ladder logic program. The rung would have too many things on it!
Thus, the program above shows that there are two conditions that are represented by an internal relay EACH.
In practical applications, sometimes an output must be held ON for an indefinite amount of time until power is cut.
Input devices are usually momentary in nature, hence, for that scenario to happen, Latching programs are required to be set up in the PLC programming software.
So far, we have talked about retaining the states of relays and actually doing it even without power supplied to the PLC.
Now, this type of operation is used when you want just an output pulse that does not retain its state all throughout the program. This function is termed as one-shot.
One shot operation is momentary, hence pushing the trigger button will only cause an impulse to the output device.
Master inputs are used when you want the whole program to be dependent on a Master Control condition.
For instance, if you want the system to run ONLY when authorized personnel has already examined the machine, a Master ON button must be switched ON first.
It does not matter if the Slave START buttons were pushed over and over again, because the system will not start without the Master ON button being in an energized state.
Equivalent Ladder Diagrams for Logical Functions
The AND condition is basically just two inputs in series with each other. Because the inputs are in series, the two inputs must BOTH have continuity in order for the output to turn on.
The OR condition is a parallel connection between the two inputs. Because the inputs are in parallel to each other, ANY of them may create continuity between the two power rails thus turning the output ON.
The NOR condition, simply put, is an OR condition using negated inputs. Negating inputs is as simple as turning a Normally Open input to a Normally Closed one.
Similar to the NOR condition, the NAND condition is just a negated version of the AND condition. Normally Open inputs are inverted.
The XOR condition has a different story than the rest of the logical conditions. If you look at the Boolean equation for the XOR condition:
A XOR B = A’B + AB’
Therefore, to XOR the two variables A and B, you would have to perform NOT, AND, and OR conditions to your ladder logic programs. This way, only ONE input variable is allowed to control the output AT A TIME.
XNOR is rarely used as a logical statement, but it is actually best applied when simplifying logical expressions that have a negated output for XOR.
For this one, notice that the ladder logic diagram has an internal relay connected in a “break” manner in order to negate the result.
Remember, because the XOR is a combination of the AND and the OR operations, negation would apply in a different manner. The safest way is to apply it to the output.
Before you start: PLC Safety Systems
In applying the PLC programming skills that you have learned throughout this mini programming course, make sure that you familiarize yourself with the different safety techniques that are most commonly used in control systems!
Emergency Stop Button
Emergency stop buttons are essential in every control system because when the output devices start malfunctioning, you can have control of the power using the PLC by setting an emergency stop button.
This button can even be replaced by a sensor for further automaticity.
The two handed start is a basic practice for machines that are hazardous to operate due to the actuators’ short distance to the user. Usually, drilling machines are controlled using this type of setup.
A sensor input is also used to ensure that an object that was expected to be in the drilling platform is actually on the platform before the operator can even start the drilling.
Lastly, proximity sensing is used as a safety measure when there are huge machines that may impose risks because of some produced tiny projectiles or even being caught by the machine itself.
This is used to ensure that the controlled machines would not cause any harm when accidentally approached (if that is even a thing).
In doing PLC programming, there are plenty more combinations of programs that you may be able to design using the knowledge that you have acquired in this post. Your imagination is the limit!
By the way, if you want a post with a discussion on the steps required to actually program a PLC, check out this link: https://www.edgefx.in/how-to-program-the-programmable-logic-controllers/