PLC Math Instructions: ADD, SUB, MUL, DIV in Ladder Logic Explained

Share:
DCS and Automation
PLC Math Instructions: ADD, SUB, MUL, DIV in Ladder Logic

PLC math instructions let a Ladder Logic program perform arithmetic directly on register values. Without them, a PLC can only switch outputs on and off.

With them, it can calculate totals, convert sensor signals, compute flow rates, and check engineering limits. These PLC math instructions are essential for any calculation beyond simple on/off control.

This guide covers the four core PLC math instructions -- ADD, SUB, MUL, DIV -- with rung examples, data type rules, overflow handling, and a live PLC math instruction calculator.

ADD Instruction SUB Instruction MUL Instruction DIV Instruction

PLC math instructions execute only when the rung condition is true. The result goes into a destination register. Data type, register size, and overflow behaviour must be understood before writing any arithmetic rung.

PLC Math Instructions in Ladder Logic

Hello everyone! Today we are going to look at something really practical -- how to do arithmetic inside a PLC program using Ladder Logic. Whether you need to add up a batch count, subtract a tare weight, multiply a sensor value, or divide a total by a sample count, these four PLC math instructions handle it all. Let us go through each one step by step.
PLC Math Instructions

Ladder Logic was originally designed for relay replacement. It handles contact logic brilliantly.

But process control needs numbers. That is where PLC math instructions come in -- they bring arithmetic into the Ladder Logic world.

These four PLC math instructions -- ADD, SUB, MUL, and DIV -- are the building blocks for all arithmetic in Ladder Logic. Click any instruction name to see what it does.

ADD (Addition): Adds Source A and Source B together and places the result in the Destination register. Source values can be constants or register addresses. The Destination must be a writable register. ADD runs every scan when the rung is true.
SUB (Subtraction): Subtracts Source B from Source A (Result = Source A minus Source B). Used for calculating differences, error values, remaining quantities, and tare corrections. The order of the sources matters.
MUL (Multiplication): Multiplies Source A by Source B and places the result in the Destination. Watch out for register overflow -- multiplying two 16-bit integers can easily produce a result larger than a 16-bit register can hold. Use 32-bit or DINT registers for large results.
DIV (Division): Divides Source A by Source B (Result = Source A / Source B). Integer division truncates the result -- it does not round. Dividing 7 by 2 gives 3, not 3.5. Use REAL (floating point) registers for fractional results.
4
Core PLC math instructions: ADD, SUB, MUL, DIV
Every scan
A math instruction executes every scan the rung condition is true
DINT
Use 32-bit DINT registers to prevent overflow in multiplication results
REAL
Use REAL (float) for fractional division results -- INT truncates decimals
Advertisement

The 4 PLC Math Instructions Explained with Ladder Rung Examples

ADD -- Addition Instruction

Result = Source A + Source B IEC: ADD_INT, ADD_DINT, ADD_REAL

ADD reads the values in Source A and Source B, adds them together, and writes the result to the Destination register. Like all PLC math instructions, ADD only executes when the rung condition is true.

The Destination register must be a different address from the Source registers in most platforms, though some allow in place addition. Both Sources can be either register addresses or immediate constant values.

(* Add batch count to running total on each cycle trigger *) |--[BatchDone]------------------------+[ADD]------------------------| | | Source A: BatchCount (INT)| | | Source B: RunningTotal(INT)| | | Dest: RunningTotal(INT)|

Practical use of the ADD PLC math instruction: accumulating batch counts, adding a trim offset to a setpoint, summing energy readings from multiple sub-meters. See the PLC counter instructions article for how counters use ADD internally.

When using ADD with INT registers (16-bit signed), the maximum result is 32,767. Adding 20,000 and 15,000 overflows the register and produces a wrong result. Use DINT (32-bit signed, max 2,147,483,647) whenever accumulated values may exceed 32,767.

SUB -- Subtraction Instruction

Result = Source A minus Source B IEC: SUB_INT, SUB_DINT, SUB_REAL

The SUB PLC math instruction subtracts Source B from Source A. The order matters: Source A is the minuend (the number being subtracted from). Source B is the subtrahend (the number being taken away).

A negative result is valid in a signed register. If you subtract a larger number from a smaller one, the Destination will hold a negative value. Unsigned registers (UINT) cannot hold negative values and will wrap around, producing a large positive result instead.

