ExamRobotic

Boolean logic for PLCs

Level: Basic 10 min read 50 questions

In a programmable controller everything ends up as a 0 or a 1: a sensor detects or it does not, a contactor is energised or it is not, a door is closed or open. Boolean logic is the tool for combining those signals and deciding what the machine does. It looks like blackboard theory, but people who master it write shorter, clearer programs with fewer bugs, and understand straight away why a machine will not start.

Zeros and ones: what we are talking about

A binary signal has only two states. In automation we call them many things, but they mean the same:

StateLogicOn a 24 V inputOn a contact
0False0 V (no voltage)Open
1True24 V (voltage present)Closed

On typical 24 V digital inputs, a voltage above about 15 V is read as 1 and below about 5 V as 0. In between there is an undefined zone that should never occur in a healthy installation: if you measure an input at 9 V, something is wrong (a loose wire, a damaged sensor or a voltage drop). The exact values depend on the module and are given in its datasheet.

The three basic operations

With just three operations you can build any logic, however complicated.

AND: all at once

The output is 1 only if all the inputs are 1. It is written with a dot: Q = A · B. It is the operation for conditions that must be met together: “the motor starts if there is a start command and the door is closed and the overload relay is OK”.

OR: at least one

The output is 1 if any of the inputs is 1. It is written with a plus sign: Q = A + B. It is the operation for alternatives: “the alarm sounds if the level is high or the temperature is high”.

NOT: the opposite

It inverts the signal: 1 in gives 0 out, and the other way round. It is written with a bar over the letter (A) or with the word NOT: Q = NOT A. On this site we use NOT because it is easier to read.

ABA AND BA OR BNOT A
00001
01011
10010
11110
& ≥1 1 ABQ ABQ AQ ANDORNOT
IEC symbols: “&” is AND, “≥1” is OR (“at least one input at 1”) and the circle on the output means negation.

The same thing, with contacts

In a ladder diagram (LAD) power flows from left to right and energises the coil if it finds a closed path. That is why:

ABQ ABQ Q = A · B (series)Q = A + B (parallel)
Top, two contacts in series (AND). Bottom, two contacts in parallel (OR). The brackets on the right are the coils.

Derived operations: NAND, NOR, XOR and XNOR

They are combinations of the basic ones, but they come up so often that it pays to recognise them:

ABNANDNORXORXNOR
001101
011010
101010
110001
On siteXOR is the tool for detecting discrepancies. If a machine has two redundant sensors that should always give the same signal, an XOR between them (with a short delay to tolerate them not switching at the same instant) warns that one of them is failing. And the classic light switched from two places, the two-way switch circuit, behaves like an XOR.

The truth table: every combination

A truth table lists what the output is for every possible combination of the inputs. With n inputs there are 2n combinations: 2 inputs give 4 rows, 3 give 8, 4 give 16 and 5 give 32. That is why, for conditions with many signals, it is worth simplifying before programming.

To build one, write the combinations in order (like counting in binary: 000, 001, 010…) and work out the output row by row. It is the safest way to check that an equation does exactly what the specification asks for.

Boolean algebra: the rules that simplify

These rules let you reduce long expressions to shorter ones that do the same. The most useful ones:

RuleWith ORWith AND
IdentityA + 0 = AA · 1 = A
DominanceA + 1 = 1A · 0 = 0
IdempotenceA + A = AA · A = A
ComplementA + NOT A = 1A · NOT A = 0
AbsorptionA + A · B = AA · (A + B) = A
DistributiveA + B · C = (A + B) · (A + C)A · (B + C) = A·B + A·C
Double negationNOT (NOT A) = A

A real example: Q = A · B + A · NOT B. Taking A out as a common factor, Q = A · (B + NOT B), and since B + NOT B = 1, we get Q = A. Signal B has no effect at all: if someone had put it in the program, it was unnecessary.

Order of operationsJust as in maths multiplication comes before addition, in logic AND comes before OR. A + B · C means A + (B · C). If you want something else, use brackets.

De Morgan’s laws

These are the two rules used most often to negate an expression:

In words: when negating, each variable is negated and AND becomes OR (or the other way round). The most common slip is negating the variables and leaving the operation unchanged.

In practice, they take you from “when it runs” to “when it stops”. If a machine runs when All_OK = C1 · C2 · C3, then it must stop when NOT All_OK, which by De Morgan is NOT C1 + NOT C2 + NOT C3: it is enough for any one of the conditions to fail.

From specification to equation

This is how a real specification is translated. “A conveyor starts when Start is pressed and keeps running when it is released. It stops when Stop is pressed, if the overload relay trips or if the guard door opens.”

  1. Conditions that must all be met (AND): stop not pressed, overload relay OK and door closed.
  2. To start: press Start. To keep running after releasing it: output M itself is “fed back”.
  3. Result: M = (Start + M) · Stop_OK · Overload_OK · Door_closed.

The term (Start + M) is the seal-in (or latch): once energised, M holds itself until one of the AND conditions is lost. And because the stop conditions are in series, stop always has priority: if Start and Stop are pressed at the same time, the conveyor does not run.

StartStop_OKOverload_OKDoor MM
Start/stop seal-in: the M contact in parallel with Start keeps the output on when the push button is released.

The case that confuses everyone: the stop button wired normally closed

Stop push buttons are wired normally closed (NC). At rest they let the 24 V through, so the PLC input is 1 while nobody presses them. When pressed, the contact opens and the input goes to 0.

Why the opposite of what might seem logical? For safety: if a stop wire breaks or comes loose, the input drops to 0 and the machine stops. If the stop were wired normally open, a broken wire would leave the stop useless and nobody would notice until the day it was needed. This is the fail-safe principle: when something breaks, the machine must go to the safe state.

And here comes the confusion: in the program, that stop is used with a normally open contact. The program contact does not copy the physical push button: it asks for the value of the bit. With the stop not pressed, the bit is 1, the normally open contact in the program passes power and the machine can run. When pressed, the bit is 0 and the contact opens the rung.

Typical mistake“The push button is normally closed, so I put a normally closed contact in the program.” Result: with the stop at rest (bit at 1) the program’s normally closed contact is open and the machine never starts. The type of contact in the program depends on the value of the bit you want to check, not on what kind of push button it is.
Emergency stopAn emergency stop button is not handled just with a contact in the program of a standard PLC. The safety function must be implemented with safety components (a safety relay or a safety PLC) according to the machine’s risk assessment. The standard PLC can read the button to show messages or stop the sequence in an orderly way, but it is not what guarantees safety.

Karnaugh maps in two minutes

A Karnaugh map is a truth table drawn so that neighbouring cells differ in only one variable. That is why the rows and columns are ordered 00, 01, 11, 10 (Gray code) and not 00, 01, 10, 11. When you group adjacent 1s into blocks of 2, 4 or 8, the variables that change inside the block disappear and the equation comes out already simplified. For 3 or 4 variables it is faster than algebra; for more, software is used, or the logic is simply written step by step with well-named intermediate memory bits.

Typical mistakes that give away someone who has not mastered logic

Summary

Test what you have learned

50 practical questions with explained answers. Free, no sign-up.

Start the test