Meridian Works
ENTR
Open the simulator
Waking the Machines

Lesson 5

Numbers in a PLC: Data Types, Compare, MOVE and Scaling

INT, DINT and REAL, comparing quantities, two thresholds instead of one, arithmetic that overflows, MOVE, and scaling raw values to units.

From Usta's field manual in Meridian Works, a story-driven PLC course: chapter 5, “Numbers”. Bits told you whether something is so. This chapter is about how much.

How much

Everything you have built so far answered a question with two possible answers. Is the guard closed? Has the box taken its twelfth jar? Bits told you whether something is so.

This chapter is about how much. Seventy-one degrees. Four hundred and eighty litres. Once a number is on the rung, you can ask it questions no bit could answer.

Numbers come into a PLC by three routes.

Analog cards. A transmitter turns temperature, pressure or level into a current. The current is usually 4–20 mA. The card measures the current and hands the processor a whole number. That number is not degrees; it is a raw value.

Instructions that keep a value. A counter's count and a timer's elapsed time are numbers too. You have been making numbers since zone 3. You just did not look at them.

People. The operator panel, a setting knob on the cabinet door, a recipe left over from last Tuesday. Any number a person types, a person can type wrong. We will come back to that.

A bit is one wire. A number is a row of bits read together. Sixteen bits stand side by side. If sixteen are not enough, thirty-two.

There is one thing nobody tells an apprentice: a number carries no meaning by itself. 13824 does not mean "half a tank". Its meaning is in your head and in the comment column of the symbol table. It is nowhere else in the machine. So write it down. Typing Temp_C — boiler flow temperature, °C takes eight seconds. It saves the next person an afternoon.

A bit is an opinion. A number is a measurement. Do not treat them the same.

Types: the size of the box

Before you write a number down, you decide which box it will live in. That decision is called the data type. It is not paperwork.

Both platforms use the same basic types:

Type Size Range For
INT 16 bits, signed -32768 … 32767 levels in per cent, whole degrees, small counts
DINT 32 bits, signed about ± 2 thousand million totals, milliseconds, the middle of a multiplication
REAL 32 bits, floating point very wide, about 7 digits scaled measurements, division with a fraction

The differences:

  • Siemens also gives you WORD and DWORD for bit patterns. Card status and packed flags live in them. They are not quantities. Do not do arithmetic on them.
  • Rockwell has no WORD type. Bit patterns are kept in a DINT too, and you reach its bits as Tag.5. Rockwell's native type is DINT. In arithmetic, INT and SINT values are first converted to DINT. The loss happens when the result is written into a smaller tag.
-32768 0 32767 32767 + 1 INT — sixteen bits, signed. No warning at either end.

Now a story. Whoever lives through this once does not forget it.

In the plant's second year the jar line had a batch counter. It went up by one for every box. Its type was INT, and it kept the total for the year. The total was printed on the shift report. One morning in November the report showed minus thirty-two thousand seven hundred and something. Production had not gone backwards. The number had not fitted in its box and had come back from the other end. Add one to the biggest INT and you get the smallest INT. That is not a software bug. It is how arithmetic works in a box of fixed size.

The add box reported the overflow only with a flag. In Siemens that flag is the box's ENO output. In Rockwell it is the S:V overflow bit. Nobody was looking at that flag. A bookkeeper noticed eleven months later.

The rule: choose the type before the number. Ask what the biggest value could ever be. Think of the worst day, not today. What happens if somebody types 999 into the batch size? Then choose a box that holds it. If you are not sure, take the next size up. Four extra bytes have never cost anybody a shift.

One more warning, about REAL. REAL is not exact. It holds an approximate value of the number you meant. Sixty degrees can come back as 59.999996. So never ask a REAL whether it is equal to something. Ask whether it is above or below. Equality is for counts and states, not for measurements.

Asking about quantities

A comparison instruction is a contact that asks about a number instead of a bit.

There are six questions. Both platforms ask the same questions but spell them differently:

Question Siemens Rockwell
equal == EQU
not equal <> NEQ
greater > GRT
less < LES
greater or equal >= GEQ
less or equal <= LEQ

Siemens draws a comparison as a contact. Rockwell draws it as a box. Their place on the rung is the same: among the contacts. The comparison asks its question. Its answer is ANDed into the power flow like any other condition in series. Put a contact before it and both must be satisfied. Put a coil after it and the coil follows the whole series.

GEQ Level ≥ 60 Auto_Mode High_Alarm the box is one more condition in the series — AND, not a replacement

The figure shows the Rockwell spelling. Siemens asks the same question with a >= contact.

There is also one instruction that makes two comparisons in a single box. "Is this value between two limits?" is IN_RANGE in Siemens and LIM in Rockwell. Siemens also has OUT_RANGE, which asks the opposite. Rockwell has no equivalent. There you put two comparisons in parallel.

