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.
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 |
|---|---|---|---|
| 1 bit | TRUE / FALSE | a contact, a valve, an alarm |
| 8 / 16 / 32 bit | bit patterns | status words, masks |
| 8 / 16 / 32 bit | -128…127, -32768…32767, ±2.1 billion | counters, steps |
| unsigned | 0…255, 0…65535, 0…4.29 billion | raw counts |
| 32 bit | ±3.4×10³⁸, about 7 digits | measurements, maths |
| 64 bit | about 15 digits | precise maths |
| 32 bit | a duration | |
| 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#2h15mYou 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
- Run the program, press START and watch Bar and Step in the watch table: Step keeps only the whole number of Bar.
- Change Counter from DINT to INT and download again: it still works here, but on a real PLC it would wrap negative after 32767.
- 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
- 01What is Structured Text, and when to use it instead of ladder
- 02How a PLC runs your code: the scan cycle and the process image
- 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
- 04Your first program: starting a pump with seal-in logic
- 05Data types: BOOL, INT, REAL, TIME and the rest
- 06Declaring variables: VAR, VAR_INPUT and RETAIN
- 07Literals and constants: 16#FF, 2#1010 and T#5s
- 08Type conversion and scaling a 4-20 mA signal
- 09Assignment and arithmetic in ST: := and operator precedence
- 10Boolean logic, comparisons and bit operations
- 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
- 12IF, ELSIF and ELSE: the rules and when to avoid them
- 13The CASE statement and your first state machine
- 14The FOR loop and the watchdog
- 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
- 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
- 17Timers: TON, TOF and TP
- 18Counters: CTU, CTD and CTUD
- 19SR and RS flip-flops: which one wins
- 20Writing your own function
- 21Writing a function block
- 22Arrays in Structured Text
- 23Structs and enumerations
- 24References and pointers
- 25The state machine pattern: a batch sequence that cannot get stuck
- 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
- 27Analogue signal processing: counts to engineering units, NE43 and filtering
- 28Closed-loop control in ST: hysteresis first, then a practical PID
- 29Motors and valves in practice: permissives, feedback and travel time
- 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
- 31Testing and debugging ST: the watch table, forcing and simulation
- 32An ST style guide: naming, comments, constants and version control
- 33Final project: a complete tank station with a state machine and alarms
- 3420 interview questions on ST and PLC programming, with answers