PLC Basics: Working With Arrays

Updated on April 5, 2019
LiamBee profile image

10+ Years in Automation, PLC and HMIs. Working with most major Automation and Instrumentation vendors

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[4], this would return the 5th element in the Array (because the Array started from 0!)

A Two dimensional Array is accessed with MyArray[0][4]. 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.


Summary

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!

Questions & Answers

    Comments

      0 of 8192 characters used
      Post Comment

      No comments yet.

      working

      This website uses cookies

      As a user in the EEA, your approval is needed on a few things. To provide a better website experience, owlcation.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

      For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://owlcation.com/privacy-policy#gdpr

      Show Details
      Necessary
      HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
      LoginThis is necessary to sign in to the HubPages Service.
      Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
      AkismetThis is used to detect comment spam. (Privacy Policy)
      HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
      HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
      Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
      CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
      Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
      Features
      Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
      Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
      Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
      Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
      Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
      VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
      PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
      Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
      MavenThis supports the Maven widget and search functionality. (Privacy Policy)
      Marketing
      Google AdSenseThis is an ad network. (Privacy Policy)
      Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
      Index ExchangeThis is an ad network. (Privacy Policy)
      SovrnThis is an ad network. (Privacy Policy)
      Facebook AdsThis is an ad network. (Privacy Policy)
      Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
      AppNexusThis is an ad network. (Privacy Policy)
      OpenxThis is an ad network. (Privacy Policy)
      Rubicon ProjectThis is an ad network. (Privacy Policy)
      TripleLiftThis is an ad network. (Privacy Policy)
      Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
      Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
      Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
      Statistics
      Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
      ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
      Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
      ClickscoThis is a data management platform studying reader behavior (Privacy Policy)