🍩 PLC Arrays 101
0%
📺 A Suspiciously Fun Lesson

PLC Arrays,
explained with a donut box.

You don't need 50 variables called Donut_01_Glazed through Donut_50_PleaseStopHelpMe. You need one box with numbered slots. That's an array.

Let's go →Takes ~3 minutes. Zero PhDs required.
MyDonuts[0]type: STRING
01 / The problem

So you've got ten donuts.

You could declare ten separate variables. You could also juggle ten donuts on a unicycle. Both are bad ideas.

😩 The Dark Times

Ten variables. Ten regrets.

VARDonut_01_Glazed := 'Glazed';
VARDonut_02_Sprinkle:= 'Sprinkle';
VARDonut_03_Choco := 'Chocolate';
VARDonut_04_Maple := 'Maple';
VARDonut_05_Jelly := 'Jelly';
VARDonut_06_Boston := 'Boston';
VARDonut_07_Cruller := 'Cruller';
VARDonut_08_OldFash := 'OldFashioned';
VARDonut_09_Cinnamon:= 'Cinnamon';
VARDonut_10_??? := 'help me';

Now imagine looping through these. Go on. Imagine it. I'll wait.

🌈 The Enlightenment

One box. Ten slots.
Chef's kiss.

VAR
MyDonuts : ARRAY[0..9] OF STRING;
END_VAR

One name. Numbered slots from [0] to [9]. You access any donut with MyDonuts[i]. Loops? Easy. Sanity? Restored.

02 / The big reveal

Meet your donut box.

Each slot has a number (the index) and can hold a value. Indices start at 0 because programmers like to keep you on your toes.
Click any slot to inspect it. Hit ✏️ Type to drop a new donut in.

MyDonuts : ARRAY[0..9] OF STRINGsize = 10
[0]
[1]
[2]empty
[3]
[4]empty
[5]empty
[6]empty
[7]
[8]empty
[9]empty
SELECTED MyDonuts[0]
VALUE = 'Glazed'
TIP Press ✏️ Type, click a slot, type, hit Enter.
You picked slot [0]. It's holding a Glazed. Looks delicious. 🤤
03 / How storage works

Watch the magic move.

Drag the slider to step through what happens when the PLC executes a single line of Structured Text. No CPUs were harmed in the making of this animation.

// One line. That's it. That's the lesson.
MyDonuts[4] := 'Sprinkle';
[0]
[1]empty
[2]
[3]empty
[4]empty
[5]empty
[6]empty
[7]empty
[8]empty
[9]empty
Step 1 of 4
The PLC reads MyDonuts[4]. "Slot 4, you're up."
04 / The real code

Now show me the actual PLC code.

Here's the same donut box written as Structured Text, the kind of code you'd see in TwinCAT, CODESYS, Studio 5000, you name it. Change the index. Watch the PLC react.

(* === Declaration === *)
VAR
MyDonuts   : ARRAY[0..9] OF STRING;
i           : INT := 3;
currentItem : STRING;
END_VAR

(* === Fill the box at startup === *)
MyDonuts[0] := 'Glazed';
MyDonuts[1] := 'Sprinkle';
MyDonuts[2] := 'Chocolate';
MyDonuts[3] := 'Maple';
MyDonuts[4] := 'Jelly';
MyDonuts[5] := 'Boston';
MyDonuts[6] := 'Cruller';
MyDonuts[7] := 'Old Fashioned';
MyDonuts[8] := 'Cinnamon';
MyDonuts[9] := 'Strawberry';

(* === Read whichever slot 'i' points at === *)
currentItem := MyDonuts[i];

🎛 Twiddle the index

Type a number for i. The PLC will fetch MyDonuts[i]. Try -1 or 12. I dare you.

i :=
CYCLE scan #1021
EXEC currentItem := MyDonuts[3]
RESULT currentItem = 'Maple' ✅
The Golden Rule: indices go 0 to 9 for an ARRAY[0..9]. Step outside and the PLC throws a tantrum.
🔁 Bonus: loop through the whole box

The real superpower of arrays

FOR i := 0 TO 9 DO
HMI_Display[i] := MyDonuts[i];
END_FOR;

// Ten lines of work? Nope. Three lines. You're welcome.
05 / Pop quiz, hotshot

Did any of that stick?

Three questions. Zero stakes. Maximum dignity.

Q1

How many slots are in `ARRAY[0..9] OF INT`?

Q2

Your code sets `i := 10;` and then runs `MyDonuts[i] := 'Boston';` on an `ARRAY[0..9]`. What happens?

Q3

What does `:=` mean in Structured Text?

06 / The cheat sheet

Stick this on your monitor.

DECLARE
MyDonuts
: ARRAY[0..9]
OF STRING;

One name. 10 numbered slots. Done.

WRITE
MyDonuts[3]
:= 'Sprinkle';

Use := to stuff a value into a slot.

READ
x :=
MyDonuts[3];

Same brackets, no := on the array side.

🙅 Don't be that engineer

  • Don't go past the last index. ARRAY[0..9] stops at 9. Not 10. 9.
  • Don't name it arr1. Name it MyDonuts and future-you will weep with gratitude.
  • Don't loop from 1 to 10. Loop from 0 to 9. (Or use UPPER_BOUND.)
  • Don't store a REAL in a STRING array. The type checker is watching. Always watching.