Technical Article

Understanding PLC Program Commands: Comparison Instructions

July 06, 2022 by Jon Peterson

​​​​​​​We will show you how to recognize, utilize, and efficiently operate your comparison instructions in Rockwell Automation’s Studio 5000 and Automation Direct’s Productivity Suite to make your program flow smoothly.

See our previous articles covering PLC programming commands:

Timers

One-Shots

Up and Down Counters

Math Instructions

Trigonometry Instructions


Comparisons in Logic

The amount of comparisons done in industrial programs is remarkable. Even in small programs, we are continually comparing information to make decisions. Some comparisons are basic while others require long equations or complex data masking. In this article, we are going to start delving into some of the basic comparisons that can be done in Rockwell Automation’s Studio 5000 for Allen Bradley PLCs and Automation Direct’s Productivity Suite.

 

Data Types Used in Comparison Commands

Before we get started with the comparisons themselves, let’s discuss what it is we are comparing. In PLCs, we use a variety of different types of numbers. Sometimes we will need to compare decimal numbers (FLOATs or REALs), integers, shorts, and doubles (INTs, SINTs, and DINTs). It is also possible to compare the number of characters in string tags. 

While there are a variety of data types that can be compared, they do have to be numbers or specific specialty types like strings. In reality, every line of logic is a comparison, but boolean commands only result in two options: true or false. Each time a boolean command is used, it is already being compared to whether its value is true or false, so it’s not accurate to say that booleans cannot be compared, but rather that is their only purpose, so they cannot be used in the special comparison commands available in PLC programs.

 

Examples of Comparison Commands in PLC Ladder Logic

I mentioned previously that comparisons are used all over the place. Let’s now get specific and talk about what places these are. A simple comparison is if you need to know if two values are equal. If the equipment has four different positions and one task for each position, you can assign the tasks by using equal instructions. If ‘position’ equals 1, ‘task’ equals 1; if ‘position’ equals 2, ‘task’ equals 2; etc.

Now let’s get a bit more complicated. A sortation system is a good example for limit instructions. If the program has to sort widgets into trays, it might use a system that sorts them by weight. If the weight is between 16 oz and 31.99 oz, it goes into tray one. If it is between 32 oz and 63.99 oz, it goes into tray two. This sortation system would use two different limit comparisons to create a destination for the widget.

Now we are going to look at the basic comparison instructions for Rockwell Automation’s Studio 5000 and Automation Direct’s Productivity Suite.

For all of these commands, the ‘Source’ tags are of any integer, floating point, or string data type, or even constant values. The resulting output will be a boolean true/false value, so it can be used to activate a rung, as shown in the examples below.

 

Using the EQU and NEQ (Equal, Not Equal) Instructions

Equal or Not Equal. These are the most self-explanatory comparisons out there. For the EQU instruction, if Source A is equal to Source B, the output is activated for the rung. The NEQ instruction is exactly the opposite. If the two are not equal, the output is activated for the rung. If you have extra permissive instructions in the rung, then those must be true in order to make your output fire. My rungs in Figure 1 contain only the comparisons. 

 

EQU and NEW comparison commands

Figure 1. EQU and NEQ are opposite of one another. When values being compared are equal, the EQU goes true. When values being compared are not equal, the NEQ goes true. All images are provided by the author.

 

You might notice that my numbers are tags. The tags I am using are DINTs. If you were to compare a variable tag to a static number, then instruction would adapt to accept whatever type of number you plug in. If you put a decimal number in, the instruction will adapt to allow the REAL to be used. It can even compare a DINT to REAL even though the data types are different.

 

Using the LES and GRT (Less-Than, Greater-Than) Instructions

Moving on to more of the simple instructions—the LES and GRT instructions. For the LES, if Source A is less than Source B, the output will go true. For the GRT instruction, Source A must be greater than Source B. Don’t get your comparison direction confused though. Take a look at Figure 2 to see how these function. As with the EQU and NEQ, the instructions will adapt to any numeric data type you feed them and compare accordingly.

 

LES and GRT comparison commands

Figure 2. LES and GRT comparison commands: In LES, when Source A is less than Source B, the output goes true. In GRT, when Source A is greater than Source B, the output goes true.

 

Using the LEQ and GEQ (Less-Than/Equal, Greater-Than/Equal) Instructions

Here is the last set of straightforward instructions. LEQ and GEQ combine the previous instructions. For the LEQ, if Source A is less than or equal to Source B, the output goes true. For GEQ, if Source A is greater than or equal to Source B, the output will go true. Data type info still applies here as well. It’ll work with whatever numerics you give it.

 

LEQ and GEQ comparison commands

Figure 3. LEQ and GEQ comparison commands: In LEQ, when Source A is less than or equal to Source B, the output goes true. In GEQ, when Source A is greater than or equal to Source B, the output goes true.

 

Using the LIM (Limit) Instruction

