In this video we get setup to start programming with the Arduino Opta PLC using the popular PLC Programming Language, Ladder Logic or Ladder Diagram! This video will get us ready for the upcoming PLC programming tutorial series coming 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 wifi 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!
Hello and welcome back to the channel.
In today's video, I'm going to show you
how to make your first PLC program in
ladder diagram. Uh, this is a super
popular, super common PLC programming
language. And to show you how ladder
diagram works, I'm actually going to
show you the structured text program
that I built in the last tutorial. Um,
and I'm going to convert that exact
program from structured text into ladder
line by line with you. But I wanted to
show you what the program does first
because ladder is a very visual
programming language. And it's pretty
different from C or Python or C. Uh so
so if you're getting into programming
for the first time, that's not a big
deal. But if you've been a programmer
for a while and you're getting into
controls, this is a more useful way to
learn it in my opinion. So the code that
you can see on screen right now does the
following to the PLC. The red light is
always on. I have a green status light
up here that is always off. And then the
blue light turns on when I push the
button that's under my index finger. So
when I push the button, the blue light
turns on with the button. Then down
here, the four green status lights over
here, I have them go in sequence from
zero, position zero to position one,
two, three, and four. Okay. So the whole
routine that you see here sets the
initial values of all seven of those
lights that I just mentioned. And then
it says, hey, if the user button was
just pressed and the last state of the
user button was that it wasn't pressed,
add one to this position variable. And
then end the if statement. Uh, and then
it checks if we're in position one, turn
L1 on. If we're in position two, turn L2
on. If we're in position three, turn L3
on. four L4 on. And then if position
exceeds four, meaning I pushed it a
fifth time, set it back equal to zero.
And then lastly, set the last user
variable equal to user. Okay, so if you
have a PLC uh an Arduino PLC and you're
programming along, make sure that um
from your overview screen, you it kind
of looks like this. Uh, I should have
started, if I did this the right order,
I should have started with a video on
how to get set up with your Arduino Opt.
Um, so make sure you watch that. If
you're not at this step and you're
trying to code along exactly with this
tutorial, get to this step, um, where
the license looks okay and all the
available interfaces look okay and it
says connected in the bottom right. Um,
again, I should have done a tutorial on
how to get here. So, just check my
channel or if you're watching all these
videos smashed together, check the first
section of this video for that. Um, now,
two other things we did uh in the
structured text video to set ourselves
up that you want to double check are in
there. We went to button inputs and the
user that is on the front of the PLC,
the user button. We added the variable
uh so that we could call it user in our
program. So, make sure you have user or
something easy to remember there. And
then for LED outputs, I added the
variable names L1 through L4, red,
green, and blue to all seven of these
lights so that I could call these lights
from the PLC as well. So, make sure you
have all those variable names or you'll
be like, hey, my version that I'm trying
to follow along with uh doesn't compile
and says that like those variable names
don't exist or something like that.
Okay. So, uh with that basic setup with
these variable names, with these button
inputs, uh let's go ahead and go to
project and let's go to add and new
program. And we'll call this name. We'll
call this ladder one because I might do
more ladder. And make sure you have LD
ladder diagram selected. Structured text
was what we did in the last video. And
we're going to assign it to fast. So,
these are different scan rates, fast,
slow, backward, uh, background, and in
it. We might get more into it later or
just let me know if you have questions
about when to assign what to where. Uh,
but we're going to assign it to fast. No
reason not to go fast. Um, and let's
just go line by line of what's happening
in this main PLC and talk about how to
recreate that in ladder. Okay, so the
first line is green equals false. So,
how do we make a line of code in ladder
diagram to set an output equal to false
all the time? Well, we're going to use
this output coil, which is what this
object looks like. We're going to put
the variable green in it. And currently,
what it reads as is actually green is
always true. So, I'm going to double
click on that coil and I'm going to
negate it. And this strike through this
uh open parenthesis la hashline
slashline and then close parenthesis
reads as not. So it's basically green is
not on. If I copy this whole ladder one
and I change this to normal and I change
this name to red.
This line of code reads as red is always
on. So we have green always false, red
always true. That's what line one and
line two here are doing. And it looks
like this in structured text. Green is
false, red is true. Okay? So, you can
see right away just with this basic
comparison that ladder is a super visual
language. There's nothing that
explicitly tells you it's true or false.
It's telling you based on what sort of
relays in electrical schematics look
like that green will not be true and red
will not be false. Uh green will not be
true and red will always be true. So
let's look at the next line of code
which if you remember was that blue the
blue light will be exactly equal to the
input of the user push button. So I
click on that coil again and I say hey
we're going to control the blue light
but we're going to control it based on
an input now. So this is called a
contact. These on the right that are
controlling outputs are called coils.
This on the left is called a contact.
And we're going to say equal to the user
input. So the way this line of code
reads as if user push button is pressed
then blue light is on. But there's a
second thing going on here which is if
user push button is not pressed blue
light is off. And that's because this
output is a momentary output which means
the blue light will be on as long as the
conditions on the left of this rung are
true. But by default the blue light will
then go off when the conditions on the
left side are false. So this sort of has
if user then blue light and if not user
then not blue light baked into one line
of code. So it really is the same way
this says blue is equal to user whether
it's true whether it's false set blue
equal to user. That is basically exactly
what this line of code is doing. So so
far we have three for three recreated
from structured text. Now this is us
setting the initial value of all four of
those lights equal to false. So, we're
going to copy this green line, which is
uh this whole rung, which is setting uh
green equal to false. And let's see, can
I move that down? I really want it in
exactly the same. I really want in
exactly the same spot. There we go. So,
I'm going to change this to L1. And now,
rather than make four rungs of code to
do the exact same thing for these
lights, I'm going to just control C and
then Ctrl +V right here. And you can see
it sort of puts this branch in which is
saying, "Hey, the same input conditions
are true for all of the outputs on this
rung. So the four outputs are just L1
through L4. Set them equal to not true."
Um, but the input is shared by all four
of them. All right. So this is how we
say L1, L2, L3, L4 equals false. That's
what rung four through 7 here are doing.
Let's just keep going. Let's take a look
at saying if user and not last user,
then position equals position + one.
Okay, that's the line of code that
checks when the user button is first
pressed uh and adds one to the position
incrementor. So to do that, we'll say
rightclick new network after. And now
we're going to check two conditions.
We're going to check if user and then
I'm just going to control crlv this. And
we're going to check if not user. So or
last user.
So last user but this one needs to be
negated. And this is saying if the user
push button is pressed and the last user
boolean is not true then but we can't
use a coil anymore. A coil is
specifically just for boolean
conditions. It's for true false things.
We need to adjust an integer value which
is called position. Ah, and I forgot to
say this in the setup, but if you go to
global variables, these two were made
for the structured text tutorial. The
variable last user, which is a boolean,
and I have the initial value set to
false, which will be how we track the
last state of the user push button. And
then position, which is an integer,
which will start out as zero. And I have
retain on, which means it will uh keep
its value on new iterations of the code
being pushed. So if position is set to
three, when I update the code, it will
stay as a three. Um, and that will track
where in the sequence we currently are.
So let's go back to our ladder. Just
make sure you have these global
variables set if you're following along
exactly. Um, and we'll say if user and
not last user. And now we need a new
block. And if you're if you don't have
this toolbar in here, go to project. No,
go to view toolbars. View toolbars. And
make sure FBD is on. Uh if you have a
decent size monitor, you can have all of
these toolbars on. It doesn't really
hurt to have sfc. Um it's just it's not
something we need right now. Um so, but
you do need FBD because new block exists
in FBD. And then when you add a new
block, uh this object browser lets you
type in text and search for things, but
you need to have most of the objects
checked and selected. So you could have
them all checked and selected. That's
not going to hurt you. It's just going
to give you the most options. And the
first thing we want to check for is an
add function. Okay, so add because what
we want to do is say if user and not
last user then add one
to position
and store that value in the position
variable and then we don't need a coil
on this rung. Okay, so this line of code
if user and not user add one to position
looks very visually different from this
if user and not last user then position
equals position plus one and if but
those are clones those are functionally
equivalent okay and just so I don't
forget because it uh comes at the very
end but we might as well set it right
now um we want the last network so the
bottom network in this to be set
last user user equal to user because uh
ladder logic reads top to bottom and
left to right. So every scan of the PLC
will start from one and go down to
however however many rungs we have in
our code and then at the end it will set
last user equal to user. So this line of
code will only ever be true once at a
time per button press and then last user
will immediately get set to the same
value as user. Hopefully that makes
sense. Uh now let's add a new rung here
to do these steps here which is if
position one then L1 if position two
then L2 and then all the way through to
if4 uh then four. And so let's just go
ahead. We need a new block here. We
actually don't need either of these
coils, but we need a new block which
will be checking for equality. So that's
EQ.
And uh you grab the EQ block and you say
if the position value is equal to 1 then
L1. And this rung of code is the exact
same thing as if position one uh then L1
from structured text right here. So this
is if position one then L1 equals true.
So we just need to copy that one 2 3 4
so that we can say okay now if two then
L2
if three
L3
and I somehow got them out of order.
Okay. 1 2 3 L3
and then if 4
L4. And now we need just one more rung
of code because if you remember from our
structured text, we had if position is
greater than four, then we need to set
position back equal to four. So we'll
put in a new network after this. And we
need two new blocks. We need to check uh
for greater than. So, we'll go to block
and we'll check uh GT is the code for
greater than. If you ever can't find it,
uh because they're not all named super
intuitively, you can just put a star in
there and all of them will show up. And
a little bit of scrolling, you'll see GE
is greater than or equal to. GT is just
regular old greater than.
and we'll say okay if the position
variable is greater than four. Now the
difference here is we don't want to
change a output based on this. We
actually want the next uh block in the
rung to be controlled by this because if
we just hit another block and we grab m
ov which is how you move a value into a
variable and we say this is when we're
going to want to move zero into the
position value like that. problem is we
need this tied to this. We can't use
enable output. This is always going to
be true. We need this output to be tied
into that. So to do that, you delete
this little tag. You right click on the
body of the object and you say new con.
Nope, it's set output line.
There we go. Set output line. Okay. So
this is what you want it to look like to
actually say if position greater than
four go move zero into position. But
these 11 lines of ladder and you can see
it's quite a bit more scrolling than
this little view of structured text but
functionally identical setting these
same preliminary values and then
sequencing through the PLC. And just so
you can see that it's functionally
identical. I'm going to go ahead and
delete this out of this uh out of this
structured text. And then I'm just going
to delete actually I'm just going to
delete main out of the routine so that
the only thing on here is this ladder
routine. And now I'm going to download
it to my PLC. I will compile it.
And I've got it up here. Make sure you
got good view. Okay. Push the button.
Blue turns on. And we're cycling through
it exactly like the structured text that
we had at the start of the video. Okay.
So, I know it uh might seem a little odd
to rather than build something from
scratch, compare it to structured text,
but I really think if you're a PLC
programmer, uh it's really important to
understand how ladder logic correlates
to structured text. Those are by far the
two most common PLC programming
languages that drive entire factories.
So, knowing both of them is a really
good idea. I hope you found this
introductory tutorial useful. Be sure to
let me know in the comments below what
you want to see next on the channel. Um,
that's going to do it for today's
starter video on Ladder Logic. Thank you
to Arduino for providing the hardware
used in this video. So, Arduino once
again is making uh things more
accessible for hobbyists and makers. So,
thank you very much to Arduino. I'll see
you next time. Thank you so much for
watching. Thanks. Bye.