PLC hardware associated with the logic gates tutorial, Boolean logic, PLC programming, two-input gates

Logical Gates in Ladder Logic for PLC

2.5K Views
40
700
60
25
60
PCBWay

Hello friends! In the previous ladder-logic introduction, we built AND, OR and NOT conditions using contacts and coils. Today we will combine those building blocks to create NAND, NOR, XOR and XNOR. Each gate answers a different question about its inputs, so understanding the question is more useful than memorizing a diagram.

We will retain the original X0, X1 and Y0 examples and walk through their truth tables. Then we will apply the same reasoning to a sensor-disagreement indicator and a command-feedback comparison. These examples make it easier to see why a rung that is logically correct can still be unsuitable for a particular process requirement.

Before starting, remember that ladder contacts inspect Boolean data. They do not change the physical input or carry a real voltage through the program. A direct contact is true when its bit is 1, an inverted contact is true when its bit is 0, and a normal coil writes the rung result to its destination when executed.

PLC hardware associated with the logic gates tutorial, Boolean logic, PLC programming, two-input gates
Figure: PLC hardware associated with the logic gates tutorial.

Review the Three Basic Operations

Contacts in series implement AND because every condition on that path must be true. Parallel paths implement OR because any complete true path is sufficient. An inverted contact implements NOT for the bit it references. From these three operations, we can construct all four gates in this tutorial.

We will call X0 input A and X1 input B when writing Boolean expressions. Y0 is the resulting command bit. Those names describe a learning example; a real project should use meaningful comments such as SensorAActive, SensorBActive and Disagreement. Different manufacturers use different address formats without changing the underlying Boolean relationships.

Two-input gates compared
ABANDORNANDNORXORXNOR
00001101
01011010
10011010
11110001

Read a row as one complete input condition. For example, at A = 1 and B = 0, AND is false but OR is true. Their inverses are therefore NAND = 1 and NOR = 0. XOR is true because the inputs differ, while XNOR is false for the same reason.

NAND Gate in Ladder Logic

NAND means NOT AND. Its expression is Y0 = NOT (X0 AND X1). The output becomes false only when both inputs are true. At least one false input is enough to make the NAND result true.

Two-input NAND truth table, Boolean logic, PLC programming, two-input gates
Figure: Two-input NAND truth table.

The historical truth-table image shows this relationship. We can implement it without an inverted output coil by applying De Morgan's law: NOT (A AND B) = (NOT A) OR (NOT B). In ladder notation, place two inverted contacts on parallel branches before a normal coil.

NAND ladder rung with parallel inverted contacts, Boolean logic, PLC programming, two-input gates
Figure: NAND ladder rung with parallel inverted contacts.

NAND with a normal output coil:

      +----[/ X0 ]----+
------+              +--------( Y0 )------
      +----[/ X1 ]----+

Y0 = (NOT X0) OR (NOT X1)

Let us inspect every case. With both inputs off, both inverted contacts are true. With only X0 on, its branch is false but the X1 branch remains true. With only X1 on, the opposite branch supplies the true result. Only when both inputs are on do both paths become false.

What NAND means in a process

Suppose two independent completion signals must both be present before a process is considered complete. A bit called NotBothComplete can use NAND. It stays true while either completion condition is missing. That name is more accurate than Fault because an incomplete process is not necessarily faulty.

The distinction matters at startup. If neither completion sensor is active, NAND returns true immediately. If you connect that result directly to an alarm without considering whether completion is currently expected, the program may report a fault during perfectly normal operation.

NOR Gate in Ladder Logic

NOR means NOT OR: Y0 = NOT (X0 OR X1). Its output is true only when both inputs are false. If either input becomes true, the NOR result becomes false. This corrects the contradictory wording in the old explanation that left the output on in both cases.

Two-input NOR truth table, Boolean logic, PLC programming, two-input gates
Figure: Two-input NOR truth table.

De Morgan's law gives NOT (A OR B) = (NOT A) AND (NOT B). Therefore, two inverted contacts in series implement NOR with a normal output coil. The difference from NAND is the series arrangement rather than parallel branches.

NOR ladder rung with series inverted contacts, Boolean logic, PLC programming, two-input gates
Figure: NOR ladder rung with series inverted contacts.

NOR with a normal output coil:

------[/ X0 ]------[/ X1 ]------( Y0 )------

Y0 = (NOT X0) AND (NOT X1)

At X0 = 0 and X1 = 0, both inverted conditions are true. At 0,1 or 1,0, one condition blocks the series path. At 1,1, both conditions are false. Thinking in terms of Boolean tests makes the result much clearer than saying that an input itself somehow closes or opens inside the program.

NOR as a neither-active indication

For two non-safety process requests, a bit called NeitherRequestActive can use NOR. It reports an idle request state. Whether the machinery should actually be idle depends on other state and command logic; a truth table alone does not define the complete sequence.

Do not interpret two inactive sensors as proof of a healthy system. They could both be inactive because no object is present, or because their supply is disconnected. Additional information is needed to distinguish those conditions.