Each input can be a tag or a literal. That choice matters more than it looks.

If you write Level >= 60, you chose sixty at your desk. Changing it means opening the program and downloading to a running plant. If you write Level >= High_Limit, the limit is a number in memory. The operator changes it from the panel at three in the morning without calling you.

Rule of thumb: anything the process might argue about goes in a tag. Levels, temperatures, batch sizes, service intervals. Literals are for things that are physically fixed. A twenty thousand litre tank holds twenty thousand litres whoever is on shift.

There is one trap, and everybody falls into it once. Do not ask a measurement whether it is equal to a number. A level rising at two per cent a second passes sixty between two scans. What your program sees is 59 and then 61. Asking an analog reading for equality gives you a rung that works perfectly on the bench and fires once a month on the plant.

Measurements are asked greater or less. Equality is for things that step from one whole value to the next: a state number, a recipe number, a count of boxes.

Two thresholds, not one

Now comes the part of this chapter you will use every week.

You have a pump and a level. The first rung that comes to mind has one comparison and a coil: below fifty, run; at fifty or above, stop. Draw it, download it, and go and stand next to the contactor.

The level does not sit still. It drifts, its surface ripples, and the transmitter adds noise of its own. While the tank stays near fifty, the number crosses the threshold again and again. Every crossing is a start. Every start draws inrush current and makes an arc across the contact tips. Each time, the contactor's life gets a little shorter.

one mark two marks 50 60 40 pump pump nine starts while the level sits on the line one start, one stop, the same twelve seconds

Nine starts in twelve seconds. A contactor lasts about a million operations. That sounds like a lot, but nine starts every twelve seconds use it up quickly. The coil buzzes, the contacts weld, and on a hot afternoon the overload trips. Somebody replaces a good contactor and never asks why it failed.

The fix is two thresholds. One starts, the other stops. Between them is a gap where nothing changes:

  • below the low threshold → start
  • above the high threshold → stop
  • in between → carry on doing what you were doing

The last line is memory. You already know how to write memory: the seal-in from zone 1. The only difference is that comparisons stand where the buttons were.

( below the low threshold  OR  already running )  AND  below the high threshold  →  pump

Read it as a sentence. Run if the level is low or the pump is already running. But in either case, run only while the level is below the high threshold. The parallel branch is the memory. The high-threshold comparison in series is the stop.

The gap between the two thresholds is called the deadband, or hysteresis. Choosing it is not a programming decision. It is a maintenance decision, and it rests on two facts that have nothing to do with software.

From the process: how far may the level really swing? A header tank that must never run dry leaves less room than a sump.

From the data sheet: how many starts per hour can the contactor and motor take? Divide the usable volume between the two thresholds by the pump's flow rate. That gives you the shortest cycle. If it comes out at forty seconds and the motor allows at most six starts an hour, your thresholds are too close.

Too narrow and you are back to frequent starts. Too wide and the process swings further than anybody wanted. You should be able to defend the value in between. You should be able to say out loud why you chose it.

Every on/off control in this building wants two thresholds: the compressor, the sump pump, the burner, the chiller. If you catch yourself writing one threshold and an output, stop. Ask what happens when the measurement sits exactly on the line. It will sit there. All shift.

MOVE / MOV: putting a number somewhere

A move instruction copies one value into one place. Siemens calls it MOVE, Rockwell MOV. Its input (IN in Siemens, Source in Rockwell) is where the value comes from. It can be a tag or a literal. Its output (OUT or Dest) is where the value goes. The output must be a tag you are allowed to write. It must never be an input. The card keeps rewriting the input values. A value you move there is gone before anybody reads it.

It is the simplest instruction in this chapter, and it does four jobs:

  • Giving a starting value. A tank capacity, a default setpoint, a number the plant needs before the operator touches anything.
  • Recipes. If product 3 is selected, you move 480 into Target_Litres and 74 into Target_Temp. A shelf of recipes is really a row of move instructions.
  • Forcing a value. Holding a scaled reading at the top of its scale, or clearing a register on a recount.
  • Publishing. This one needs a habit of its own. It is described below.

The arithmetic boxes work the same way. ADD, SUB, MUL and DIV have the same names on both platforms. If you want a long formula in one box, use CALCULATE in Siemens and CPT in Rockwell.

Move and arithmetic boxes are actions. They run only while the rung feeds them power. They pass that power straight on to the right. So more boxes or a coil can follow them on the same rung. A box on a rung with no conditions runs every scan. That is what you want for a scaling calculation. It is what you do not want for a recipe.

Take this pattern for yourself. You will build it in the boiler room:

  1. Calculate the result in a scratch register wide enough for the worst case.
  2. Check the result: can this plant really do that?
  3. Only then move it into the register the rest of the program reads.

