PLC Logic – The Logic Behind The PLC Program

This is an introduction to PLC logic and how to use it to build PLC programs from simple electrical circuit diagrams. In this article, I will work with combinatorial logic to put together the functionality of a PLC program.

To bring the example closer to a real PLC programming language such as ladder logic, I will draw the PLC logic as contacts and relays in an electrical circuit.

You will start with a very simple PLC program, using combinatorial logic to build it.

The reason for this is that a PLC program is really just logic. Everything in a PLC has two states, just like in digital logic. In logic, the two states are called FALSE and TRUE. Sometimes these two states are referred to as “0” and “1”.

Using the numbers 0 and 1 for these states lets us express them as binary numbers.

Since the PLC works with two states only, it makes sense to use the base-2 numeral system or binary numbers to represent the two states. Binary numbers, just like the PLC, has two numbers or states. 0 and 1. We can now represent the FALSE with 0 and TRUE with 1. This will be very helpful when working with analog inputs, outputs and calculations.

The PLC uses logic to decide how it will set its outputs. The inputs to that logic stand for the PLC inputs. Taken together, all the logic forms our PLC program, and the outputs of the logic become the outputs of our PLC program.

Your First PLC Logic Program

For this first program, we need a single input and an inverted output. An on signal becomes off, and an off signal becomes on. You can think of it as pressing a light switch remotely.

The program needs to work as follows:

  • If the input is 0 or FALSE; then the output will be 1 or TRUE.
  • If the input is 1 or TRUE; then the output will be 0 or FALSE.

We can place this functionality in the PLC’s scan cycle:

  1. Scan the inputs.
  2. Use the input I0.0 and set output Q0.0 as the inverted.
  3. Set the outputs

During the first step of the scan cycle, the PLC reads the states of all its inputs. Our PLC logic program, drawn with logic gates, runs in the second step. Here, the input state and the logic together determine the output state.

The input and output names are not something you need to worry about yet. I have named the input I0.0 to follow the input naming convention of a Siemens PLC such as the Siemens S7-1200. The output name Q0.0 follows the same convention.

Input and output naming conventions differ between PLC manufacturers and depend on the number of inputs and outputs. For more on I/O addressing in a Siemens PLC, read the S7-300 instruction list, page 7-10. Siemens identifies inputs with the letter I and an address, and uses the letter Q for outputs.

Making Use of PLC Logic

With the inputs and outputs defined, and the required function clear, you can build the PLC logic:

We need input I0.0 to control output Q0.0. Connecting them directly would be a natural starting point:

PLC logic program direct connection

Direct connection between input and output in PLC logic

A direct connection would make the output follow the input. You can see the result without a truth table: an input of 0 gives an output of 0, and an input of 1 gives an output of 1.

Our program needs the output to have the opposite state to the input. A NOT-gate does this by inverting the signal before it reaches the output. It changes on to off and off to on.

The diagram now looks like this:

PLC logic program with NOT-gate

NOT-gate to illustrate the inverter function in PLC logic.

The signal is inverted before being sent to the output, which gives us the required function. The truth table confirms that the output is the inverse of the input:

I0.0 Q0.0
0 1
1 0

This is a deliberately simple example, intended to show how a PLC program actually works. The program consists of the logic that decides the output states. You can then choose which inputs to use in that logic. Their states, together with the logic you build, determine the states of the outputs.

Many PLCs have communications and other advanced functions. Their main function, however, is still to use logic to decide the states of the outputs.

Representing PLC Logic with Electrical Circuits

The PLC logic program we have drawn with a NOT-gate still looks quite different from actual PLC programming. To move closer to the way you program a PLC, we need to draw the same program another way. In this example, that means making it look more like ladder logic.

Ladder logic looks much like an electrical circuit diagram. That makes contacts and relays useful for thinking about PLC logic. As I explained in my article on combinatorial logic, both have two states, matching the two states in PLC logic. A contact can be ON or OFF, and so can a relay.

Combining normally open and normally closed contacts with relays produces logic that looks much like actual PLC programming. We can use the NOT-gate example to see how this works.

Contacts in PLC Logic for Inputs

In the above example, I used a NOT-gate to illustrate the inverted logic of the PLC logic program. The NOT-gate is also called the inverter function can be illustrated with a normally closed contact. The normally closed (NC) contact is in PLC programming referred to as an examination if open instruction (XIO). Examine if closed is an instruction that examines a bit for the off condition.