(* Calculate remaining batch quantity *) |--[Enable]--------------------------+[SUB]------------------------| | | Source A: TargetQty (DINT)| | | Source B: FilledQty (DINT)| | | Dest: Remaining (DINT)|

Practical use: calculating remaining quantity in a batch, computing error (setpoint minus process variable), finding a tare-corrected weight (gross weight minus tare weight). The error term in a manual PID calculation uses SUB for the SP minus PV step.

MUL -- Multiplication Instruction

Result = Source A times Source B Watch: result can be twice as wide as inputs

MUL multiplies Source A by Source B and places the result in the Destination. Multiplication is where overflow becomes a real risk.

Multiplying two 16-bit INT values can produce a result up to 32,767 squared -- just over one billion. A 16-bit register cannot hold this.

Always use a DINT or REAL destination register when multiplying INT values, or when the result may exceed the source register size.

(* Convert raw 0-4095 ADC count to engineering units *) |--[AlwaysOn]------------------------+[MUL]------------------------| | | Source A: RawADC (REAL)| | | Source B: 100.0 (REAL)| | | Dest: Scaled (REAL)| | +[DIV]------------------------| | | Source A: Scaled (REAL)| | | Source B: 4095.0 (REAL)| | | Dest: EngValue (REAL)|

Practical use: scaling raw ADC counts to engineering units, converting between measurement units, calculating flow rate from pulse count and K factor.

See the flow meter K factor guide and the PLC memory addressing guide for related detail.

DIV -- Division Instruction

Result = Source A / Source B Integer: truncates. REAL: gives decimal.

The DIV PLC math instruction divides Source A by Source B. Two rules are critical: the data type of the Destination determines whether the result is truncated or fractional, and division by zero causes a fatal fault on most PLC platforms.

Integer division truncates toward zero. For a PLC math instruction using INT: 7 / 2 = 3, and 9 / 4 = 2.

If you need the fractional part, convert both sources to REAL before dividing and use a REAL Destination register.

(* Calculate average temperature from sum and count *) |--[SampleDone]---------------------+[DIV]------------------------| | | Source A: TempSum (REAL)| | | Source B: SampleCnt(REAL)| | | Dest: AvgTemp (REAL)|

Division by zero protection: always check that Source B is not zero before a DIV instruction.

Use a compare rung (GT or NEQ) to gate it with a contact. If Source B can ever be zero, the DIV must be protected or the PLC will fault.

(* Protect against division by zero *) |--[SampleCnt NEQ 0]----------------+[DIV]------------------------| | | Source A: TempSum (REAL)| | | Source B: SampleCnt(REAL)| | | Dest: AvgTemp (REAL)|
Advertisement

PLC Math Instruction Data Types and Overflow Rules

Data TypeSizeRangeUse WithRisk
INT (Integer)16-bit signedminus 32,768 to 32,767ADD, SUB with small valuesOverflow if result exceeds 32,767
UINT (Unsigned Int)16-bit unsigned0 to 65,535ADD with positive values onlyCannot hold negative results from SUB
DINT (Double Int)32-bit signedminus 2,147,483,648 to 2,147,483,647ADD, SUB, MUL for large valuesStill overflows if MUL result exceeds 2.1 billion
REAL (Float)32-bit IEEE 754approx. plus/minus 3.4 x 10 to the 38DIV for fractional results, scaled engineering valuesPrecision limited to 7 significant digits; compare REAL values with tolerance, not exact equality
LREAL (Long Float)64-bit IEEE 754approx. plus/minus 1.8 x 10 to the 308High-precision scientific calculationsNot supported on all PLC platforms
On Rockwell platforms (Allen Bradley), INT is 16-bit and DINT is 32-bit. On Siemens TIA Portal, INT is 16-bit and DWORD is 32-bit unsigned, DINT is 32-bit signed. Always check your platform's data type definitions before writing PLC math instructions. The names are standardised in IEC 61131-3 but some platforms use legacy naming conventions.

Worked Example: PLC Math Instruction Chain for Scaling a 4 to 20 mA Signal

A pressure transmitter outputs 4 to 20 mA for 0 to 100 bar. The PLC reads this as a raw ADC count from 0 to 4095. The PLC math instruction chain below converts the raw count to bar.

Scale Formula: Pressure = (RawADC minus Offset) x Span / ADC_Max

