In this video we start programming with the Arduino Opta PLC using the popular PLC Programming language, Structured Text! This video continues the PLC programming tutorial series on the channel!
A PLC or Programmable Logic Controller is the staple of industrial automation and controls, and Arduino has made one of the most affordable ways to get into the industry! This micro-PLC has Relay outputs, and programmable digital or analog input and output channels as well.
It also supports Modbus OPC communications for easy integration into SCADAs or larger controls systems, RJ45 ethernet communications and Wi-Fi connectivity! Additionally there are no licensing fees to use the Arduino PLC IDE where you can program in Ladder, Structured Text, FBD, SFC or using the regular Arduino IDE/C++ based language!
Don't forget to leave a like, subscribe to the channel, and comment what you want to see next!
Okay, in uh this chapter we are going to
make a new project and we're going to
call it first
structured
structured
text. Okay, so we're going to go new
project. Make sure you have the opta
selected if you're using the same
controller as me. And we're going to say
okay. It's going to generate a new
project. Um now I have previously loaded
the firmware onto my uh Arduino. So
everything else here should be fine.
We'll go to online setup communication
mod bus and properties and just make
sure COM 10 is still selected. That
looks good. And we're going to hit
connect just to make sure that
connections look good and the uh all PLC
Arduino Opta still looks good. All the
available interfaces should show green
lights. So this is all good. Hopefully
uh in a previous video and previous
chapter, if you're watching a long form
uh tutorial, uh I covered all of this
stuff in some greater detail. So, if you
haven't gotten to this step and you're
not able to get online with your Arduino
Opt, I suggest you go back on the
channel and you watch the getting set up
or getting started with your Arduino PLC
video. Um, so, uh, assuming you can get
to this step and you have a new project
just like I have and if you were to go
into the project tab over here on the
left and hit main, you just have this
default program it gives us that is
count equal to count + one. Um, but if
you're able to get this far, then we're
in the same spot and the rest of this
video is going to go so smoothly.
So, let's go to resources because for
today's video, I'm going to use the uh
LEDs that are built into the Arduino
Opta. And I'm going to use the user
button, which is on the front of the
PLC. Uh so, you could see there actually
when I was pushing that button, it was
doing stuff. That's because I have
different code on there as shown by this
diff code thing uh in the bottom left of
my window right there. Um so, maybe I'll
hang out right there so you can see my
tab. Um, but right now I'm going to go
ahead and just download this empty uh
PLC program to it so that there's
nothing on my PLC. Uh, and then we'll be
in exactly the same spot. So, compiling
sketch, please wait. I will wait for you
Arduino.
Okay. So, once you get source, okay, or
you might have had nothing. Um, but in
this case now we have PLC that has
nothing on it. Uh, pushing the button
does nothing. The lights are not there.
The green light isn't even on. You would
not know anything was working on this.
Um, so let's go to the resources and
click on the opta. Um, this should still
look good, but since we're using the
button and the LEDs that are on the
opta, we're going to add variables here.
So, the name of them is just kind of how
you identify what the button is. There's
one push button on the front of the Opta
uh that is programmable. The other one
is the reset button, which is used for
like factory resetting. So, there's only
one button on the front of the Opta we
can use. It's the user push button. So,
I'm going to assign it a variable name
that is just going to be user all caps
like that. Now, I'm going to go to LED
outputs and I'm going to do basically
the same thing. L1, L2, L3, L4,
LR, LG, LB.
And so this is my exist, but it's not
automatic. That's fine.
LR. Okay, it's really unhappy.
Oh, mercy. Okay, I'm going to call this
red,
green, and blue.
uh basically you need to assign variable
names to these uh hard-coded inputs and
outputs so that we can use them in our
program. So we have L1 through L4 refers
to the four status lights at the bottom
of the opta and then red and green show
up in the same spot above the reset
button and blue shows up right above uh
the user button. So we're assigning all
those variable names so that when we go
to write our program here in Maine, we
have user and uh all seven of those
lights that we can play around with. So
let's write functionally
what can be considered uh the hello
world the intro to programming for
structured text. Let's say green equals
true. And a couple important syntax
things here is green colon equals is how
you set a value equal to true all caps
and then a semicolon. So for structured
text it's variable that you want to
change the value of and then setting a
value is colon equals and then the value
you want to set it to and then a
semicolon. And this should compile and
download. So I'll hit this little button
for download PLC code. It'll ask me if I
want to compile it. Say yes. And now if
I hold up the PLC, the green light is
on. Okay. So that's my hello world right
there. That's the green light is on. I
am talking to the PLC and it is
listening.
Now let's go ahead and go a little bit
further. Let's say that red will be
equal to false. By default, it'll be
off, but um let's go ahead and say blue
is equal to user. Okay. So, uh, you'll
notice the colon equal sign for all of
those and the semicolon at the end for
all of those. And all I'm doing is
making sure the red light is off. But
the blue light, I'm doing something a
little more interesting here. I'm saying
it's equal to the value of user. And I'm
going to do something here. I'm going to
add all of these values to watch. Uh,
just by right-clicking on the variables
that I want to watch. I'm going to add
them to watch. And I have this window
over here. That's the watch window. And
it shows the current value of them. the
current value of green is true because
my original code was just green equals
true. So now if I download and compile
this and then hold the PLC up, you'll
see green is still true, red is still
off, and that's all good. But if I go
ahead and push this button, you'll see
the blue light turns on. And if you're
watching the watch window here, you'll
see user and blue change when I push the
button. Okay, so uh that's actually it's
pretty cool how fast we're able to
advance through this uh PLC programming
tutorial. We are now controlling the
state of an output from an input. So the
input being that user push button, blue
being the output light. Um but you're
not really learning too much structured
text with this. We're just directly
setting values based on uh other values.
So let's go ahead and take a look at an
if else if statement. Uh let's say if
the user push button is pressed. So if
user semicolon actually if user then and
no semicolon I get my uh Python my C++
and my structured text syntax is messed
up. But here we're checking if a boolean
value is true. So, it's just if user, so
if the user push button is pressed, then
let's go ahead and set L1 equal to true.
And let's go ahead and do L2 also equal
to true. And let's just make sure that
L3 is off when the push button is
pressed. And that L4
is off when I push the push button in.
Now, I'm going to do else if not user.
So this is about the same thing as an
else statement. Um but I think it's
important to call out that else if in
structured text looks a little different
than most programming languages. So it's
this els if and then not user then and
you'll notice if user then and else if
user then don't have semicolons but all
these command lines do. So, I'll copy
these four instructions here, and I'll
shift tab. There we go. I'll change L3
and L4 to the ones that should be on and
L1 and L2 to the two that should be off.
And then, super important in structured
text, you need end underscore if and
then a semicolon. Okay. So, unless I
miss something important, which is
perfectly possible, this should make uh
the the bottom four status lights
flip-flop once it's downloaded.
Okay. So, actually good sign. You can
see that three and four turn on with the
button not pressed. So, I'll go ahead
and press the button. And there you go.
You can see all four of the status
lights on the bottom of the PLC as well
as the blue light are changing. Okay.
So, that's cool. That's if, an else- if,
and an end if statement. Um, but again,
it's all pretty uh straight up boolean
binary con comparisons. So, let's add
one more thing here uh for this
introduction to structured text. Let's
add a variable and do a little bit of
sequencing. Okay.
So, let's go into uh project and global
variables. Doubleclick. And I'm going to
insert two variables here. I'm going to
call one last
user. And this is going to be a boolean
that I'm going to use to track the last
state of the user push button. So
similarly, if you've done anything in
Arduino or other programming languages,
sometimes you want a condition that only
happens on the rising edge of a value.
And to do that, you want some sort of VA
variable to track the last state of it.
Uh, and then at the end of your routine,
you'll set last user equal to user. Uh,
and then this second variable I will
just call position. And I'm actually
going to make this an integer. And I'm
going to say initially I want it to be
zero. Initially I'll have this be false.
False. So those are just initial values.
Come on. There we go. Oh, I have caps
lock on.
Okay. Uh, and I'm going to use position
to track where in the sequence we are.
And here I'm going to give it an
attribute called retain, which means on
uh downloads and power cycles, it's
going to keep its value. Uh, so that's a
useful thing when you have some sort of
PLC program that you want to hold values
even when you download new iterations of
the code, you want it to be set to
retain. Okay, so we made a few new
global variables that we can use in our
code. Let's go back to main. And we're
going to scrap most of this, all of this
really.
And let's go ahead and start by setting
L1 through L4 all equal to false here in
the beginning. Um, and a little way that
structured text programming works is
this code is going to go through and set
all four of these lights equal to false
uh at the beginning of each scan. But
it's only going to matter what the value
of it is at the end of the scan. So if
you had code here that said green equals
true and then you added a line here that
said green equals false within your code
right here, you're changing the value of
green to true and then to false. But
what's going to happen? The only thing
that would actually happen if you ran
this code, so let's just do it
is Whoops. There we go. Is the green
light will be false. all the lights will
be false. It doesn't show green turning
on and then green turning off because
the only thing that executes is the
value of the variables at the end of the
routine. So by setting L1 through L4
false at the beginning, we have the rest
of this code to decide if we want to
turn uh L1 through L4 off or on. So what
we're going to do is we're going to say
if position is equal to one then I want
L1 equal to true. Okay. And uh a couple
things to call out here. In structured
text the comparison is just single equal
sign. Most programming languages that
you've probably seen before two equal
signs is how you check for equality. Um
that's definitely how Python works and
that's my favorite. So uh then we're
going to say else if position is equal
to two L2 is equal to true and then
we'll say else if position 3 L3 else if
position 4 L4 and then we'll say else if
one more thing and then I'm going to end
if okay so let's just get this all lined
up position 3 three position four four
and now let's say else if position is
greater greater than four.
Then what I want to do here, dang. Then
what I want to do here is say, okay,
position has been has exceeded my max
step. So I'm going to set back to zero.
I'm going to put back to the beginning.
And notice that the if thens don't have
semicolons at the end. Uh, but end if
does. And all of the action lines in
each if condition do have semicolons.
Okay, so this is great, but nothing's
actually changing the state of position
yet. So let's move this down a little
bit and let's say if the user button
gets pressed and it's a rising edge. So
not me holding down the user button. Let
me say this is just when user first gets
pressed. So if user and not last user
then I want position to be equal to
position + one and then end my if
statement. And I missed a semicolon
there.
Then at the very end, like we talked
about, we're going to say last user set
it equal to the value of user. Okay, so
this should be all we need. This is
setting the initial values of all of our
lights.
And just to make sure this new program
really took let's go ahead and say we
actually want red to be on now. And
we're going to turn green off. So, when
this boots up, if it's working properly,
we'll know right away from red being on
and green being off. Let's see if we
made any errors. Looks good because I'm
a professional. And the red light's on.
Nothing else is on, which makes sense
based on the code that we wrote. And let
me try to get this framed properly. I'm
going to push the button and the blue
light turns on. And position one turns
on. Push it again. Position two turns
on. three 4 0 1 2 3 4 0 Okay. And you
can add position to your watch window.
Add to watch as well as last user if you
really want to as well as L1 through L4.
Um and now as you're cycling through
pressing the button, you can see the
value of position change every time you
push the button as and blue user and
last user all change almost exactly
together. Um but so that's really fun.
That's basic sequencing. Um, a a very
common way to write uh controls
sequencing is in step one do a series of
things and then once you've checked some
condition like pressure, flow,
temperature, move to step two and then
do a series of things in step two. So
this sort of sequential move through a
position value uh is actually a really
great introduction to PLC programming.
So that's some basic structured text. I
think uh up next we'll sort of recreate
this program in ladder logic. So you get
to see both of the main sort of two uh
PLC programming languages that sort of
duke it out for ultimate control. Um but
I hope you found this uh introduction to
structured text programming useful. The
Arduino Opta is a really cool affordable
way to get into PLC programming. Thank
you to Arduino for providing the
hardware for this video. I hope you
learned something with this structured
text uh tutorial. Let me know what you
want to see next on the channel. Thank
you for watching and we'll see you next
time. Thanks.