Bird
Raised Fist0
PLC Programmingscripting~15 mins

Comparison instructions (EQ, NE, GT, LT) in PLC Programming - Deep Dive

Choose your learning style10 modes available
Overview - Comparison instructions (EQ, NE, GT, LT)
What is it?
Comparison instructions in PLC programming are commands that check how two values relate to each other. They test if values are equal, not equal, greater than, or less than. These instructions help the PLC decide what to do next based on these comparisons. They are simple yes/no questions the PLC asks about data.
Why it matters
Without comparison instructions, a PLC cannot make decisions based on changing data. For example, it wouldn't know if a temperature is too high or if a part count reached a target. This would make automation impossible or very limited. Comparison instructions let machines react and control processes safely and efficiently.
Where it fits
Before learning comparison instructions, you should understand basic PLC programming concepts like variables and simple logic. After mastering comparisons, you can learn about combining them with timers, counters, and branching logic to build complex control flows.
Mental Model
Core Idea
Comparison instructions are simple questions a PLC asks to decide if one value matches or relates to another in a specific way.
Think of it like...
It's like comparing two numbers on a scoreboard to decide who is winning or if the game is tied.
┌───────────────┐       ┌───────────────┐
│   Value A     │       │   Value B     │
└──────┬────────┘       └──────┬────────┘
       │                       │
       │                       │
       ▼                       ▼
  ┌─────────────────────────────────┐
  │ Comparison Instruction (EQ, NE, │
  │ GT, LT)                        │
  └───────────────┬─────────────────┘
                  │
          ┌───────┴────────┐
          │ Decision Output │
          └─────────────────┘
Build-Up - 6 Steps
1
FoundationWhat are Comparison Instructions
🤔
Concept: Introduce the basic idea of comparison instructions in PLCs.
Comparison instructions check the relationship between two values. EQ means equal, NE means not equal, GT means greater than, and LT means less than. These instructions return true or false, which the PLC uses to control outputs or other logic.
Result
You understand that comparison instructions are simple yes/no checks between two values.
Understanding that these instructions are basic decision points helps you see how PLCs make choices.
2
FoundationBasic Syntax and Usage
🤔
Concept: Learn how to write and use EQ, NE, GT, and LT instructions in PLC code.
In ladder logic, comparison instructions are placed between two values or variables. For example, EQ compares if ValueA equals ValueB. If true, the rung continues; if false, it stops. This controls whether outputs activate or other instructions run.
Result
You can write simple comparison instructions to check values in PLC programs.
Knowing how to use these instructions in code lets you start building decision-making logic.
3
IntermediateCombining Comparisons with Logic
🤔Before reading on: do you think multiple comparisons can be combined directly in one rung or need separate rungs? Commit to your answer.
Concept: Learn how to combine multiple comparison instructions using AND/OR logic.
You can combine comparisons by placing them in series (AND) or parallel (OR) in ladder logic. For example, to check if ValueA is greater than ValueB AND ValueC is less than ValueD, place GT and LT instructions in series. This lets you build complex conditions.
Result
You can create compound conditions that depend on multiple comparisons.
Understanding how to combine comparisons expands your ability to control complex processes.
4
IntermediateUsing Comparisons with Different Data Types
🤔Before reading on: do you think comparison instructions work the same for numbers and text? Commit to your answer.
Concept: Explore how comparison instructions behave with integers, floats, and strings.
Comparison instructions typically work with numbers like integers and floats. Some PLCs allow comparing strings with EQ and NE, checking if text matches exactly. However, GT and LT usually apply only to numbers. Knowing data types is important to avoid errors.
Result
You understand which data types can be compared and how to avoid type errors.
Knowing data type limits prevents bugs and unexpected behavior in your programs.
5
AdvancedHandling Edge Cases and Limits
🤔Before reading on: do you think comparing very large or very small numbers can cause problems? Commit to your answer.
Concept: Learn about potential issues like overflow, precision, and limits in comparisons.
When comparing very large or very small numbers, floating-point precision can cause unexpected results. Also, some PLCs have limits on number size. For example, comparing a number outside the allowed range may give false results. Properly handling these cases is critical for reliable control.
Result
You can anticipate and avoid errors caused by numeric limits in comparisons.
Understanding numeric limits helps you write safer and more predictable PLC programs.
6
ExpertOptimizing Comparison Instructions in Production
🤔Before reading on: do you think using many comparison instructions slows down PLC scans significantly? Commit to your answer.
Concept: Discover how to write efficient comparison logic to optimize PLC scan time and reliability.
In large programs, excessive or redundant comparisons can slow down the PLC scan cycle. Experts optimize by minimizing comparisons, using range checks instead of multiple GT/LT, and structuring logic to short-circuit unnecessary checks. This improves performance and reduces risk of missed cycles.
Result
You know how to write comparison logic that is both correct and efficient in real-world systems.
Knowing how to optimize comparisons prevents performance bottlenecks in complex automation.
Under the Hood
Comparison instructions work by the PLC CPU fetching the two values from memory, then performing a simple arithmetic or equality check using its processor logic. The result is a boolean true or false that controls the flow of the program. This happens very fast during each scan cycle.
Why designed this way?
These instructions were designed to be simple and fast because PLCs must scan all logic repeatedly and quickly. Complex operations would slow down the control system. The limited set of EQ, NE, GT, and LT covers most decision needs efficiently.
┌───────────────┐       ┌───────────────┐
│   Memory A    │       │   Memory B    │
└──────┬────────┘       └──────┬────────┘
       │                       │
       ▼                       ▼
  ┌───────────────────────────────┐
  │      CPU Comparison Unit       │
  │  (performs EQ, NE, GT, LT)    │
  └───────────────┬───────────────┘
                  │
          ┌───────┴────────┐
          │ Boolean Result │
          └─────────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does EQ check if two values are approximately equal or exactly equal? Commit to your answer.
