Skip to content

08Data and variables

Type conversion and scaling a 4-20 mA signal

INT_TO_REAL, TRUNC and ROUND, and the formula that turns a raw count from the input card into an engineering unit.

Lesson 8 of 3412 minIEC 61131-3

Why conversion is needed

An analogue input card does not hand you "73.4 percent". It hands you a raw count: 0…27648 on Siemens, 0…32767 on other cards. Turning that into something an operator understands is your job.

Explicit conversion

The standard wants conversions spelled out:

Scaled := INT_TO_REAL(Raw);
Steps  := REAL_TO_INT(Position);   (* rounds to nearest *)
Whole  := TRUNC(Position);         (* cuts the fraction off *)

Note that TRUNC(3.9) is 3 while REAL_TO_INT(3.9) is 4. That difference is a common source of one-step position errors.

The classic trap: integer division

Percent := Raw / 27648 * 100;      (* wrong: the result is zero *)

Raw and 27648 are both integers, so the division is integer and gives 0 before the multiplication. Convert first, or write the constants as reals:

Percent := INT_TO_REAL(Raw) / 27648.0 * 100.0;   (* correct *)

The scaling formula

For any raw range [RawLo…RawHi] and engineering range [EngLo…EngHi]:

Eng := (INT_TO_REAL(Raw) - RawLo) / (RawHi - RawLo) * (EngHi - EngLo) + EngLo;

Example: a 4-20 mA level transmitter spanning 0-5 m, on a card that gives 0…27648 for 0…20 mA. At 4 mA the raw count is about 5530, so:

Meters := (INT_TO_REAL(Raw) - 5530.0) / (27648.0 - 5530.0) * 5.0;

Always clamp

Sensors go out of range. Clamp the result so the operator never sees "-3 metres":

Meters := LIMIT(0.0, Meters, 5.0);

Better still, know why it went out: below 3.6 mA or above 21 mA means a sensor fault under NAMUR NE43, which is lesson 27.

A ready-made tool

The workshop on this site has a 4-20 mA and loop calculator: enter the range and the current and it gives you the engineering value and the loop voltage budget.

Try it in the lab

The lab runs in your browser: an ST editor, a virtual process that suits the lesson, the same program as a live ladder diagram, a watch table, and export to Siemens SCL.

What to try

  1. Run the program, press START and watch mA, Raw and Meters follow the level.
  2. At 50% you should see 12 mA, 13824 counts and 2.5 m.
  3. Change EngHi to 10.0 and download again: the metre reading doubles, so the pump stops earlier at the same 4.5 limit.

Terms in this lesson

People find this lesson by searching

4-20ma scaling plcscale raw counts to engineering unitsINT_TO_REAL structured textTRUNC vs ROUND plctype conversion structured textinteger division trap plcanalog input scaling formula

Course outline

  1. 01What is Structured Text, and when to use it instead of ladder
  2. 02How a PLC runs your code: the scan cycle and the process image
  3. 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
  4. 04Your first program: starting a pump with seal-in logic
  5. 05Data types: BOOL, INT, REAL, TIME and the rest
  6. 06Declaring variables: VAR, VAR_INPUT and RETAIN
  7. 07Literals and constants: 16#FF, 2#1010 and T#5s
  8. 08Type conversion and scaling a 4-20 mA signal
  9. 09Assignment and arithmetic in ST: := and operator precedence
  10. 10Boolean logic, comparisons and bit operations
  11. 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
  12. 12IF, ELSIF and ELSE: the rules and when to avoid them
  13. 13The CASE statement and your first state machine
  14. 14The FOR loop and the watchdog
  15. 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
  16. 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
  17. 17Timers: TON, TOF and TP
  18. 18Counters: CTU, CTD and CTUD
  19. 19SR and RS flip-flops: which one wins
  20. 20Writing your own function
  21. 21Writing a function block
  22. 22Arrays in Structured Text
  23. 23Structs and enumerations
  24. 24References and pointers
  25. 25The state machine pattern: a batch sequence that cannot get stuck
  26. 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
  27. 27Analogue signal processing: counts to engineering units, NE43 and filtering
  28. 28Closed-loop control in ST: hysteresis first, then a practical PID
  29. 29Motors and valves in practice: permissives, feedback and travel time
  30. 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
  31. 31Testing and debugging ST: the watch table, forcing and simulation
  32. 32An ST style guide: naming, comments, constants and version control
  33. 33Final project: a complete tank station with a state machine and alarms
  34. 3420 interview questions on ST and PLC programming, with answers