Ever wondered what a PLC is or how it works? In this video, I break down what a Programmable Logic Controller is, how it functions, and why it's the backbone of modern industrial automation.
From inputs and outputs to scan cycles and ladder logic, this video covers the fundamentals you need to understand before diving into PLC programming.
This video is perfect for anyone curious about automation, electricians looking to move into controls, engineering students, or hobbyists wanting to understand how factories and machines are controlled.
Topics covered: What is a PLC? How does a PLC work? Inputs, outputs, and the CPU The PLC scan cycle Where PLCs are used in industry PLC vs Arduino vs relay logic
Don't forget to leave a like on the video and subscribe to LeMaster Tech for tons more great automation content!
AI Generated·May contain errors
You may never have thought about it because it's one of those easy things to take for granted,
but something told that traffic light on your drive to work today when to change from red to
green. Similarly, every time you jump in an elevator, something is monitoring those buttons
to see what floor you select and then controlling a motor's ramp and acceleration to get you safely
to your destination. Likewise, almost every single product you can walk into a store and
buy something off the shelf was assembled and packaged in a highly automated high-speed factory.
The machines responsible for all of these calculations and controls going on in the
background and making everyday life possible are called PLCs or programmable logic controllers
like this one. And in today's video, I'm going to explain to you what they are and how they work.
Basically, the entire world of controls and automation and robotics and hobby electronics
boil down to inputs and outputs. Inputs are things about your environment like the state
of an actuator being extended or retracted or volume, temperature, pressure, flow. And outputs
are things that you can control to affect your environment like a motor or a pump or a valve.
There's a big need in industry to monitor inputs, make decisions or calculations based on that,
as well as track things over time like counters, variables, and statuses, and then use those
decisions to control those outputs. If you've ever tinkered with an Arduino or a Raspberry Pi,
you're probably pretty familiar with the basic concepts we just talked about. But in industry,
you need something that is going to scan those inputs, make decisions about them in the exact
same time, in a defined set amount of time, every time, and control outputs very repeatably and
reliably. This is where the PLC or programmable logic controller comes in. And if you take one
thing away from this video, it's keep this word in mind, determinism. The biggest difference between
an industrial PLC or industrial PC and a standard Windows laptop or MacBook is the repeatability and
determinism of the code that's running on those machines. Your Windows PC could stop to do
something like garbage collection that could create inconsistencies in how it's processing
logic. Your PLC will never do that. The way the code is written and compiled and the way the
hardware is built is basically a promise that the inputs will be taken a snapshot of at the very
beginning, it will process and execute the code in a known and repeatable way, and then it will
make decisions about the states to control outputs to, repeatable in a way that is exactly defined.
With this comes a bit of syntactic nuance and different programming languages even than
conventional computer programming is done in. Because the entire field and the need for PLCs
started as a need to replace these complicated intricate relay circuits, the most common way you
program these is with a language called ladder diagram, which actually looks more like an
electrical circuit than a common programming language. And these PLCs are used absolutely
everywhere. Oil and gas, pharmaceutical, food and beverage, paper and wood products, it literally
does not matter the industry, you pick one, PLCs are driving it. Theme parks and roller coasters,
PLCs are driving them. Elevators and stoplights, PLCs are driving them. The need for a highly
repeatable deterministic machine is why the PLC is used everywhere. Some of the biggest industry
manufacturers in this space are Siemens, Allen-Bradley, ABB, Modicon, Schneider Electric,
as well as some other manufacturers that make them for more focused specialized purposes often
like Keyence, Mitsubishi, Panasonic, and Beckhoff. But that's enough of a high level, let's go ahead
and take a look at some practical examples of how these PLCs work. Okay, for the practical side
of today's video, we're going to use a Siemens setup. So I have a basic affordable Siemens PLC
and their programming software open. That doesn't matter, what we're talking about today is how PLCs
work. So regardless of what platform your job or your industry or your career takes you to,
the stuff we're about to talk about is platform agnostic. Okay, so I have a brand new PLC program
open. There's no code running on my PLC. The only thing I've done in advance is I have two push
buttons and an output light. So I have two digital inputs, on-off inputs, and one on-off output we
can make reference to while we talk through some of these concepts. And then this main organization
block is generated by Siemens and most PLC providers when you create a new PLC program.
You can think of this as your main loop. So by default, every PLC has some master thing that
it references while it's doing code and logic execution, and that's your main function. So
typically, especially in larger PLC programs, you don't put any code inside main. All you do is you
call the other functions that you want to have execute code. And I'm going to add a new block,
call it a function, and I'll just say example function. Okay, so this will be very simple.
The way main typically works is you would have it execute the function example by calling it here.
And then inside example, we can put that code back in there, which is start push button,
light turns on. And we'll actually say, when the start button is pressed, turn on the light.
And when the stop button is pressed, you got it, turn off the light. And I'm not going to spend a
bunch of time teaching the ladder logic that I'm writing here, because I have tutorials on ladder
programming on the channel already, I really want to focus on how PLCs work. Okay. So to speak
briefly about scan time, which is the most important concept in PLC programming, the way a
PLC is going to execute is it's going to go to its main organization block. And it's going to execute
all code from top to bottom, left to right, just like you're reading a book, which means if I have
a function here called example, and I were to drop another function down here called example two,
it's going to do everything in this example function before it does anything in example two.
And it processes code in a deterministic way, like we said in the intro, it starts by taking a
snapshot view of what all the inputs are currently showing. And then it executes all of the logic
that lives on the PLC. And then it makes a decision about whether or not to change the state of
outputs based on that. It's actually doing these steps in that defined order. It's just doing it
at a cycle time, that's typically one to two milliseconds for a well structured small program.
So it doesn't even feel like it takes any time at all to the human. And so to show this, I'm going
to go ahead and activate this program, we're going to load this onto the PLC really quick.
And then I'll show you that the PLC actually tells you how long it's taking to cycle the code.
So if I go to example, and I go live monitor, I can press the start push button, and the light
turns on. And I can push the stop push button, and the light turns off. And to me, it looks
instantaneous, it looks like I pushed the button, the light turned on immediately. But that's
because if we go to online and diagnostics and go to cycle time, the PLC is telling me, hey,
I am running at one millisecond, okay, I'm doing an entire cycle processing everything I have on my
PLC. In one second, the longest cycle time it's had since booting up was two milliseconds. This
is great. And this is very, very fast for larger projects and complicated projects doing advanced
math, it is normal to see these get up there to 8910 milliseconds, you actually want to try to
keep this number down by writing efficient code. Okay, so every major PLC manufacturer gives you
some way like this to see your cycle time. And that's a good indicator of how your code is
running, how burdened how overloaded your PLC is. Now, hopefully that way we blocked it out of
take a snapshot of your inputs, do all your evaluation logic, and then make decisions about
what to do with your outputs make sense as an overall structure, how PLCs execute code to you.
So let's take a little bit more of a look at what the reading top to bottom and left to right
actually means practically, let's go ahead back into example. And let's put a few rungs of code
in here that sort of conflict with each other. Okay, let's say if either push button is pressed,
then I want the light to turn on. And if either push button is not pressed, then I want the light
to turn off. And you can maybe see already where the conflict could come in. What about the case
where one button is pressed, and one buttons released? Okay. And so again, the way this code
reads, even if you're not familiar with ladder is if start push button, or if stop push button. So
my two buttons, if either of those are pressed, turn on the light. But the next rung of code is
if not start push button or not stop push button, turn off the light, I'm going to go ahead and put
this on my PLC here. And we're going to take a look at what that means. Okay, and you can see
just when I go online to monitor it, neither of the push buttons are being pressed right now. So
the light is off. Now I'm going to push one of the push buttons, what do you think is going to
happen to the light, it doesn't turn on, okay, I can verify the light is off in the field, even
though the coil right here is lit up green saying, Hey, I'm trying to turn the light on. What's also
happening is the other buttons still not pressed. And so the light is still not turning on. Because
this rung of code is getting executed after this piece of code, which means it's saying,
all right, check network one, oh, I should turn the light on check, check network two, I should
not turn the light on. And then at the end of processing all of this code, its final decision
is do not turn the light on. If I hold both buttons down. Now the light does turn on because
it's start or stop, and not stop and not start. So if I release both of them, the light goes back
off. What's super interesting about this and a really good explanation of what's going on here
is if I put this to not and not down here, and I put this to on or on up here, flip these around.
Now what I've done is I've changed the order of the two rungs exact same code. But now I've said
if not start, start push button, or not stop push button, the light should be off. But then I've
said if start push button or stop push button, the light should be on. So now when I hold just
one light down, and it doesn't matter which push button, excuse me, push button, not light,
it doesn't matter which button I hold down, the light turns on in the field, I don't have to hold
both of them down. What happened here is we changed the order that those rungs execute in.
So we're saying even though these rungs are often in conflict with each other, where I have only
one button pressed in, then whichever one comes later in my program is going to win, you can use
this structure of left to right and top to bottom to your advantage as a programmer if you know what
you're doing. But it can also create a lot of headaches where you'll have overwritten outputs
if you don't know what you're doing. So frankly, a much cleaner way of structuring your code
is find a way to only control outputs from one rung of code. If you want to make it to where
start or stop turns the light on, don't have a different rung of code that turns the light off,
find a way to put all of the code that can control that output in one spot. All right,
so hopefully that's a clear example of how this code processes top to bottom and left to right.
Let's take a look at one more concept in the PLC, and that's the idea behind a cyclic interrupt.
What if you want something that does not execute as fast as humanly possible that the PLC is
continuously evaluating for? So it's usually generally considered pretty cool and useful
that the PLC can process something every millisecond. Let's go ahead and delete that
function and say we actually want something to be evaluated no faster than every half second.
I can make a new organization block and call it cyclic interrupt, and this is slightly different
in Allen Bradley. They do timed function blocks as well, but a little bit different to set up.
Again, not a syntax lesson, and we're going to call this half second, okay? I'm going to go
ahead and make this, and now I have a new organization block that's only going to be
evaluated every 500 milliseconds. So now I can say, let's just simplify and say when the start
button is pressed, turn my light on. Alrighty? And so what's pretty cool about this one we're
going to see when I download it is I can now click the start push button and the light won't turn on.
I'm able to click it fast enough that the light won't always turn on. Likewise, when I hold it
down and the light does turn on, I let go of the button, and there's a time delay before the light
turns back off. So I go ahead and click the button. The light does not turn on because I'm
clicking it in much less than 500 milliseconds, and it's only evaluating this rung once every
500 milliseconds. But if I hold it down for a half second, then the light does turn on,
and if I release it, there's a little period of time where the light stays on because it only
detects at its next 500 millisecond cliff that the button has been released. One important note
about these is it doesn't mean that there will always be a half second delay between me pressing
the button and the light turning on, or always be a half second delay between me releasing the button
and the light turning off, because I could be triggering that button anywhere in that 500
millisecond window. Likewise, I could be releasing the button anywhere in the 500 millisecond window.
So if I happen to be pushing the button and the code is about to scan the next millisecond,
it will still look pretty much simultaneous when it turns on, but it could take up to a half a
second. Again, there are actual PLC programming tutorials on my channel. You can check those out
if you're interested in learning more. The most important thing to retain from this video, so I'm
repeating it at the end, is PLCs are deterministic. They give you an exact way to know how your logic
is going to behave, and behave every time repeatedly and reliably. They're deterministic,
they're time-bound, they scan left to right and top to bottom, they take a snapshot of the inputs,
they move it into an image table, they then compute all of their internal logic based on
those inputs and variables, and then they make decisions about what outputs to change accordingly
based on those inputs in a scan. Hopefully this didn't get too in the weeds, and you found it
interesting, and you want to go to LomasterTech, like the video, subscribe to the channel,
and learn more about the world of controls and automation. Thanks for watching. Let me know if
you have any questions in the comments below, or what you want to see next on the channel.
Thanks for watching and see you next time. Bye.