Technical Article

PLC Program Commands: Arrays and Pointers

September 01, 2022 by Jon Peterson

The ability to utilize an array effectively is a fundamental skill in becoming a PLC programmer, but it can also be very complex and difficult to master. Let’s do a run-down of arrays and some of their complexities!

See our previous articles covering PLC programming commands:

Timers

One-Shots

Up and Down Counters

Math Instructions

Trigonometry Instructions

Comparison Instructions

Move and Copy Instructions


 

​​Using arrays in programming is an efficient way to sort and store a lot of similar data under one header. We use them in programs all the time. If you are new to arrays, here is an example that doesn’t use programmer lingo:

Let’s say you have a stack of 1000 papers in front of you. There are no page numbers. Each piece of paper has a piece of data on it, a random number. This is an example of uncategorized data. A massive, sometimes scary, pile of entries.

Now, let’s put some of this data into an array. You see that pages 700-900 each have a recipe number on them and all go together as a list. You put them in their own little booklet called “Recipes” with page numbers from 1-200. This is your new array—200 recipes in a nice little package. In the future, as you search for data, you can now bypass these 200 pages if you are not looking for recipe data, or flip to them directly if you need a specific piece of data inside. 

 

Arrays in Programming

Let’s look at how we can use an array in a program by building off of our previous example. 

Imagine a program that is wrapping boxes. There are 200 different wraps it can do, each of which is stored in a single piece of data in our “Recipe” array. As a box pulls up to the wrapping station, it tells the program to use a specific wrap. The first one might tell the program to use Recipe 10. The next one uses Recipe 28. The third one uses Recipe 185. That number position in the array that each box carries is called a pointer because it points to a specific piece in the array. These pointers are also known as ‘indexes’ (technically called indices) in many programming situations.

Instead of each box having to carry along an entire wrapping layout, all it has to carry is a single number to point to the correct wrapping recipe. 

 

array data type in Rockwell's RSLogix (Studio) 5000 software

Figure 1. Creating an array data type in Rockwell’s RSLogix (Studio) 5000 software.

 

In Allen-Bradley’s RSLogix (which start with 0 instead of 1), our array would be written like this:

Recipe[0]

Recipe[1]

….

Recipe[199]

 

Pointers in Programming

In the last section, I mentioned that the pointer is the number that points to the position in the array. Let’s dig in a little deeper here.

There are two kinds of pointers—static and variable. A static pointer always points to the same place. If the pointer is 128, it will always go to the 128th number in the array. The variable pointer is a little bit more complex. It allows us to look at different locations in the array based on another piece of information. If that piece of information is “Box Three’s Position” during its travel through a machine, we’re not always going to look at position one. If box three moves forward, the pointer moves forward with it. 

 

Static PointerAlways points to the same place. 

A static pointer would be written like this in RSLogix:

Recipe[128]

 

Variable PointerPoints to the location designated by the variable we choose. 

A variable pointer would be written like this in RSLogix:

Recipe[BoxThreePosition]

 

Overview of Data Types

You’re still here! Awesome! We’re going to change gears for a moment and talk about something called data types

In many programming languages, and particularly in PLCs, we are able to make our own packages of data. Ordinarily, you might name a piece of data “TotalPieces” and the data type is just an integer (a number). Another piece of data might be “PieceLabel” and the data type is string (a set of letters). You can make your own data types that contain many pieces of data though.

 

creating array data type as Data Block in Siemen's TIA Portal software

Figure 2. Creating an array data type as a Data Block in Siemen’s TIA Portal software.

 

Let’s say I make my own data type called “BoxInformation” and it has four integers and a string. If I made a 40 piece array of BoxInformation data, each one would have four integers and a string. Now what if I added an array of five integers inside my datatype? Each piece of data that was of that data type would now have four integers, a string, and an array of five Integers. If I made an array of these, each spot in the array would have another array inside of it.

This is very complex, but I have personally worked in programs that had to do this. It is an array nested inside another array. We’ll touch on an example of this in the section about keeping track of nested variables in the field.

 

Throw Some Math into the Equation

I know you want this to be more complicated! Don’t worry, I can help with that. Did you know you can also do math inside variable pointers? I mentioned earlier that we were looking at “BoxThreePosition” in the machine. But what if we want to look ahead of box three? Many programs will allow you to do math commands inside the pointer such as “BoxThreePosition+1” in order to change where you’re looking.

 

A variable pointer with math would be written like this in RSLogix:

Recipe[BoxThreePosition+1]

 

creating array data type in Automation Direct's Productivity Suite software

Figure 3. Creating an array data type in Automation Direct’s Productivity Suite software.

 

Keeping Track of Nested Variables

Here is an example of one of the hardest array locations I have had to hunt down in RSLogix:

 

DataSet[CurrentLayer].Box[Orientation+1].Position

 

It’s a variable pointer with an added one that is pointing to ANOTHER variable pointer.

This is where I break out another text document or even a paper and pencil in the field. Save a copy of the program to your computer and look at the program without it being online and operating. This way, you can track down data without the data changing on you as the machine runs.

Start with the first variable on the left. Once you track down the CurrentLayer variable, you can down that number and figure out where that point in the first array is. Once you’ve got that number, find the next variable, the “Orientation” number. Once that is found, add one to it and write it down.

You can now fill in your numbers and figure out where exactly a piece of data is held inside that nested array.

 

Conclusion

Arrays are incredibly useful and fairly simple to start with. Be sure not to let an overly complex set of arrays overwhelm you though! Take your time, save a snapshot, go offline from the live machine so everything isn’t changing in front of you, and write your thoughts out as you decode these complex systems.

 


 

If you enjoyed this article, check out the rest of the PLC programming article sequence:

Timers

One-Shots

Up and Down Counters

Math Instructions

Trigonometry Instructions

Comparison Instructions

Move and Copy Instructions