Technical Article

Understanding Array Data Types

September 03, 2020 by David Peterson

In this article, we’ll investigate the meaning of an array, but also explore some of the ways in which arrays can be used specifically within the context of a PLC system.

Computer programming, including PLC systems, relies on data types that consist of certain arrangements of individual boolean bits.

Most single data types are very familiar: the single boolean, various lengths of integers, and real-type decimal values. Some other values are useful for programming but can be more confusing, including the array data type.

 

What is an Array?

As a very short, loose definition, an array is a group of identical data types (I’ll call them tags since they will be related mostly to PLCs in the scope of this article). Therefore, an array by itself is not exactly a tag, but creating an array creates a set of tags.

Arrays are used to hold many pieces of related data. One of the most common occurrences of an array in programming is the string. This data type is really an array of individual characters that together form words or sentences.

 

Array type tags can make more efficient use of PLC memory, and they can aid in project organization by keeping consistent data grouped together.

 

Each character is a letter, space, or symbol, and the number of characters defines how long the word or sentence could be. Depending on the programming language, this is most often called by the name ‘string’ but in the case of a PLC, it will likely be defined as an array consisting of ASCII characters.

The use of arrays is certainly not limited to words and characters. They’re very commonly used to store consecutive values for related tags. If the system contains many similar sensors or parameters, it would make sense to find a more logical way of storing these values than creating a bunch of individual INTs or REALs.

Although the organization of the tags may be more efficient, it doesn’t consume less memory. It might make sense to consider using a single array tag instead of 10 INT tags, and one the surface that might seem like a smaller consumption of memory, but not the case.

This will simply create 10 similarly-named INTs, still with the same total memory usage, so you still don’t want to use these with any less discretion than creating the same number of individual tags.


 

The Parts of an Array

Arrays contain two parts. The first is the name. Every tag within the array will have the exact same name. This makes it very easy to remember since they do not need unique names. 

 

The process of creating an array. All PLC programming software is capable of creating arrays, the software shown is Productivity Suite from Automation Direct.

 

The second part of the array is the index. The index is the unique way of identifying which of the individual tags within the array is meant to be used. In more generic terminology, the individual pieces are not called tags, but rather elements.

In summary, an array consists of a defined number of individual similar tags or elements. Each element has the same name, followed by a unique index number, always in brackets [ ], braces { }, or parentheses ( ).

 

Uses for an Array

A discussion of a data type isn’t complete without a few examples of where it might be seen, and proof that it can be more effective than alternatives. If not, why would it be worth discussing?

As mentioned before, the nature of the information contained in array elements should be similar. If we have a selection of temperature sensors along a process, the temperature values can be stored in an array. The benefit of this strategy could be displaying any of those temp values on an HMI.

Simply change the number of the index and that temperature will display. If they were contained in unique tags, you would need to change that tag name reference in the HMI to retrieve the data — more difficult than a numeric change of index, and extremely difficult if you need to change the process and add new tag names.

 

A tag database showing an example of an array "Temperature_Data" and the corresponding tags inside the array. They are indexed with parentheses, (1) etc.

 

This strategy has another side benefit of saving memory in the HMI. Again, we do not need to create a whole bunch of tags in the PLC and in the HMI — only the array in the PLC. Then the single task required of the HMI is to change an index number to ‘point’ to one of the array elements in the PLC.

If the sensors are discrete, it might also make sense to be able to reference which sensor information we need by index number rather than tag name. As an added bonus, an array of booleans uses less memory than individual boolean tags. Each boolean tag uses 32 bits. However, inside an array, every multiple of 32 bools use only those 32 bits.

Another common application is character arrays. Transferring data from an HMI to a PLC might be words or letters, not just numbers (for example, a password). An array can be created with an element for every expected character. The number of characters in the array at both the PLC and HMI must be the same or a communication error may occur.

 

What NOT to Do with Arrays

It would be possible to create an array to contain unrelated information. For example, we might have a variety of different types of integers or bools in a system. You could create an array to store every piece of information, but the lack of clarity and organization is a problem with no benefit from saved memory space.

Another caution: be aware that boolean arrays are not commonly seen. There may be many instructions in the software well suited to dealing with arrays of integers, reals, or characters. However, there may not be as many methods of dealing with boolean arrays, and individual tags may continue to be more reasonable.