Now we’re starting to get to something a little bit more fun. I’ve thrown two limit instructions below. A LIM instruction checks to see if the test tag is between the values of the ‘low limit’ and ‘high limit.’ If it’s between the limits, it goes true. I added the first instruction so you can visualize a test against two static limits. The second instruction uses a test against two variables. Two is a DINT tag that can be any whole number that will fit. Ten is another DINT tag. These variables can be useful if you have to test a number against dynamic parameters.

You might see variables used in a batching program. If Batch A is running, you might see a certain set of variables. When Batch B is started up, the new variables from the recipe manager drop in, and the new sortation can begin.

 

A LIM comparison instruction

Figure 4. A LIM instruction checks to see If the test tag is between the values of the ‘low limit’ and ‘high limit’ of two static limits; if so, the output goes true. 

 

Using the CMP (Compare) Instruction

The CMP instruction is the most dynamic of everything we’ve looked at so far. It can be as simple or complex as you make it. I have examples in Figure 5 of some basic comparisons, but there are far more complex things that they can do. For all the different operators, you can use Studio 5000’s help file and search for ‘valid operators.’ You will find the list—there are dozens of operators. You can make formulas using absolute values, multiplication, division, logarithms, etc.

As with everything else so far, you can use tags or constants as needed and the program adjusts for data types robustly.

 

CMP comparison command

Figure 5. CMP is a dynamic instruction that can be used with dozens of operators in both basic and complex comparisons.

 

Using the CMPV (Compare) Instruction

Now let’s examine many of the same expressions as used in the Productivity Suite software. Productivity uses a radically different method than Studio 5000. If you use the automatic data type generation, you have two inputs that must be assigned values. These values are then compared by the instruction and you are given a boolean output for whichever condition is true. Then, any combination of less, equal, or greater values are simply series and parallel arrangements of those boolean outputs. 

This instruction is formatted as an output instruction, as opposed to Studio 5000, which uses comparisons as inputs. 

If you don’t use the automatic data type generation, you can directly address data into and out of the comparison instruction. If Input 1 is ProductWeight, you can just plug that tag name right in. In this way, it is very convenient to use.

In this CMPV instruction, all integers, floating point numbers, or constant values are permissible, but not string values.

 

CMPV comparison command

Figure 6. Productivity’s CMPV instruction is used to compare two inputs with assigned values to see which value is greater than, equal to, or less than the other.

 

Using the CMPS (Compare String) Instruction

Another instruction that Productivity has is the ability to compare strings (text, characters, and numbers, like passcodes and user IDs). Put a string data type into Input1 and a string into Input2, and a number of characters to compare only a limited portion of the strings, if desired. The inputs may also be constant string values.

This scenario may be very useful if a badge ID is to be scanned with an RFID reader. Perhaps the last 5 characters may be ignored, while only the first 5 are compared to a database to confirm authorized entry into a gated area. The Number of Characters would be set to 5, and the resulting ‘Equal’ boolean output would confirm if the scanned badge matches the database value.

 

CMPS comparison command

Figure 7. CMPS instruction compares two strings to determine if they have the same value, partially or entirely. 

 

Review

Comparisons are everywhere throughout programs. Take a look over your programming software and learn to recognize, utilize, and efficiently operate your comparison instructions to make your program flow smoothly. As we saw comparing Studio 5000 to Productivity, there is a huge difference in the use of comparison instructions. Your software may be different as well. Don’t be afraid to dig into the help files to ensure you know how your comparisons function. They are a mandatory tool in the programmer’s toolbox.

 


Want to test your knowledge of PLCs? You think you N.O. a lot about ladder logic? Are you a normally-open or normally-closed minded engineer?

Check out our PLC Programming worksheet!

3 Comments
  • zurda10 July 13, 2022

    Hi Jon, may I ask you a question.

    I am finishing my FDP (Final Degree Project) which is about programming the automatic control of an industry line.
    At some point, I needed to compare if two REAL variables were equal (it was about a SetPoint of a machine and the current Position values) so I used the EQ operator, setting as sources both variables. When I ran the simulation that part of code did not work as expected.
    I was lucky because those variables had only one decimal number so what I did was to create auxiliary variables converting to INTEGRER those values multiplied by 10 and then use those variables instead of the main ones in the EQ operator. This way the process worked fine.
    My question is if that issue might have happened because the way REALS are saved in memory is different from the rest of numbers or if it might have been another reason because from what you explain in the article it should be possible to compare those two numbers correctly.

    Thanks for the article and sorry if there’s something not well explained in my comment. I’m not native English speaker.
    Carlos.

    Like. Reply
    • bourne327 July 13, 2022
      Carlos, I have done some testing and found the following results: Test Rig: Studio 5000 FW 33.11 CompactLogix 1769-L16ER Controller I am running an EQU instruction comparing two REALs. The instruction goes TRUE when REAL1 and REAL2 are 1.234. The instruction goes FALSE when the REAL1 is 1.234 and REAL2 is 1.235. It is possible your system is different though! Can you tell me what controller and programming software platform you are using? -Jon Peterson, Author
      Like. Reply
      • zurda10 July 15, 2022
        I was using CODESYS V3.5 SP18 Patch 2 and simulating a scene in a software called Factory I/O using computer as a virtual PLC. Thanks for your answer
        Like. Reply