Variables, data types, and assignment in Structured Text

The Core Idea

A variable is a named place to store a value. Its data type defines what it can represent, and an assignment calculates a result and writes it into that variable.

What you will learn

  • Declare a variable with a name, a type, and an optional starting value.

  • Choose BOOL, INT, or REAL from what a value means.

  • Read := as "calculate the right side, then store it in the variable on the left".

Why it matters

A calculation depends on how its working values are stored. Clear declarations and assignments keep the intended meaning from being lost between steps.

The problem

A bottle-filling station shows how much liquid one full case holds. The operator enters the fill per bottle in millilitres, and the program reports the case volume in litres.

SignalTypeMeaning
FILL_SP_MLREALFill per bottle, entered by the operator, in mL. Starts at 1250.0 mL.
BOTTLES_PER_CASEINTBottles packed in one case. Starts at 12.
CASE_VOLUME_LREALLiquid in one full case, in L.

The program first converts the fill to litres and keeps it in a working variable of its own, FILL_L. The program's VAR block declares FILL_L as an INT. The lesson simulator scans every 100 ms.

See it go wrong

Now enter 500 mL, then 2000 mL, on the human-machine interface (HMI). Compare each case volume with what 12 bottles should hold.

Fix it

Change one declaration so the case volume is right for every fill, then run the checks. Editing pauses the program, so press Run to watch your version live.

Passing the checks records Structured Text progress on the shared numeric-types concept.

What happened and why

The division was never the problem: FILL_SP_ML / 1000.0 gives 1.25. The assignment then stores that result in FILL_L, converted to FILL_L's declared type. This simulator keeps only the whole-number part of a value stored in an INT, so 1.25 became 1 and 0.5 became 0. Only 2000 mL, exactly 2 L, survived. Declaring FILL_L as REAL changed what the same assignment could store.

Declarations: name, type, starting value

Written out in full, this station's variables look like this:

VAR_INPUT
    FILL_SP_ML : REAL := 1250.0;     (* fill per bottle, mL *)
    BOTTLES_PER_CASE : INT := 12;    (* bottles in one case *)
END_VAR
VAR_OUTPUT
    CASE_VOLUME_L : REAL;            (* liquid in one case, L *)
END_VAR
VAR
    FILL_L : REAL;                   (* fill per bottle, L *)
END_VAR

Each line reads name : type := starting value ;. The starting value is optional: without one, a BOOL starts FALSE and an INT or REAL starts at 0. VAR_INPUT holds values the program reads, VAR_OUTPUT holds results it writes, and VAR holds its own working values.

In this lesson the inputs and outputs are supplied as the Tags list, so the program declares only FILL_L. Variables a program declares this way are not listed in Tags, so you see FILL_L through the case volume. In ST Studio, a variable comes from the tag tray or a declaration block. Tab moves through the fields of an inserted snippet and indents code; it does not create variables.

Choosing a type

TypeHoldsUse it for
BOOLTRUE or FALSESwitches, lamps, commands, permissives
INTWhole numbers from −32768 to 32767Counts, quantities, step numbers
REALNumbers with a fractional partMeasurements, setpoints, engineering units

Ask what the value means. You cannot pack half a bottle, so a bottle count is an INT. A volume can be 0.75 L, so it is a REAL.

What := does

Each time the scan reaches an assignment, the controller calculates the right side from the values the variables hold at that moment. It stores the result in the variable on the left, and nothing else changes. A single = is not an assignment: it asks whether two values are equal.

Common mistakes

  • Storing a fractional value in an INT: the fraction is lost before any later line can use it. Give a working variable the type its value needs.

  • Setting a default with an assignment: FILL_SP_ML := 1250.0; in the program body runs every scan and replaces the operator's entry. Put a starting value in the declaration, where it applies once.

  • Writing = for :=: FILL_L = FILL_SP_ML / 1000.0; is a comparison, not a statement, and the editor reports an error.

Platform notes

  • This simulator converts an INT to a REAL automatically inside a calculation such as FILL_L * BOTTLES_PER_CASE. Some editors require an explicit conversion, such as INT_TO_REAL, before mixing the two types.

  • Storing a REAL result in an INT keeps only the whole-number part here. IEC 61131-3 (the international standard for programmable controller languages) calls for an explicit conversion, such as REAL_TO_INT, which rounds to the nearest whole number, and many editors refuse the implicit form. Either way, choose a destination type that can hold the value.

  • Real platforms also offer longer integer types, such as the 32-bit DINT. This simulator's integer type is the 16-bit INT.

Enable JavaScript to run the embedded simulator and complete the interactive exercises.