XOR Gate in Ladder Logic

XOR means exclusive OR. For two inputs, it is true when exactly one input is true. Equivalently, it detects that the inputs differ. Its expression can be written as Y0 = (X0 AND NOT X1) OR (NOT X0 AND X1).

Two-input XOR truth table, Boolean logic, PLC programming, two-input gates
Figure: Two-input XOR truth table.

The first branch detects the 1,0 case and the second detects the 0,1 case. Put a direct X0 contact and inverted X1 contact in series on one branch. On the other branch, use inverted X0 and direct X1. Rejoin the branches before the normal output coil.

XOR ladder diagram with two unequal-state branches, Boolean logic, PLC programming, two-input gates
Figure: XOR ladder diagram with two unequal-state branches.

Two-input XOR:

      +----[ X0 ]----[/ X1 ]----+
------+                        +----( Y0 )----
      +----[/ X0 ]----[ X1 ]----+

At 0,0, each branch contains a false direct contact. At 1,1, each branch contains a false inverted contact. The output is therefore off for matching states. In each unequal state, one whole branch becomes true while the other stays false.

Example: detect disagreement

Suppose two ordinary sensors observe the same process condition. XOR can create a Disagreement bit when one reports active and the other reports inactive. That is a useful diagnostic observation, but it does not tell us which sensor is correct.

Two sensors may also disagree briefly because their thresholds or positions differ. If a moving object reaches one sensor 20 ms before the other, the XOR result can be true during that interval even when both devices work correctly. A diagnostic requirement may need a persistence timer and a clearly defined operating state.

Three-input XOR is not exactly-one logic

For two inputs, XOR and exactly one true happen to match. With three inputs, chained XOR returns true for an odd number of true inputs, including three. If the requirement is exactly one active request out of three, use a dedicated expression or count the true conditions instead of blindly extending the two-input diagram.

For Boolean A, B and C represented numerically as 0 or 1, exactly one means A + B + C = 1. Three-input parity instead means that the sum is odd. Writing the requirement as a count exposes the difference before the ladder program becomes complicated.

XNOR Gate in Ladder Logic

XNOR is the inverse of XOR. For two inputs, it is true when they match: both false or both true. We can write Y0 = NOT (X0 XOR X1), or expand it as (X0 AND X1) OR (NOT X0 AND NOT X1).

Two-input XNOR truth table, Boolean logic, PLC programming, two-input gates
Figure: Two-input XNOR truth table.

The expanded form uses one branch with two direct contacts and another with two inverted contacts, followed by a normal coil. This construction is easy to inspect because each branch corresponds to one true row of the truth table.

Two-input XNOR with a normal coil:

      +----[ X0 ]-----[ X1 ]----+
------+                        +----( Y0 )----
      +----[/ X0 ]---[/ X1 ]----+

Historical XNOR ladder example using an inverted coil, Boolean logic, PLC programming, two-input gates
Figure: Historical XNOR ladder example using an inverted coil.

The original XNOR diagram instead shows XOR conditions followed by an inverted coil. In platforms that support that instruction, the destination receives the negated rung result. ABB's coil documentation describes this behavior. It is not a physical normally closed output contact, and you should not assume that every editor uses the same symbol or supports the instruction.

Agreement does not necessarily mean healthy

If both sensor inputs are zero, XNOR is true. If both are one, it is also true. A bit called InputsAgree is therefore accurate. A bit called MachineSafe would claim much more than the expression proves.

For example, both signals could be zero because they share a failed power supply. Boolean agreement cannot detect every common-cause failure. The value of XNOR is that it answers a precise equality question; other process checks must supply the additional meaning.

Use De Morgan's Laws to Review a Rung

De Morgan's laws exchange AND with OR when a complete expression is inverted. They also invert each individual condition. This explains why two inverted contacts in parallel implement NAND, while two inverted contacts in series implement NOR.

NOT (A AND B) = (NOT A) OR (NOT B)
NOT (A OR B)  = (NOT A) AND (NOT B)

A common mistake is to invert every contact but keep the original branch arrangement. Starting with AND and merely inverting its series contacts produces NOR, not NAND. The branch arrangement is part of the logic and must be transformed together with the contacts.

These laws also help when a controller lacks an inverted-coil instruction. Rewrite the Boolean expression so a normal coil receives the desired condition. Prefer the representation that your team can review reliably rather than the one with the fewest symbols at any cost.

Combine a Gate with an Enable Condition

Suppose a disagreement indicator should operate only while MonitoringEnabled is true. The required expression is MonitoringEnabled AND (A XOR B). Put the enable contact outside the complete XOR branch structure so it applies to both unequal-input paths.

If the enable contact is placed on only one branch, one direction of disagreement will bypass it. Test A = 1, B = 0 and A = 0, B = 1 separately with monitoring disabled. This targeted check catches a wiring-like error in the program's branch layout.

