Skip to content

05Data and variables

Data types: BOOL, INT, REAL, TIME and the rest

Every ST type has a size and a range, and the wrong choice gives you a silent overflow or a lost decimal.

Lesson 5 of 3411 minIEC 61131-3

Why the type matters

In a PLC, memory is limited and time is counted, so every variable takes exactly its size. Choosing a type that is too small gives you a silent overflow: the counter reaches its limit and wraps to negative with no error at all.

Boolean and numeric types

Type

Size

Range

Used for

BOOL

1 bit

TRUE / FALSE

a contact, a valve, an alarm

BYTE WORD DWORD

8 / 16 / 32 bit

bit patterns

status words, masks

SINT INT DINT

8 / 16 / 32 bit

-128…127, -32768…32767, ±2.1 billion

counters, steps

USINT UINT UDINT

unsigned

0…255, 0…65535, 0…4.29 billion

raw counts

REAL

32 bit

±3.4×10³⁸, about 7 digits

measurements, maths

LREAL

64 bit

about 15 digits

precise maths

TIME

32 bit

a duration

PT on timers

STRING

by length

text

messages, batch names

Trap one: INT overflows

INT stops at 32767. A production counter of that type wraps to -32768 after 32767 parts. Use DINT for anything that accumulates.

Trap two: REAL is not exact

REAL is approximate: 0.1 + 0.2 is not exactly 0.3. So never compare two REALs for equality:

IF Level = 50.0 THEN   (* may never be true *)
IF ABS(Level - 50.0) < 0.1 THEN   (* do this instead *)

TIME

TIME is not an ordinary number; it is a duration with its own literal form:

T#500ms    T#2s    T#1m30s    T#2h15m

You cannot assign 5000 to a TIME variable; it has to be T#5s.

Choosing in practice

  • an analogue field signal → REAL
  • a state machine step → INT
  • a batch counter or running hours → DINT
  • anything on/off → BOOL

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 Bar and Step in the watch table: Step keeps only the whole number of Bar.
  2. Change Counter from DINT to INT and download again: it still works here, but on a real PLC it would wrap negative after 32767.
  3. Try Delay : TIME := 5000; and the compiler refuses it: a duration is written T#5s.

Terms in this lesson

People find this lesson by searching

plc data typesstructured text data typesINT vs DINTREAL vs LREAL plcTIME literal structured textinteger overflow plcBOOL WORD DWORD

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