Step 1 SUB: RawADC (0-4095) minus 0 (offset for 4mA zero) = AdjustedCount Step 2 MUL: AdjustedCount x 100.0 (bar span) = ScaledValue Step 3 DIV: ScaledValue / 4095.0 (ADC max) = PressureBarExample with RawADC = 2048 (mid-scale, about 12 mA): Step 1: 2048 minus 0 = 2048 Step 2: 2048 x 100.0 = 204,800.0 Step 3: 204,800.0 / 4095.0 = 50.01 barCheck: mid-scale ADC should give 50 bar. Result: 50.01 bar. PASS.
Result: 50.01 bar at mid-scale ADC count -- correct within rounding.
On many PLC platforms, the ADC scaling is handled by an analogue input module that delivers a pre-scaled integer or REAL value directly to a register. When this is available, use it. The manual SUB/MUL/DIV chain above is needed when the module delivers a raw count, or when custom engineering unit conversion is required beyond the module's built in scaling range.

PLC Math Instruction Result Calculator

PLC Math Instruction Simulator
Check result, data type overflow, and register fit before writing your rung
-
-

PLC Math Instructions: Full Comparison Table

InstructionOperationResult Register TypeKey RiskCommon Application
ADDA + BSame as inputs or largerOverflow if sum exceeds register sizeAccumulating totals, adding offsets, summing sub-meter readings
SUBA minus BSigned (allows negative)Negative result in unsigned register wraps aroundError calculation, remaining quantity, tare subtraction
MULA x BUse DINT or REALResult can be much larger than either inputUnit conversion, scaling ADC counts, flow calculation
DIVA / BREAL for fractional resultDivision by zero causes major faultAveraging, rate calculation, percentage calculation

Real-World Applications Using PLC Math Instructions

Batch Totalising

ADD accumulates the batch count each completed cycle.

A SUB rung subtracts the total from the target to give remaining quantity. When the SUB result reaches zero, a compare triggers the end of-batch output. Uses the same register principles as PLC counter instructions and the shift register.

Engineering Unit Scaling

MUL and DIV convert raw ADC input counts to engineering units (bar, °C, m3/h). The formula requires multiplying by the engineering span and dividing by the ADC full scale count. REAL registers are used throughout to preserve fractional values.

Timer Setpoint Calculation

A MUL PLC math instruction converts a setpoint entered in minutes to milliseconds (multiply by 60,000) for use as a timer preset. See PLC timer instructions for how the preset value is used in TON and TOF timers.

Average Calculation

ADD accumulates samples into a sum register. A DIV PLC math instruction divides the sum by the sample count to give the rolling average.

The sample count must be checked NEQ 0 before the DIV runs. Used for flow averaging and temperature smoothing.

Watch: PLC Math Instructions in Ladder Logic

Advertisement

PLC Math Instructions Questions

When does a PLC math instruction execute?
Every scan the rung condition is true. Use a rising-edge trigger contact if the calculation should run only once per event, not continuously on every scan.
What happens if the result of a PLC math instruction overflows the register?
The value wraps around silently. An INT register maximum is 32,767. If ADD produces 33,000 the register wraps to minus 32,536 -- wrong, with no fault warning. Always size the Destination to fit the expected result range.
Why does DIV give the wrong answer with INT registers?
Integer division truncates toward zero. 7 DIV 2 gives 3, not 3.5. Convert both source values to REAL before dividing and use a REAL destination register to get the fractional result.
How do I prevent a division by zero fault in a PLC program?
Add a compare contact (NEQ 0 or GT 0) before the DIV rung. The DIV only executes when the contact is true, which protects the rung when the divisor is zero.
Can I chain multiple PLC math instructions on the same rung?
No. Each PLC math instruction occupies one rung. Chain them by using the Destination of one rung as the Source of the next, passing results through intermediate registers.

External References

Advertisement

What We Learn Today

  • PLC math instructions (ADD, SUB, MUL, DIV) execute every scan the rung condition is true
  • ADD: result = A + B. Use DINT when accumulated values may exceed 32,767
  • SUB: result = A minus B. Order matters. Negative results need a signed register
  • MUL: result = A x B. Use DINT or REAL destination -- products can far exceed input size
  • DIV: result = A / B. Integer truncates -- use REAL for fractional results. Always guard against division by zero
  • Chain PLC math instructions rung by rung using intermediate registers -- never on the same rung
“A PLC math instruction is only as reliable as the data type you chose for the result. Pick the wrong register size and the number wraps -- silently, without a fault, and at the worst possible moment.”

Leave a Reply

Your email address will not be published. Required fields are marked *