Back to tutorials
PLC Instructions

PLC Programming Computational Mathematical Instructions – SUB | Subtraction

By
Vladimir Romanov
Table of Contents

Introduction

The SUB, also known as the Subtraction, is an instruction which will perform a mathematical subtraction of two values which may be integers or floats. This instruction is used quite frequently in PLC programming and is one of the most fundamental mathematical operations. It can be used to calculate the difference between two sensor values, the deviation from a setpoint and much more. At its core, the instruction will evaluate the difference between the two values specified in “Source A” and “Source B” and store the resulting outcome in the “Dest” or the destination register.

In this tutorial we will be looking at basic uses of this instruction as well as one example in which it may come in handy in a practical sense.

Example & Usage of SUB

Here’s a real-world scenario of an SUB instruction:

  1. A Micrologix 1100 Allen Bradley PLC is used to control a process.
  2. In rung 0000, a SUB  instruction is used to subtract a constant equal to 56 from an integer N7:0 and store the resulting value in the integer N7:1.
  3. Since the value of the integer is currently 100 and the value of the constant is 56, the result is evaluated to 44.
  4. In rung 0001, a SUB  instruction is used to subtract an integer N7:3 from an integer N7:2 and store the resulting value in the integer N7:4.
  5. Since the value of the integer is currently 100 and the value of the second integer is 56, the result is evaluated to 44.
  6. In rung 0002, a SUB instruction is used to subtract a float F8:1 from a float F8:0 and store the result in the float F8:2.
  7. The SUB instruction is preceded by an XIC tied to B3:0/0 which will prevent the SUB to execute until it’s set to HIGH.
  8. Since the value of the float is currently 242.45 and the value of the second float is 123.123, the result is evaluated to 119.327.

Programming example in RSLogix 500:

SUBSubtractInstructionRSLogix5000
  1. In rung 0003, we have three distinct branches. The first branch is used to start a certain process through an XIC instruction paired with an ONS instruction. The MOV Instruction transfers an integer from one register to another.
  2. The second branch of the same rung contains the same pair as above which indicates the completion of a certain batching process. The branch is also utilising the SUB instruction in order to remove a specific quantity of product from the target production run.
  3. Lastly, the final branch will make sure to stop the process once the target is achieved or exceeded through the use of an LEQ Instruction and an OTU Instruction.

Programming example in RSLogix 500:

SUBInstructionStudio5000ExampleTutorial

Outcome:

The SUB instruction can be used for simple mathematical computations which will result in a subtraction of two integers or floats. It’s an output instruction which means that it will only execute if all the preceding conditions are evaluated to true. The instruction will perform a subtraction of the value in the register specified in “Source B” from the value in the register specified in “Source A” and store the resulting value in the register specified in the “Dest”

The final rung within our example illustrates a simple batch process which is enabled through the use of the B3:0/1 boolean value. As the manufacturing process continues, the batches produced are being subtracted from the target amount until the process is finalized. Once that’s the case, the process is stopped and may be started again through an HMI, start button or otherwise.

Data Types Allowed for SUB

The SUB can be used to compare two values of identical types or mismatching types such as float and integer. These value can be INTs or FLOATs.

  • Integer – You may specify each operand to be of “Integer” type.
  • Float – You may specify each operand to be of “Float” type.
Back to tutorials

Related Tutorials

Advanced

PowerFlex 525 Parameter, Input and Output Programming Tutorial in RSLogix Studio 5000

The PowerFlex 525 series of Variable Frequency Drives from Allen Bradley is highly utilized and respected within the automation field. The drives are compact, versatile and easy to use. We’ve written an extensive tutorial on how to get started with these drives in a previous tutorial; it covers how to initialize the drive, connect it to an EtherNet/IP based PLC and how to set the right parameters. In this tutorial, we’re looking to expand on the concepts and demonstrate a way to access and utilize the multiple Input and Output options available on the drive. The reason you may want to utilize the IO is to save money on purchasing external components, integrate instrumentation into your drive and save floor space, wiring and cost. We’ve seen the need to integrate closely related sensors into a PowerFlex 525 application; this would be the right way to do it.

By
Vladimir Romanov
Advanced

Publishing MQTT Data Using Opto 22 groov EPIC PAC

MQTT is a communication protocol that has taken off in the IIoT community. It’s a light-weight, efficient protocol that works through a publisher / broker / subscriber model. It creates an easy way for field devices to communicate and retrieve data from a single location. In this article, we’ll go over MQTT and dive into an example of how you can publish data using a groov EPIC PAC and retrieve the data using a MQTT client.

By
Vladimir Romanov
Intermediate

HMI Design - Best Practices for Effective HMI Screens

HMI Design is the practice of building HMI screens that are intuitive to the end user, pleasing to the eye and are efficient to operate. As control systems within manufacturing are migrated from traditional push button designs to primarily operated from HMI displays, HMI Design has become trivial to any new installation.Although there is a wide range of HMI based systems, the basic principles of good design remain the same. Different industries may dictate different patterns of layout for their systems, but the good practices remain.

By
Vladimir Romanov