Boolean logic for PLCs
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:
| State | Logic | On a 24 V input | On a contact |
|---|---|---|---|
| 0 | False | 0 V (no voltage) | Open |
| 1 | True | 24 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.
| A | B | A AND B | A OR B | NOT A |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 |
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:
- Contacts in series = AND. Power only flows if they are all closed.
- Contacts in parallel = OR. One closed contact is enough.
- Normally closed contact = NOT. It passes power when its signal is 0.
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:
| A | B | NAND | NOR | XOR | XNOR |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 0 | 1 |
| 0 | 1 | 1 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 0 | 1 |
- NAND is a negated AND: it gives 0 only when all the inputs are 1.
- NOR is a negated OR: it gives 1 only when all the inputs are 0.
- XOR (exclusive OR) gives 1 when the inputs are different. Its IEC symbol has “=1” inside.
- XNOR gives 1 when the inputs are equal.
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:
| Rule | With OR | With AND |
|---|---|---|
| Identity | A + 0 = A | A · 1 = A |
| Dominance | A + 1 = 1 | A · 0 = 0 |
| Idempotence | A + A = A | A · A = A |
| Complement | A + NOT A = 1 | A · NOT A = 0 |
| Absorption | A + A · B = A | A · (A + B) = A |
| Distributive | A + B · C = (A + B) · (A + C) | A · (B + C) = A·B + A·C |
| Double negation | NOT (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.
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:
NOT (A · B) = NOT A + NOT BNOT (A + B) = NOT A · NOT B
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.”
- Conditions that must all be met (AND): stop not pressed, overload relay OK and door closed.
- To start: press Start. To keep running after releasing it: output M itself is “fed back”.
- 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.
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.
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
- Confusing the program contact with the push button contact. The program one checks a bit.
- Forgetting brackets.
Level + Temp · NOT Maintis not the same as(Level + Temp) · NOT Maint: in the first case, a high level would also sound during maintenance. - Applying De Morgan halfway. Negating the variables without swapping AND and OR.
- Redundant conditions. Repeating a signal (
A · A) or leaving in signals that have no effect. It does not fail, but it makes the program harder to understand and maintain. - Wiring stops normally open. It works on day one and fails on the day it matters.
Summary
- AND: all at 1 (contacts in series). OR: at least one at 1 (in parallel). NOT: the opposite (normally closed contact).
- With n inputs there are 2n combinations.
- AND comes before OR; when in doubt, use brackets.
- De Morgan: when negating, negate the variables and swap AND for OR.
- Stops are wired normally closed for safety and, in the program, used with a normally open contact.
- Emergency stop safety is guaranteed by safety components, not by the standard PLC.
Test what you have learned
50 practical questions with explained answers. Free, no sign-up.
Start the test