No rung after the calculation should ever see a half-finished or impossible number. Not for one scan. A working register is a promise to every rung that reads it. If a bad value lands there even briefly, some rung will read it on exactly the wrong scan.

Calculate wherever you like. Publish carefully.

Scaling: from raw values to units

The analog card does not know what it is measuring. It measures a current and reports a raw value.

On Siemens cards the full measuring range is 0 to 27648. The card can measure a little outside that range. So you may see values above 27648 too. On Rockwell the raw range depends on the module. On most Rockwell analog modules you can do the scaling in the module's own settings. Then the tag arrives already in engineering units.

Whichever platform you are on, turning the number into a unit a person can read is your job. It is called scaling. You will do it on every analog signal you connect.

0 13824 27648 0.0 60.0 120.0 × 120.0 ÷ 27648.0 raw counts from the card °C — what the operator reads

For a probe reading 0–120 °C on a card with a 0–27648 range, the sum is:

Temp_C  =  Raw_Temp  ×  120.0  ÷  27648.0

There are two traps in that one line.

The order. Divide first and the result collapses. In whole numbers 13824 ÷ 27648 is not 0.5 but 0. A whole number has nowhere to put the half. Multiply first and 13824 × 120 is 1 658 880. That does not fit in an INT either. You get an overflowed, meaningless number instead of a zero.

The type. The real fix is to do the work in REAL. In REAL a half is a half and a million is a million. Write the constants with a decimal point: 120.0, 27648.0. Write the result into a REAL tag. In this simulator, if any operand is a REAL, the whole operation is done in REAL. The type of the destination does not change that. Two whole numbers divide as whole numbers, even when the result goes into a REAL tag. On real platforms the details differ. In Siemens you choose the data type of a math box, and all its inputs take that type. If needed you first convert an INT to REAL with CONV. Rockwell has no conversion box. The conversion happens when the value is written into the tag. The rule is the same on both: if you want a fraction, do the arithmetic in REAL on purpose. If a display insists on a whole number, convert at the very last step, in one place.

The general formula is worth writing inside the cabinet door:

value = (raw - raw_min) × (hi - lo) ÷ (raw_max - raw_min) + lo

Both platforms give you ready-made instructions for it:

  • Siemens: NORM_X turns a raw value into 0.0 to 1.0 across its range. SCALE_X turns that fraction into engineering units. Together they are the whole analog input path.
  • Rockwell: SCP does the same job in one box. Not every controller has it. Where it is missing, you write the formula with CPT.

On a 4–20 mA transmitter raw_min is not zero. If the channel is set up as 0–20 mA, 4 mA reads about 5530 raw. You subtract raw_min first, so that zero really means zero. If the channel is set up as 4–20 mA, the card does that subtraction itself and reads 0 at 4 mA. Find out which from the hardware settings; do not guess.

A live zero has one more benefit: a free broken-wire detector. A healthy transmitter never goes below 4 mA. If the reading falls far below 4 mA, somebody has cut the cable.

The card tells you where the needle is. Only you know what the dial says.

When the arithmetic bites

Two things go wrong with numbers, and both are quiet.

Division by zero. The divisor comes from a tag. One morning that tag is zero. Maybe the processor has just powered up. Maybe nobody loaded the recipe. Maybe the operator stopped halfway through typing. In this simulator the CPU does not stop for it. The instruction writes zero into the destination, sets the overflow flag, and the scan carries on. Real CPUs do not stop for it either. In Siemens the box's ENO output goes to 0. In Rockwell the S:V overflow bit is set and the controller logs a minor fault. Do not trust what is left in the destination.

Do not be relieved that the program kept running. Zero is a perfectly reasonable-looking answer. A reasonable-looking wrong answer is worse than a fault, because somebody investigates a fault. So guard the divisor. Put a comparison in series in front of the divide box: is the divisor not zero, or greater than zero? Let the division run only with a divisor that makes sense. One comparison removes a lot of three-in-the-morning phone calls.

Overflow. You saw it on page two: add one to the biggest INT and you get the smallest INT. Overflow usually happens in multiplication. Multiplication makes big numbers out of small ones without any warning. 900 × 50 is 45 000. That is a small number for a person, but it does not fit in an INT. Rockwell does the arithmetic in DINT. Even so, the same loss happens when you write the result into an INT tag.

Two habits protect you. First: calculate in a register wider than you think you need. A DINT is four bytes and holds up to two thousand million. Second: put a limit check between the calculation and the register the plant uses. Catch an impossible value where it is made. Do not let it be read as true forty rungs further down.

Every number a person types gets a range check. Always. Not because operators are careless. Because the keyboard does not know how many litres your tank holds. When somebody's thumb rests on the 9 key, you want to see a lamp, not a full vessel.

The processor does what you told it. A hundred times a second, without tiring, including the arithmetic you did not check. It will not notice the mistake. You will.