The same reasoning applies to mode selection and process permissives. State the parentheses explicitly when translating a requirement into an expression. A AND (B OR C) differs from (A AND B) OR C because the second expression permits C to bypass A.

Command and Feedback: A Worked Diagnostic

Let Command represent a non-safety actuator command and Feedback represent confirmation that the actuator is active. XOR detects a mismatch between them. There are two possible mismatches, and they may need different messages.

Interpret command and feedback combinations
CommandFeedbackXORInterpretation
000Off is commanded and indicated
011Feedback remains active without a command
101Command is active without confirmation
110On is commanded and indicated

The first mismatch can indicate delayed release, a stuck contact or incorrect feedback wiring. The second can indicate delayed pickup, missing power or a failed actuator. XOR groups them into one bit, but retaining the individual conditions helps the operator diagnose the problem.

Assume, for a teaching example, that normal pickup or release can require up to 80 ms. An alarm that trips after one 10 ms scan would be too sensitive. A hypothetical 150 ms persistence condition allows the expected transition time plus some margin. Those numbers illustrate the reasoning; select actual timing from the device and process requirements.

A persistence timer should reset when the mismatch disappears if the requirement is continuous disagreement. A latched alarm may then remember a confirmed event until acknowledged. The instantaneous XOR result, timer state and latched alarm are three different pieces of information.

Scan Order and Output Ownership

Use separate internal result bits if you want to compare all four gates in one learning project. Writing each gate to the same Y0 on separate rungs makes the later write overwrite the earlier results. Four rungs do not automatically combine their output commands.

The examples assume a stable input snapshot during evaluation. In controllers with asynchronous I/O updates, copying related inputs into a local snapshot at an appropriate point may be useful when consistency matters. Follow the platform's documented task and data-handling model rather than assuming that the online display proves simultaneous sampling.

Rockwell's bit-instruction reference is useful for distinguishing contact tests, ordinary output assignments and latching instructions. Use the equivalent instructions in your own PLC environment, with one clear owner for each final hardware command.

A Practical Simulation Procedure

  1. Create a learning project with two test inputs and separate internal result bits.
  2. Write the expected truth table before drawing the rungs.
  3. Implement NAND and NOR using inverted contacts and normal coils.
  4. Implement XOR and XNOR using the two-branch constructions.
  5. Apply input combinations 00, 01, 10 and 11, allowing the program to execute after each change.
  6. Compare every result with the table and investigate any mismatch.
  7. Add a common enable, then repeat the cases with that enable both true and false.
  8. Remove forces and save the project with comments explaining every bit.

Two Boolean inputs have 22 = 4 combinations; adding an enable gives 23 = 8. Exhaustive checking is therefore practical for these small examples. It is much stronger than trying one convenient input combination and deciding that the program works.

The diagrams here explain the intended logic and preserve the historical tutorial examples. They do not constitute a report of a newly executed PLC hardware test. Use a simulator or a controlled low-voltage training arrangement before connecting outputs to equipment.

Check a written requirement yourself

Try this exercise: illuminate an indicator when monitoring is enabled and neither request is active. Define E as the enable and A and B as the requests. The expression is E AND NOT (A OR B), which becomes E AND NOT A AND NOT B. Draw one direct E contact and two inverted request contacts in series. With E false, all four request combinations must leave the indicator off. With E true, only the 0,0 request combination may illuminate it.

Review and Troubleshooting

If NAND and NOR appear exchanged, inspect series versus parallel placement. If XOR is on for both inputs true, one inverted contact may be missing. If XNOR is off for both inputs false, the all-false branch may be absent. Check the expression first, then the address assigned to each symbol.

If a truth table passes but the application behaves incorrectly, inspect what each input actually means. A healthy circuit represented by 1 is different from a fault represented by 1. Correct Boolean algebra applied to misunderstood signals still produces the wrong machine behavior.

Frequently Asked Questions

What is the quickest way to distinguish NAND and NOR?

NAND is false only when both inputs are true. NOR is true only when both inputs are false. With normal output coils, NAND uses parallel inverted contacts and NOR uses series inverted contacts.

Is XOR the same as ordinary OR?

No. Both are true for unequal two-input states, but ordinary OR is also true when both inputs are true. XOR is false in that case.

Does XNOR prove that two sensors are working?

No. It proves only that their Boolean values match. Both sensors can agree on an incorrect value, especially if they share a failure cause.

Do I need an inverted coil for XNOR?

No. Use parallel all-true and all-false branches followed by a normal coil. That makes the equality condition visible without relying on a special output instruction.

Can these gates provide a complete safety function?

These exercises teach Boolean logic. A machinery safety function requires appropriate architecture, hardware, diagnostics and validation beyond a truth table.


Comments

6

Join the conversation

1 reply
Reply1.1
Replying to adiono

Hi,

Rite now I'm more concerned about the basic concepts, so I won't recommend any simulation software instead I'll more focus on developing logics which I'm gonna cover in future articles, will also post about simulation software for plc.

Thanks.