To illustrate that with the normally open contact, you can think of the two sides of the contact as an input and an output. When the input of the contact is off (open), then the output of the contact will be on. Or said in another way; the output will examine a bit. And thereby the function name examines if open.

Here is how the illustration with the normally open contact looks like:

Normally open contact in PLC logic

Normally open contact to illustrate examine if open PLC logic.

The normally closed contact now stands for the PLC logic, with the function unchanged. When input I0.0 is off, output Q0.0 is on. This gives us the same function as the NOT-gate.

The contacts in this diagram do more than show the PLC logic: they also stand for the inputs. Within the logic, the contacts show the logical function, while the state of each contact shows the state of the input it stands for.

These input contacts differ from the physical contacts wired to the PLC inputs. The contacts in the program are boolean instructions. My article about the normally closed stop button explains the difference between boolean instructions and physical contacts.

Negative Logic

This example uses ‘negative logic’, which reverses the usual on and off states. An off input signal counts as on in the logic, and an on signal counts as off. This lets us reverse the control behavior.

An off input therefore gives an on logic state. With the input on, the logic state is off.

The following video explains negative logic, the same function as the examine if open instruction mentioned earlier:

Representing PLC Outputs with Relays

PLC programming needs a way to show the outputs as well as the inputs. Our PLC logic diagram must therefore include something that can illustrate two states. As with digital inputs, a digital PLC output can have only two states: on or off.

We already use an electrical circuit to draw the inputs, so we can use relays for the outputs. Relays suit this purpose for two reasons:

  1. Relays have two states like the PLC outputs
  2. Relays have functional contacts to use for latching and much more

With a relay standing for each PLC output, we can continue the example.

Our PLC logic diagram now looks like this:

Representing the PLC output with a relay

Representing the PLC output with a relay.

A relay now shows the state of output Q0.0. When the relay in our logic is on, the output state is also on.

Relays also have functional contacts. Several normally open or normally closed contacts can show the state of a single relay, following each change in the relay state. Latching is one of the most common uses for these contacts.

Latching keeps a relay energized, or on, through a functional normally open contact belonging to that relay. The same arrangement in PLC logic can keep an output on.

In this illustration, one normally open contact stands for PLC input I0.0. The PLC output now has both a relay and a contact.

Latching a relay or a PLC output with a normally open contact.

Latching a relay or a PLC output with a normally open contact.

An input I0.0 state of 1 turns on the normally open contact I0.0. The relay is then energized and output Q0.0 turns on. Latching follows from this change.

Once the output state changes to 1, its normally open contact also turns on. Even if the PLC input then changes to 0, that contact keeps the relay energized. Output Q0.0 therefore keeps itself energized.

Be aware that this is just an example. In this example, it would be impossible to de-energize the relay and turn the output off. To do so, you would have to insert another contact between the output contact and the output relay to break the connection. For example, you could use another input represented by a normally closed contact:

Latching a PLC output and breaking the latch with a normally closed contact

Latching a PLC output and breaking the latch with a normally closed contact.

The input and output states fit into the logic as follows:

  • The state of the input is set outside the PLC logic and represented by contacts
  • The state of the outputs is set inside the PLC logic and represented by relays and contacts

For a more detailed explanation, I recommend a good PLC programming book.

Electrical Circuit Diagrams and PLC Programming

The diagram looks much like an electrical circuit, and it almost is one. Think of the input side as the positive side of a power source, and the output side as its negative or ground side.

There are two reasons for drawing it this way. The more obvious reason is:

The PLC programming language called ladder logic looks almost like electrical circuits.

Here is a small example of ladder logic:

Ladder logic diagram looks like an electrical diagram

The ladder logic diagram looks just like an electrical circuit diagram. Source: Wikipedia

The example resembles an electrical circuit and works as described here. Ladder logic is the most visual way to show PLC logic, and it is the usual starting point for most beginners.

There are other PLC programming languages, though I think ladder logic and ladder diagrams are the best known. Ladder logic is also the easiest route into PLC programming.

With this understanding of PLC logic, you can start learning ladder logic programming if you have not begun programming PLCs yet.

If you have anything to add, leave a comment below.

1 Comments
Leave a response

Leave a Response