Common Belief:EQ checks if two values are close enough, allowing small differences.
Tap to reveal reality
Reality:EQ checks for exact equality; even a tiny difference means false.
Why it matters:Assuming approximate equality can cause logic to fail unexpectedly, especially with floating-point numbers.
Quick: Can GT and LT be used to compare text strings alphabetically? Commit to your answer.
Common Belief:GT and LT can compare text strings to see which comes first alphabetically.
Tap to reveal reality
Reality:Most PLCs do not support GT and LT for strings; these instructions work only with numbers.
Why it matters:Trying to use GT or LT on strings can cause errors or unpredictable behavior.
Quick: Does using many comparison instructions always slow down the PLC significantly? Commit to your answer.
Common Belief:More comparisons always make the PLC slow and unreliable.
Tap to reveal reality
Reality:While many comparisons add some overhead, efficient programming and modern PLC speeds usually handle them well.
Why it matters:Overestimating performance impact may lead to unnecessary complexity or premature optimization.
Quick: Does NE mean the same as NOT EQ? Commit to your answer.
Common Belief:NE is just the opposite of EQ and works exactly the same way but reversed.
Tap to reveal reality
Reality:NE checks if values are not equal, but some PLCs treat special cases like undefined or null differently, so behavior can vary.
Why it matters:Assuming NE is always the exact inverse of EQ can cause subtle bugs in edge cases.
Expert Zone
1
Some PLCs optimize comparison instructions internally by short-circuiting evaluation in complex rungs, which affects how you order comparisons.
2
Floating-point comparisons can be unreliable due to precision errors; experts often implement tolerance ranges instead of direct EQ checks.
3
In safety-critical systems, comparison instructions are combined with watchdog timers and redundancy checks to ensure reliable decision-making.
When NOT to use
Avoid using direct EQ comparisons on floating-point numbers due to precision issues; instead, use range checks or tolerance-based logic. For complex string comparisons, use dedicated string functions or external processing.
Production Patterns
In production, comparison instructions are often used in sequence to implement state machines, threshold alarms, and interlocks. Experts write modular comparison blocks to reuse logic and simplify debugging.
Connections
Boolean Logic
Comparison instructions produce boolean results that feed into boolean logic operations like AND, OR, and NOT.
Understanding how comparisons output true/false values helps you combine them into complex decision trees.
Digital Circuit Design
Comparison instructions mimic digital comparators in hardware that output signals based on input voltage levels.
Knowing digital comparators clarifies why PLC comparisons are fast and reliable at the hardware level.
Decision Making in Psychology
Comparison instructions are like mental checks humans do to decide between options based on conditions.
Seeing comparisons as simple yes/no decisions connects programming logic to everyday human reasoning.
Common Pitfalls
#1Using EQ to compare floating-point numbers directly.
Wrong approach:IF Temperature EQ 75.0 THEN // do something END_IF
Correct approach:IF ABS(Temperature - 75.0) < 0.01 THEN // do something END_IF
Root cause:Misunderstanding that floating-point numbers can have tiny precision errors making exact equality unreliable.
#2Trying to use GT or LT to compare text strings.
Wrong approach:IF Name GT "John" THEN // do something END_IF
Correct approach:IF Name EQ "John" THEN // do something END_IF
Root cause:Assuming all comparison instructions work the same for all data types without checking PLC capabilities.
#3Stacking many comparisons without combining logic efficiently.
Wrong approach:IF A GT B THEN IF C LT D THEN IF E EQ F THEN // action END_IF END_IF END_IF
Correct approach:IF (A GT B) AND (C LT D) AND (E EQ F) THEN // action END_IF
Root cause:Not knowing how to combine comparisons with boolean logic leads to verbose and inefficient code.
Key Takeaways
Comparison instructions let PLCs make decisions by checking how two values relate.
EQ, NE, GT, and LT cover the most common comparisons needed for control logic.
Data types and precision affect how comparisons behave and must be handled carefully.
Combining comparisons with boolean logic builds complex decision-making in automation.
Efficient use of comparisons improves PLC performance and program clarity.

