Variables
What Is a Variable?
When writing ladder on a PLC, you specify memory by physical address (such as DM100), but with variables you specify it with names such as 'aaa'
Inside the PLC a table such as 'aaa' = address 0, 'bbb' = address 1 is created; it is allocated automatically at build time, so you do not need to worry about which address the variable 'aaa' is assigned to
How to Name Variables
For variable names,
anything is fine as long as it is easy to understand
It is an extreme way to put it, but this is the basic rule
English, romanized Japanese or Japanese, anything is fine; easy to understand is what matters most
Except for common abbreviations, writing words in full rather than abbreviating them leads to fewer mistakes, even if names get longer
Easy to understand means easy for you and for others, but first aim for variables that are easy for you to understand and picture
At first you will hesitate a lot, but if you look at and imitate other people's samples, patterns will form over time
Reference
For how to name variables, see Naming Rules
There is also this site "A summary of English words often used in programming" (Japanese)
Types of Data Types
For details of the types, see the manual; here I roughly describe how to choose between them
Boolean type
The Boolean type represents ON/OFF, with values expressed as TRUE/FALSE
It is mostly used for I/O contacts and as flags in ladder
| BOOL | FALSE, TRUE |
Bit string types
BYTE, WORD, DWORD and LWORD
Use them only when bit operations (AND, XOR, etc.) or instructions require them, and avoid using them actively
However, BYTE arrays are convenient in communication ladder, so they are recommended
| BYTE | 0 ~ FF |
| WORD | 0 ~ FFFF |
| DWORD | 0 ~ FFFFFFFF |
| LWORD | 0 ~ FFFFFFFFFFFFFFFF |
Numeric types
Basically, use these numeric types
By size there are SINT, INT, DINT and LINT, plus U versions USINT, UINT, UDINT and ULINT
U means unsigned, with no negative values
| SINT | -128 ~ +127 |
| INT | -32768 ~ +32767 |
| DINT | -2147483648 ~ +2147483647 |
| LINT | -9223372036854775808 ~ +9223372036854775807 |
| USINT | 0 ~ +255 |
| UINT | 0 ~ +65535 |
| UDINT | 0 ~ +294967295 |
| ULINT | 0 ~ +18446744073709551615 |
Real types
REAL and LREAL
These data types can handle decimals, but note that errors occur
(avoid them with rounding and similar processing in calculations)
REAL has about 6 digits of precision and LREAL about 14
They are very convenient, but if 3 decimal places of precision is enough, it can be better to multiply by 1000 and calculate with numeric types
They tend to be slower than numeric types, so if the scan time looks like it will grow, switching to numeric types may shorten it...
| REAL | -3.402823 x 1038 ~ -1.175495 x 10-38 0 +1.175495 x 10-38 ~ +3.402823e x 1038 |
| LREAL |
-1.79769313486231 x 10308 ~ -2.22507385850721 x 10-308 0 2.22507385850721 x 10-308 ~ 1.79769313486231 x 10308 |
Date and time types
TIME, DATE, TIME_OF_DAY and DATE_AND_TIME
Timer values can be written clearly, such as T#500ms
When calendar processing such as 7 days from today is needed, just add T#7d and you get the result without worrying about crossing months, leap years in February or crossing years
Internally, the elapsed time accumulated in ns from 1970/01/01 is converted to year, month, day, hour, minute and second
You do not need to worry about it, but it is worth remembering that clearing a DATE_AND_TIME gives 1970/01/01 00:00:00
| TIME | -9223372036854.775808ms (-106751d_23h_47m_16s_854.775808ms) ~ +9223372036854.775807ms (+106751d_23h_47m_16s_854.775807ms) |
| DATE | 1970-01-01 ~ 2106-02-06 |
| TIME_OF_DAY | 00:00:00.000000000 ~ 23:59:59.999999999 |
| DATE_AND_TIME | 1970-01-01-00:00:00.000000000 ~ 2106-02-06-23:59:59.999999999 |
String
A NULL (0x00) is added at the end of the string so it is handled as a string
The character encoding is UTF-8
The maximum length is 1986 bytes (including NULL), and it decreases when Japanese is used (about 661 characters if all Japanese)
SJISToUTF8 and UTF8ToSJIS are provided for character encoding conversion
Derived data types
Structure type (STRUCT)
You can create a data type that groups multiple variables
[Example] Creating a structure named sPosition for 3D coordinates looks like this
| Structure name | Member | Data type |
|---|---|---|
| sPositon | Struct | |
| X | LREAL | |
| Y | LREAL | |
| Z | LREAL |
Furthermore, adding an enable flag variable Enable and a coordinate name variable Name lets different variables be grouped into one, as below
| Structure name | Member | Data type |
|---|---|---|
| sPositon | Struct | |
| X | LREAL | |
| Y | LREAL | |
| Z | LREAL | |
| Enable | BOOL | |
| Name | STRING[20] |
Union type (UNION)
Lets the same data be accessed as different data types
However, only BOOL and bit string types can be union members
Example: creating a union of BOOL, WORD and DWORD looks like this
| Union name | Member | Data type |
|---|---|---|
| uTemp | Union | |
| B | BOOL[32] | |
| W | WORD[2] | |
| DW | DWORD |
Putting the hex value 12345678 into DW of this union stores it in memory as below
| H | L | |||||||||||||||||||||||||||||||
| BOOL[32] | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 0 |
| WORD[2] | 1234 | 5678 | ||||||||||||||||||||||||||||||
| DW | 12345678 | |||||||||||||||||||||||||||||||
VariableName.b[0] is FALSE, VariableName.b[3] is TRUE and VariableName.W[0] is 5678
Enumeration type (ENUM)
Structure type (STRUCT)
Lets values be replaced with meaningful names
| Name | Enumeration value |
|---|---|
| Positive direction | 0 |
| Shortest way | 1 |
| Negative direction | 2 |
| Current direction | 3 |
| No direction | 4 |
Arrays
Arrays are used when preparing multiple sets of the same data, such as recipes, and byte arrays are often used in communication processing
Example
Example: creating 20 sets of position data using the structure sPosition above gives an array like the one below
Entering sPosition[20] as the data type converts it automatically as shown below
Example
Use it as below (in ST)
Position[0].X:=100.0
Position[0].Y:=200.0
Position[0].Z:=10.0
Position[1].X:=120.0
Position[1].Y:=220.0
Position[1].Z:=12.0
Local and global variables
Basically, design programs so as not to use global variables
If you keep structured programming in mind, global variables naturally become fewer
For example, create a frequently used block (such as a loader) as a basic POU and keep global variables to a minimum so it can be reused in other machines
Scope
Local variables can be seen only within the POU, while global variables can be seen from anywhere

