Bool for single bits, Int up to 32,767, DInt for anything that accumulates, Real for scaled measurements and Word for grouped status bits. Physical signals live in the process image (%I, %Q) and are copied once into DB_IO; application data lives in optimized global DBs built from UDTs, with Retain set only on values that must survive a power cut.
- DInt for counters and totals – Int overflows at 32,767.
- Never compare a Real for exact equality; use a tolerance.
- Retain values that matter after a power cut, not machine state.
Why Data Types Matter
Choosing a type is choosing what can go wrong. An Int that should have been a DInt turns negative after 32,767 parts; a Real compared for equality never matches; a non-retentive counter is zero after a power cut. Get the type right once, at the DB, and every block and every HMI tag inherits it.
Lab Flow: From Sensor Signal to the Right Data Type
Every lesson in the Softwell PLC lab manual series follows the same six stages and the same address rule, so the code you write here plugs straight into the HMI lessons.
Stage 1Field Signals and PLC Tag Table
| PLC tag | Address | Type | Device / comment |
|---|---|---|---|
B30_Part | %I6.3 | Bool | Part sensor at the outfeed |
PT30_Raw | %IW100 | Int | Pressure transmitter 4–20 mA = 0 – 27648 (0 – 10 bar) |
H30_Status | %QW84 | Word | Example of a 16-bit status output to a remote I/O panel |
| Type | Size | Range | Use for |
|---|---|---|---|
| Bool | 1 bit | 0 / 1 | Contacts, commands, status bits |
| Int | 16 bit | −32,768 … 32,767 | Raw analog values, small set values |
| DInt | 32 bit | ±2,147,483,647 | Counters, totals, runtime seconds |
| Real | 32 bit float | ±3.4E38, ~7 digits | Scaled measurements, setpoints |
| Word / DWord | 16 / 32 bit | bit pattern | Grouped status and fault bits for the HMI |
| Time | 32 bit | ±24 days (ms) | Timer presets and elapsed times |
| String | up to 254 chars | text | Product codes, user names |
Stage 2Input Mapping and Memory Areas
| Area | Example | Written by | Rule in this series |
|---|---|---|---|
| Inputs (process image) | %I6.3, %IW100 | Input module | Read only in FC_InputMapping |
| Outputs (process image) | %Q3.0 | Program → output module | Written only in FC_OutputMapping |
| Bit memory | %M10.0 | Program | Avoid in new projects |
| Data blocks | "DB_Prod".Count_DInt | Program, HMI | All application data, symbolic, optimized |
| Temp / static | #tmp, #stat | Block | Local to FC/FB |
// FC_InputMapping
"DB_IO".B30_Part := "B30_Part";
"DB_IO".PT30_bar := SCALE_X(MIN := 0.0, MAX := 10.0,
VALUE := NORM_X(MIN := 0, VALUE := "PT30_Raw", MAX := 27648));Scaling details: PLC analog input scaling.
Stage 3Logic: UDT_Prod, Retain and Safe Comparisons
// PLC data type UDT_Prod
Count_DInt : DInt; // accumulates → DInt, Retain in DB_Prod
Pressure : Real;
Status : Word; // %X0 run, %X1 overload, %X2 door … for the HMI
// FB_Production – STAT: PartOld : Bool;
IF "DB_IO".B30_Part AND NOT #PartOld THEN
"DB_Prod".Data.Count_DInt := "DB_Prod".Data.Count_DInt + 1;
END_IF;
#PartOld := "DB_IO".B30_Part;
"DB_Prod".Data.Pressure := "DB_IO".PT30_bar;
// Never: IF "DB_Prod".Data.Pressure = 5.0 THEN … (exact Real compare)
"DB_Prod".AtSetpoint := ABS("DB_Prod".Data.Pressure - 5.0) < 0.05;
"DB_Prod".Data.Status.%X0 := "DB_M1".Run_Cmd; // slice access into a WordTick Retain for Count_DInt in DB_Prod; leave commands and state non-retentive. The same Word-of-bits idea drives the HMI alarm word in HMI alarm configuration.
Stage 4Link PLC Tags to the HMI
Drag DB_Prod into the HMI tag table – TIA Portal copies the data types exactly. A Real written into an Int tag silently loses its decimals, as shown in HMI I/O fields and setpoint entry.
| HMI tag | PLC member | Used by |
|---|---|---|
DB_Prod_Data_Count_DInt | "DB_Prod".Data.Count_DInt (DInt) | Output field |
DB_Prod_Data_Pressure | "DB_Prod".Data.Pressure (Real) | Output field 99.99 bar |
DB_Prod_Data_Status | "DB_Prod".Data.Status (Word) | Lamps with single-bit animation |
Stage 5Link the Graphics
| # | Object | Properties | Tag / event |
|---|---|---|---|
| ① | Output field count | Format 999999999, no decimals | DB_Prod_Data_Count_DInt |
| ② | Output field pressure | Format 99.99, unit text bar | DB_Prod_Data_Pressure |
| ③ | 8 lamps | Appearance animation type Single bit, bit 0 … 7 | DB_Prod_Data_Status |
Stage 6Perform and Test: Data Types Live
Parts are counted into an Int and a DInt at the same time; a Real pressure drifts around 5.0 bar; a Word packs eight status bits for the HMI. Watch the Int overflow, the exact Real comparison that never fires, and the effect of the Retain setting on a power cycle.
Troubleshooting
| Symptom | Likely cause | First check |
|---|---|---|
| Counter jumps to −32768 | Int overflow | Use DInt |
| Comparison never true | Exact Real equality | Use a tolerance or range |
| Count lost after power cut | Member not retentive | Retain checkbox in the DB |
| HMI shows wrong value | HMI tag type differs from PLC | Drag DB members, do not type addresses |
Hands-On Lab: Choose and Prove PLC Data Types
Hands-on- TIA Portal with an S7-1200 or PLCSIM
- Estimated time: 60 minutes
Declare the tags with the right types
Declare B30_Part (Bool), PT30_Raw (Int) and the status outputs, and note each type's range.
Map and scale into DB_IO
Copy B30 and scale PT30 to bar (Real) in FC_InputMapping.
Build UDT_Prod and DB_Prod
Create a UDT with Count_DInt, Pressure, Status (Word) and a retentive DB_Prod; count with an edge.
Link HMI tags
Drag DB_Prod into the HMI tag table and check that every HMI type matches.
Display and test
Show counts, pressure and status bits, then power-cycle the CPU and verify retain.
Prove the failure cases
Force an Int past 32,767, compare a Real for equality and confirm the tolerance version works.
Every value has a type that fits its range and use, counts survive a power cycle, and no comparison depends on exact floating-point equality.
Frequently asked questions
When should I use DINT instead of INT?
Whenever a value accumulates. INT stops at 32,767, so production counts, runtime seconds and totalisers need DINT.
What is the difference between optimized and standard data blocks?
Optimized blocks store data in the CPU's preferred order and are accessed only symbolically. Standard blocks keep a fixed byte layout and allow absolute addressing, which some older partners require.
Why does my REAL comparison never trigger?
Floating point values are rarely exactly equal to a constant. Compare against a range or use a tolerance instead of testing for exact equality.
What does retentive memory actually protect?
Values you need after a power cycle, such as production counters, runtime hours and recipe parameters. Machine state and commands should normally start clean.
Should I still use M memory in new projects?
Sparingly. Optimized DB members are symbolic, cannot clash between programmers and can be released to the HMI. Keep M memory for system bits such as clock memory if you use them at all.
What is a UDT and why use one?
A PLC data type: a named structure such as UDT_Motor that you declare once and reuse for every motor. Changing the UDT updates every instance, and the HMI can use the same structure.