Learn › Instructions
Compare instructions: EQ, NE, LT, GT, LE, GE, LIMIT and CMP
Compare instructions are conditions, like contacts, but instead of reading a single bit they test numbers: is the count at 10 yet, is the level above the limit, is the temperature inside its band.
How compare instructions work
A compare instruction goes in the condition part of a rung, anywhere a contact could go. It reads its operands every scan, does its test, and passes power on only if the test is true and power is coming into it from the left. So a compare in series with contacts is just another AND condition (see AND, OR and NOT in ladder logic).
Operands can be a number you type (10, 2.5, -3, or a based number like 16#FF), a DINT or REAL tag, a BOOL tag (which reads as 0 or 1), or a timer or counter word such as T1.ACC or C1.PRE. A whole timer or counter tag (just T1) isn't a number, so it can't be compared directly; pick its .ACC or .PRE. If an operand can't be read, for example a misspelled tag, the instruction is simply false.
The six basic compares
Each of these takes two operands, Source A and Source B.
| Instruction | Name | True when |
|---|---|---|
EQ | Equal | A = B |
NE | Not Equal | A ≠ B |
LT | Less Than | A < B |
GT | Greater Than | A > B |
LE | Less Than or Equal | A ≤ B |
GE | Greater Than or Equal | A ≥ B |
| BatchDone |
|----[GE A Count.ACC B 12]----------------( )-------|
Prefer GE and LE over EQ for "has it reached" tests. If a value can jump past your target (a total that goes up by 5, a timer accumulator that moves several milliseconds per scan), an EQ might never see the exact value and never go true. GE catches it whether it lands on the target or overshoots.
Be careful with EQ on REAL values. Decimal fractions aren't stored exactly, so a calculation you'd expect to give 0.3 can give 0.30000000000000004, and EQ says false. Compare REALs with a range instead (LIMIT, or GE and LE together).
Example: stages from one timer
Compares let one timer drive several steps. Run a single TON with a 30 second preset, then compare its accumulator (which counts in milliseconds) to switch outputs at different times:
| Stage1 |
|----[LT A Seq.ACC B 10000]-------------------( )----|
| Stage2 |
|----[GE A Seq.ACC B 10000]----[LT A Seq.ACC B 20000]----( )----|
| Stage3 |
|----[GE A Seq.ACC B 20000]-------------------( )----|
The two compares in series on the middle rung make a "between" test. That pattern is so common it has its own instruction.
LIMIT: Limit Test
LIMIT has three operands: Low Limit, Test and High Limit. When Low is less than or equal to High, it's true while Test is inside the band, including the limits themselves: Low ≤ Test ≤ High.
| InRange |
|----[LIMIT Low 10000 Test Seq.ACC High 19999]----( )----|
If you enter a Low Limit that's greater than the High Limit, LIMIT flips and tests for outside the band instead: it's true when Test ≥ Low or Test ≤ High. With Low 80 and High 20, it's true for values of 80 and up and 20 and below. Real Allen-Bradley PLCs behave the same way, and it's a neat way to raise an alarm when a value drifts too far either way.
MEQ: Masked Equal
MEQ compares only some of the bits of two integers. It has Source, Mask and Compare operands, and is true when Source AND Mask equals Compare AND Mask. The mask picks which bits matter. For example, with Mask 16#0F only the lowest four bits are compared, so MEQ Source Status Mask 16#0F Compare 3 is true whenever the low four bits of Status equal 3, whatever the other bits are. It's mostly used with status words packed full of separate flags.
CMP: Compare with an expression
When a test doesn't fit the basic instructions, CMP takes a whole expression and is true when the expression is true. For example:
Level * 2 >= Max(Count MOD 5) = 0(true on every fifth count)ABS(Setpoint - Actual) < 1.5
You can use + - * /, MOD, ** (power), parentheses, the comparisons = <> < <= > >=, and AND, OR, XOR and NOT. Functions include ABS, SQRT, SIN, COS, TAN, LN, LOG, DEG, RAD and TRN (truncate). Note that "not equal" is written <>, as on a real PLC, and equality is a single =.
Expressions are checked when you press Run, like Verify in a PLC editor. If one doesn't make sense (a missing bracket, or a tag that doesn't exist), the program won't start and the Playground tells you which rung has the problem. Expressions can be up to 120 characters long.
CMP is flexible, but a rung of simple GE and LT boxes is usually easier for the next person to read. Save CMP for genuinely mathematical tests.
IsINF and IsNAN
Two more compares check REAL values for results that aren't ordinary numbers. Dividing a REAL by zero gives infinity, and some operations (like zero divided by zero) give "not a number". IsINF is true when its Source is infinite, and IsNAN when it's NaN. Use them to catch bad maths before the result is used to drive something.
Try it in the Playground
- Open the Playground and add a TIMER tag
Seq(preset 30 seconds) and BOOL tagsStage1,Stage2andStage3. - Rung 1:
XIO Seq.DNdrivingTON Seq, so the timer restarts itself every 30 seconds. - Add the three stage rungs from the example above, using
LIMITfor the middle one. - Run it and watch the stage bits take turns in the Tags panel. Try changing the timer preset while it runs and see how the stages respond.
Key points
- Compares are conditions: they go before the outputs and AND with whatever is in series with them.
- Use
GE/LEfor "reached" tests and ranges for REALs;EQcan miss. - Timer
.ACCand.PREare in milliseconds, even though you type presets in seconds. LIMITwith Low above High tests outside the band.