PLC Basics: Working With Arrays
What Are Arrays?
Most PLC applications will have an Array declared in them somewhere. Arrays are extremely useful for grouping types of data together that share the same format.
For example, say your application has 20 safety sensors that all need to stop the process if returning FALSE. It's far easier to check the Array contains no FALSE values than it is to check all 20 sensors individually!
Arrays are also good for "Chunking" of data, for example a motor may send a packet of information to your PLC over a network. This packet could consist of the motors Speed, Temperature, Voltage etc. If you have 10 motors, grouping all of the data together in to a Speed Array, or Temperature Array could prove beneficial when it comes to checking that data later on.
So what actually IS an array? An array is a group of common typed elements, declared by a parent name. For example:
MyArray : ARRAY [0..9] OF BOOL;
The above declaration would result in "MyArray" having 10 elements, all of the BOOL type. You cannot have different data types in an array, but you can have arrays of arrays:
MyArray : ARRAY [0..1] OF ARRAY [0..9] OF BOOL;
This declaration would give you the variable "MyArray" as a Two Dimensional Array. This basically means that you'd need to specify not only which element number you want to look at, but also which Array element you wish to look at first.
A single dimension Array (like the first declaration) would be accessed with MyArray, this would return the 5th element in the Array (because the Array started from 0!)
A Two dimensional Array is accessed with MyArray. This would return the 5th element in the first Array element of the "MyArray" variable... Quite a mouthful!
Example Of Array Use
Expanding a little on the earlier example of Proximity Sensors, the above shows a little function for checking 10 sensors.
In the image above, you can see that the variable Proximity_Sensors is declared as an Array that is 0 to 9 elements long, giving us 10 element "slots" in which we can insert data. The data type is declared as BOOL, so its digital signals that are being stored here (TRUE/FALSE).
The Ladder Logic is performing the following, line by line
Line 1. Set the OK_To_Run variable to TRUE. It's a latching coil so if Start_Process became FALSE again, OK_To_Run would remain TRUE until it is Reset.
Line 2. Check a Proximity Sensor. So there's a little more going on here than just checking a sensor. First of all, the contact is a negated contact, so we're looking for a FALSE signal to advance our logic on to the next instruction with a TRUE. So if Proximity_Sensor[i] is FALSE, then OK_To_Run is RESET (The coil is a Reset coil)
So what's the i variable for? This is the index variable, it's the number of the element you want to obtain the value for in your Array. We'll come on to how this updated on the next line, but for now lets assume i = 2. This would give us the 3rd proximity sensors data at the contact we're checking. Lets assume that this data returns a FALSE, this means OK_To_Run gets reset. If you look at Line 4, the contact there that is checking OK_To_Run would be FALSE and DO_PROCESS would no longer be True. This would be the case if ANY of proximity sensors were false.
Line 3. This is the logic that is causing Line 2 to be repeated until all sensors are checked. The EQ function is checking if i equals 10, if it doesn't (notice the circle on the output of EQ is round, that means it's a negated output) then ADD 1 to i and jump back to Check_New_Sensor. Because i has now incremented by 1 a new sensor is checked on Line 2, giving a new possibility of setting OK_To_Run to FALSE.
Once all 10 have been checked, i will be at 9 and the EQ will return a FALSE (because it's negated). The MOVE command's EN input (enable) is also negated, so the FALSE output from EQ would equate to a TRUE input and cause the MOVE to execute, returning i to 0. The jump to Check_New_Sensor would not occur because the jump's evaluation would still be FALSE. This allows the logic to reach Line 4 and continue through the Ladder.
It's a lot to take in if your new to PLC programming and Arrays, but what we've looked at here is a way of checking 10 items of data that are stored in a common variable. This variable can be indexed and that elements value pulled out. This allowed us to repeat the same line of code to check all sensors.
If this was done without an array and 10 individual sensors, it would have looked something like this:
Now imagine you had 100 sensors that needed checking...
I hope this made sense, feel free to drop a comment if you need some extra guidance, it's tricky to get your head around at the beginning!