Practice

(1/5)
1. Which comparison instruction checks if two values are exactly the same in PLC programming?
easy
A. EQ
B. NE
C. GT
D. LT

Solution

  1. Step 1: Understand the meaning of EQ

    EQ stands for 'equal' and checks if two values are exactly the same.
  2. Step 2: Compare with other instructions

    NE means not equal, GT means greater than, and LT means less than, so they do not check equality.
  3. Final Answer:

    EQ -> Option A
  4. Quick Check:

    EQ means equal [OK]
Hint: EQ means equal, so it checks if values match exactly [OK]
Common Mistakes:
  • Confusing EQ with NE
  • Thinking GT checks equality
  • Mixing LT with EQ
2. Which of the following is the correct syntax to check if variable A is greater than variable B in a PLC program?
easy
A. IF A LT B THEN
B. IF A NE B THEN
C. IF A EQ B THEN
D. IF A GT B THEN

Solution

  1. Step 1: Identify the comparison for 'greater than'

    GT means 'greater than', so to check if A is greater than B, use 'A GT B'.
  2. Step 2: Check other options

    LT means less than, EQ means equal, NE means not equal, so they are incorrect for this check.
  3. Final Answer:

    IF A GT B THEN -> Option D
  4. Quick Check:

    GT means greater than [OK]
Hint: GT means greater than, so use it to compare if A > B [OK]
Common Mistakes:
  • Using LT instead of GT
  • Confusing EQ with GT
  • Using NE for greater than check
3. Given the following PLC code snippet:
VAR
  X: INT := 5;
  Y: INT := 10;
  Result: BOOL;
END_VAR

Result := X LT Y;

What will be the value of Result after execution?
medium
A. Syntax Error
B. FALSE
C. TRUE
D. Undefined

Solution

  1. Step 1: Understand the comparison LT

    LT means 'less than'. The code checks if X (5) is less than Y (10).
  2. Step 2: Evaluate the condition

    Since 5 is less than 10, the condition is true, so Result will be TRUE.
  3. Final Answer:

    TRUE -> Option C
  4. Quick Check:

    5 LT 10 = TRUE [OK]
Hint: LT means less than; 5 < 10 is true [OK]
Common Mistakes:
  • Thinking LT means greater than
  • Assuming Result is FALSE
  • Confusing syntax with error
4. Identify the error in this PLC comparison instruction:
IF A NEQ B THEN
  // do something
END_IF
medium
A. Missing THEN keyword
B. NEQ is not a valid comparison instruction
C. Comparison should be EQ instead of NEQ
D. IF statement missing parentheses

Solution

  1. Step 1: Check the comparison instruction used

    The code uses 'NEQ' which is not a standard PLC comparison instruction.
  2. Step 2: Identify correct instruction

    The correct instruction for 'not equal' is 'NE', not 'NEQ'.
  3. Final Answer:

    NEQ is not a valid comparison instruction -> Option B
  4. Quick Check:

    Use NE for not equal, not NEQ [OK]
Hint: Use NE for not equal, not NEQ [OK]
Common Mistakes:
  • Using NEQ instead of NE
  • Forgetting THEN keyword
  • Adding unnecessary parentheses
5. You want to write a PLC program that turns on a motor only if the temperature is greater than 50 and the pressure is less than 100. Which combination of comparison instructions correctly checks this condition?
hard
A. IF Temperature GT 50 AND Pressure LT 100 THEN Motor := TRUE;
B. IF Temperature LT 50 OR Pressure GT 100 THEN Motor := TRUE;
C. IF Temperature EQ 50 AND Pressure EQ 100 THEN Motor := TRUE;
D. IF Temperature NE 50 AND Pressure NE 100 THEN Motor := TRUE;

Solution

  1. Step 1: Translate the condition into comparisons

    The motor should turn on if temperature is greater than 50 (GT 50) and pressure is less than 100 (LT 100).
  2. Step 2: Check each option

    IF Temperature GT 50 AND Pressure LT 100 THEN Motor := TRUE; uses GT and LT with AND, matching the condition exactly. Other options use wrong comparisons or OR instead of AND.
  3. Final Answer:

    IF Temperature GT 50 AND Pressure LT 100 THEN Motor := TRUE; -> Option A
  4. Quick Check:

    GT and LT with AND matches condition [OK]
Hint: Use GT for greater, LT for less, combine with AND for both conditions [OK]
Common Mistakes:
  • Using OR instead of AND
  • Using EQ instead of GT or LT
  • Mixing NE with equality checks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard