How-to
How to Display Text Labels From a Sensor Range in CODESYS
Short answer
Classify the reading in the controller with an IF/ELSIF chain that assigns a STRING label to each sensor range, then point the textbox's text variable at that string instead of the numeric tag. The number becomes a category in the PLC, not on the screen.
On this page
Yes — classify the reading in the controller and show the result as a string. Write an IF/ELSIF chain in structured text that compares the sensor value against the range limits and assigns a label such as Low to a STRING variable, then point the textbox's text variable at that string instead of the numeric tag. The screen renders the category the controller supplies; a text element does not sort a number into ranges by itself. The decisions you own are which label each exact boundary value gets, what appears above the top threshold, and what to do when the request is really about a chart's numeric Y-axis.
The method in four steps #
- Confirm the variable feeding the comparisons holds the scaled engineering value, not a raw count; the checks in scaling a PLC integer into an HMI display value apply to labels exactly as they do to numbers.
- Declare the pair the classification needs:
SensorValue : REAL;for the reading andsDisplay : STRING(INT#10);for the label. - Write the IF/ELSIF chain with one branch per band, closed by a final ELSE so a reading above the top threshold still gets a defined label.
- Set the textbox's text variable to
sDisplay, then test each exact boundary value online before calling it done.
The classification code #
IF SensorValue <= 0 THEN
sDisplay := 'Empty';
ELSIF SensorValue > 0 AND SensorValue <= 50 THEN
sDisplay := 'Very Low';
ELSIF SensorValue > 50 AND SensorValue <= 100 THEN
sDisplay := 'Low';
ELSIF SensorValue > 100 AND SensorValue <= 140 THEN
sDisplay := 'High';
ELSIF SensorValue > 140 AND SensorValue <= 180 THEN
sDisplay := 'Very High';
ELSE
sDisplay := 'Over range';
END_IF
IEC 61131-3 structured text evaluates an IF/ELSIF chain from the top and stops at the first TRUE branch. Two consequences follow. Every branch after the first needs only its upper comparison, because anything reaching it already failed the branch above; the AND lower guards are redundant, and you keep them only so each line reads as a self-contained range. And no semicolon belongs after END_IF: IF is a statement, and semicolons mark the assignments inside it.
The ELSE is not decoration. Without it, a reading above 180 fails every branch and sDisplay keeps whatever it held last, so a runaway value can leave Low on the screen indefinitely. Over range is 10 characters, exactly the declared capacity of STRING(INT#10); a longer assignment is cut to that length.
Which label owns each boundary #
| Reading | Label | Boundary effect |
|---|---|---|
| SensorValue <= 0 | Empty | Zero and everything below |
| 0 < SensorValue <= 50 | Very Low | Includes exactly 50 |
| 50 < SensorValue <= 100 | Low | Includes exactly 100, excludes 50 |
| 100 < SensorValue <= 140 | High | Includes exactly 140 |
| 140 < SensorValue <= 180 | Very High | Includes exactly 180 |
| SensorValue > 180 | Over range | Requires the final ELSE |
With these comparisons, every threshold value lands in the band that ends at it: 50 shows Very Low, 100 shows Low, 140 shows High, 180 shows Very High. The request for 50-100 to show Low is met for everything strictly above 50. If the operating specification puts 50 itself in Low, flip the operators at that boundary on both sides — Very Low becomes SensorValue < 50 and Low becomes SensorValue >= 50 — so the bands neither overlap nor leave a gap.
One caution while you decide: the label describes the current value only. A stale but plausible reading still produces a plausible label, so sensor and communication diagnostics stay a separate job.
Bind the textbox to the string variable #
In a CODESYS visualization, the text field exposes its dynamic text through the "Text variable" property; enter sDisplay there and the element shows the label live. A rectangle works the same way: give the rectangle your output string as its text variable and keep placeholder static text in the element. The same element configuration applies in a web visualization.
The text variable field expects a variable reference. A function block property or an expression can be rejected there, so if the label lives inside a function block, copy it to a plain program variable each scan and reference that variable. When the HMI is a separate panel rather than the CODESYS visualization, expose the string through that HMI's driver or tag database and bind the panel's tag; the classification still lives in the controller either way.
Worked example: labeling a reading of 50.0 on the 0 to 180 scale #
Inputs:
SensorValue= 50.0, a REAL sitting exactly on the contested boundary between Very Low and Low. The value carries whatever engineering unit the scaling produces; the threshold list (0, 50, 100, 140, 180) is unitless on its own.sDisplaydeclared asSTRING(INT#10), capacity 10 characters.- The chain above, with each band's upper limit inclusive.
Rule in words: the reading lands in the first band, evaluated from the top of the chain, whose upper limit it does not exceed.
Arithmetic:
- Branch 1: 50.0 <= 0 → 50.0 is greater than 0 → FALSE.
- Branch 2: 50.0 > 0 → TRUE, and 50.0 <= 50 → TRUE. Both comparisons hold, so the branch executes:
sDisplay := 'Very Low'. - The chain stops. The Low, High and Very High branches and the ELSE never evaluate.
Result: the textbox shows Very Low, 8 characters inside the 10-character declaration.
Decision: at exactly 50 the operator reads Very Low, and Low begins with the first value above 50. If the specification assigns 50 to Low, change the two comparisons at that boundary as described above, and this same reading then produces Low.
Letters on a chart's Y-axis #
The follow-up request — alphabetic values on the Y-axis — runs into what a chart is. A textbox displays one string in one place; a chart places each sample at a numeric Y coordinate, and the axis ticks are numbers because the coordinates are numbers. Changing the tick text does not move the samples.
Commit to one of three honest options. If the chart element supports custom tick labels or a text list for dynamic text, map the numeric positions to your labels and accept that the samples still sit at their measured heights. Otherwise, show the category beside the chart from sDisplay, or replace the chart with an element built for discrete states. The bands are unequal — 50 wide, then 50, then 40, then 40 — so letters on the axis hide those distances; an operator who needs the value's position keeps the numeric axis and reads the label beside it.
Test each boundary before you call it done #
- Watch
SensorValueandsDisplaytogether online. If the input moves and the string does not, the classification code is not executing in an active task, or the element references a different variable instance. - Write 0 and read Empty.
- Write 50 and read Very Low, then a value just above 50 and read Low.
- Write 100 and read Low, then a value just above 100 and read High.
- Write 140 and read High; write 180 and read Very High.
- While the label reads Low, write a value above 180 and confirm Over range appears; without the ELSE this is the step that fails.
- Return to the live sensor and confirm the compared value is the scaled engineering quantity, not the raw count.
You can drive these forced writes from the visualization in simulation while the controller runs live; testing HMI simulation against a live PLC covers making that connection.
Questions asked
Can I use a CASE statement instead of the IF/ELSIF chain?
You can, but not on the raw reading. CASE selects by matching discrete values, and a REAL sensor reading rarely lands exactly on a value you list. Either classify with IF/ELSIF into a state integer and switch CASE on that integer, or keep the IF/ELSIF chain as the single place the ranges are defined. The chain also reads as a list of bands, which is exactly what you check when a label looks wrong.
Should the label variable be WSTRING instead of STRING?
STRING holds single-byte characters and is enough for these English labels. Use WSTRING when the text must carry characters outside that set and the visualization element, the font and any external HMI driver all support wide characters end to end. Changing only the controller declaration does not make the glyphs render; the whole path has to carry them.
The string is correct online but the textbox stays blank. What is wrong?
Look at the element, not the logic. The text variable must reference the same variable instance the code writes, the element must render text rather than a numeric format, and the runtime must be connected to the controller that executes the classification. For an external HMI, follow the string through the driver tag as well; a correct controller string with an empty screen points downstream of the code.
Does a reading of exactly 50.0 always land on the boundary?
Only if the value feeding the comparison is exactly representable at that point. A reading scaled in the controller from an integer, or passed through a filter, can sit a fraction above or below the threshold, and the label then follows the side it lands on. Test at the threshold with the real scaling active, and if a band edge is operationally critical, set the operators so a rounding hair cannot flip the label.