The global variable Global01 can be seen from both POU1 and POU2
The local variable aaa of POU1 and the local variable aaa of POU2 have the same name but are assigned to different memory
The local variable aaa of POU1 cannot be seen from POU2, and likewise the local variable aaa of POU2 cannot be seen from POU1
In other words, the local variable aaa of POU1 is said to be "in scope" from POU1 and "out of scope" from POU2
Attributes
Initial value
The initial value is put into the variable when the PLC starts
If Retain is checked, the initial value is ignored
Assignment
Variables registered in the I/O map receive some value
To access CJ unit memory, set as in the table below
| CJ memory | Assignment |
|---|---|
| CIO_0 | %0 |
| CIO_0.00 | %0.0 |
| WR0 | %W0 |
| HR0 | %H0 |
| DM0 | %D0 |
| EM0_0 | EM0_0 |
| EM4_0 | EM4_0 |
Example

Retain
Retains the value when the PLC power is off
Constant
Value cannot be changed from ladder etc.
Network Publish
When accessing variables from a PC etc., always select Publish
For tag data links, select Input or Output
FAQ
What is a variable?
A variable is a mechanism for handling memory by name instead of physical address. Addresses are assigned automatically inside the PLC, so users do not need to be aware of address numbers.
Which numeric types are commonly used in PLCs?
Numeric types such as INT, DINT, UINT and UDINT are generally used. Choose signed or unsigned and the data size according to the application.
What is a structure (STRUCT)?
A structure is a user-defined data type that groups multiple variables. It lets you manage related information such as coordinates or equipment data together.
What is a union (UNION)?
A union is a data type that lets the same memory area be referenced as different data types. It is used when handling data in bit or word units.
What is the difference between local and global variables?
Local variables can be used only within the POU where they are defined, while global variables can be referenced from the whole project. Keeping global variables to the minimum necessary is recommended for maintainability.
