In this tutorial, I break down what a state machine is, why they're essential for writing clean PLC code, and then we build one together from scratch in Siemens TIA Portal. If your ladder logic has ever turned into an unreadable mess of interlocks and latches, state machines are the fix. We'll cover: - What a state machine is and why PLC programmers use them - The core components: states, transitions, and actions - How to structure a state machine in ladder logic - Common pitfalls and how to avoid them - Live build of a working state machine in TIA Portal
Whether you're new to PLC programming or looking to clean up years of spaghetti code, state machines are one of the most valuable tools you can add to your programming toolbox.
Hello, and welcome back to the channel.
In today's video, we're going to cover a super important concept in the world of
PLC programming controls programming called the finite state machine, or
sometimes just state machine in a very basic setting.
Sometimes a state machine is also just considered like a sequencer or sequential
logic.
They overlap a little bit in small systems, but when you get into large,
complex systems, a state machine is really like a hierarchy or a fundamental
way of structuring your code.
That is super scalable, easy to debug, easy to write additional code onto.
So let's start a little bit more high level and talk about what is a state
machine.
And then I'll show you a practical example of one together.
Okay.
So let's talk about a very simple example of something that we could create a
state machine to control.
And let's take a look at of an industrial setting.
Okay.
We have a tank, right?
We have some vessel that we want to be able to fill with material.
And then we want to be able to blend it and mix it up.
And then we want to be able to drain it and discharge material.
So I have all the IO, all the inputs and outputs used in this system shown here.
We're going to have a fill valve that will gravity feed raw material in.
We'll have a high and low level sensor, just digital inputs that tell us there
is material in the tank at different levels.
So we can see if we're below a low level set point or above a high level, we'll
actually have a mixer, kind of like a mixer descending into the tank to blend
the material.
And then we'll have a discharge valve that will also be gravity fed.
So we're not going to worry about pumps or things like that to transfer it.
We'll use gravity in this case.
And let's talk about how we want this system to operate normally.
Okay.
So we want the tank sort of sitting there waiting for a command.
Someone would hit start on an HMI or a push button.
Doesn't really matter.
Someone says, okay, start a blend.
And the system will check, make sure everything's good.
No faults.
We're ready for that command.
We're going to start by opening the fill valve until high level is reached and then
starting the mixer mix for some set point desired.
And then we'll stop the mixer, open the discharge valve.
And once low level is reached, then we'll close the discharge valve and return to
idle.
Okay.
So conceptually, you can see how this might make sense.
If you've been in industry or in a chemical plant a little bit, you've probably seen
blend systems.
They are very simple.
It's like raw material in.
Sometimes an operator will put a couple cups of additives in the top or you'll use
little diaphragm pumps for dosing.
You'll blend it up and mix it and then you'll send it on its merry way to go get
packaged out.
So this is a very realistic real world scenario.
Now to program this in a logical way, a well-documented, well-structured way, we
want to create a state machine for it.
And state machines, broadly speaking, are defined by states and transitions between
states.
Okay.
So for this system that we've just described, the states could effectively be idle.
It's sitting there not doing anything.
Filling where the mixer is off and the discharge valve is closed and only the fill
valve is open and the tank is building up its volume of raw material.
And then mixing where it's blending, no new materials coming in, no new materials
going out.
We're just mixing and blending and then draining where the mixer's off, the fill
valve's off, and we're just letting the blended material leave.
And then another state to think about is if something goes wrong, like the mixer
fails to start or a valve gets stuck open, that might go into alarm state.
So here shown in kind of the blue bubbles are what you could think of as the states.
And this whole schematic I'm showing here is known as a state transition diagram
because you're defining the states and the transition events that take you from one
state to another, right?
So when you're sitting there in idle, the only thing that's going to take you out of
idle the way I've described the system would be someone pressing the start button.
You could have an automated system where just like high level or replacing a tote or
something automatically triggers a fill.
In this case, we're saying a person indicates they want to start a fill.
That's a transition from the idle state to the filling state.
Now, if I'm in filling and something bad happens, I might need to go into alarm
state. But if a person says this was a mistake, call it off and they hit the stop
button, I might just go right back to idle.
But in normal circumstances, I've hit start.
I've started a fill and I've reached high level.
Then I will transition from the filling state to the mixing state again.
Here I could hit the stop button here.
Alarms could still occur.
But the normal behavior is I mix for a desired amount of time.
I reach my mix time and I enter the draining step here.
Once low level is reached, I'll go back to idle.
So a normal operation flow is idle.
Someone presses start.
I fill to high level.
I mix for a desired set point and I drain to low level.
But you can see just in this one tank 5.0 point simple system how these state
transition diagrams also capture a lot of unique edge cases.
And the PLC programmer needs to be thinking about these edge cases when they write
the code for it. So this state transition diagram actually does not look too
different from an IEC 61131 PLC programming language called SFC or
sequential function chart.
Sometimes I think mislabeled sequential flow chart.
But it actually is a programming language that uses this concept of states and
transitions similar to this.
It's not common in PLC programming.
Far more common is ladder diagram and structured text.
So we're going to stick with those.
But it is worth calling out that that is basically what an SFC is, is a document of
the states and transitions between the states.
And you need to write lower level code to actually automate the system.
OK. So enough PowerPoint engineering.
Let's take a look at what this actually means in a PLC program.
And because it would probably take, you know, an hour to write together.
I'm going to go ahead and talk about the state machine that I have already written
and walk you through it.
So let's go to main the main PLC.
Let me close this side by side real quick.
OK. And so what I have set up ahead of time for this demonstration is something to
handle the basic states, something to simulate as if we had these inputs and outputs.
This isn't a hardware tutorial.
So I'm not, you know, actually standing up a tank in my garage to show you guys this.
And then I have that running logic, that normal sequence built.
OK. So that's all that's in main.
Let's go to state handler.
Right. This is where I'm looking at my main states and the things that could transition
between primary states.
So active, just not being true, me not being in sequence is going to say I want to be in
zero. I want to be an idle.
Running will be our main control code.
This is where the things are happening.
That'll be when someone hits the start command.
We'll start it up. We'll say we're in active.
We're in run mode stopping.
You could think of 10, 10 steps later, mode 20.
One quick just sort of syntax thing.
It's not a good idea to program yourself into a corner when you're doing PLC programming.
So when you're setting up a program initially for a small system, maybe you want to take
jumps of tens rather than individual integers.
When you're doing a large system, maybe you would want to go from zero to one hundred to
two hundred or a thousand, two thousand, three thousand.
Just because as systems grow in complexity and age and features get added, sometimes you
want to add a step that's actually halfway between idle and running, right, like a
startup sequence.
And maybe you want to make that five or something so that these numbers still make sense
and are sort of sequential.
So that's why this is zero to 10 and not zero to one.
It's just sort of like a programmer's delight sort of sort of feature.
But anyways, so we have running, stopping and alarmed or faulted.
OK, those are kind of what you could think of as like your keystone modes.
And then we'll go into running and we'll take a look at what actually happens when we
enter running. So someone has hit start.
We've entered active.
First thing we do is we check.
Are there no active alarms?
If there are no active alarms, let's go start a fill.
If there are active alarms, let's go to that mode 30 and let's sit there until someone
clears them or acknowledges them.
Now we're in mode 11, right, which is just right after starting up the running sequence.
Now we're in filling and we're going to open the fill valve until we reach high level.
And then once high level is reached, we're going to move to step 12 and step 12 is going
to be mixing here.
We're going to start the mixer.
And again, if you've been following along with the PLC programming tutorials on the
channel, you already know this.
But using these momentary coils means we don't need any extra code to close the fill
valve. Just once this condition is no longer true, the fill valve will shut.
So these momentary coils imply if I'm in step 11 and I don't have high level, open the
fill valve. But it also says if I'm not in step 11 or I do have high level, close the
fill valve so that you implicitly get that code for using these momentary coils.
Hopefully that's clear.
Go watch the previous tutorials if you're unfamiliar with why we don't need to also
command the fill valve to close.
OK, but now moving on to look at the mixing step again.
We're turning on the mixer.
We'll wait to watch for mixer feedback.
So this is a very common convention in digital outputs for motors or pumps is you
command a PLC output to turn on a pump or motor.
But usually that could chunk some big motor contactor pulls in and you want feedback off
of that contactor that it has been pulled in.
So other than just looking at the output and saying, is the output on?
Ideally, you'd actually be looking at a feedback on that contactor that says, yep, it
pulled in and now I'm counting up in timing.
That's a normal source of alarms and faults is like, hey, I commanded this output to
turn on, but I never got feedback that the contactor pulled in.
And anyways, then we're going to time for some set point.
I have one minute in there right now.
This could be a dynamic set point.
It could be programmable from an HMI.
I just have one minute in there.
And when that timer finishes, we'll go to step 13.
Similar to the valve, we won't need to tell the mixer to turn off.
It will just turn off once we're no longer in step 12.
Step 13 will be draining.
It looks exactly like filling.
But now we open the drain valve until low level is met.
And then when low level is met, we're going to turn off the active bit and we're going
to go back to step zero, which is idle.
OK, so that's the normal sequence of events.
And you can see how each step handles this little discrete chunk of logic.
And it's really easy to see how from an HMI or a monitoring system of any sort, you can
always know exactly what your code thinks it ought to be doing just by looking at this
one tag.
This one mode tag will tell you exactly what it should be trying to do and what state it
should be in right now.
This is a lot cleaner than a world of dozens of interlocking booleans and all this logic
that's trying to happen at all time.
You just find what mode currently is and you go look at what it's trying to do in that
specific step.
Now, because this is a software setup and I don't have all this physical IO, I'll walk
you through the same logic I have here really quickly.
When fill valve is open, I have every 500 milliseconds, so twice a second.
I have add one to tank level, just this integer value that's tracking the tank level.
And then when the drain valve is open, similarly, I subtract one every 500 milliseconds.
So when I command the fill valve to fill, we have this value tracking tank fill and tank
drain. And then when the mixer turns on, I today tell the feedback to act as if that
worked properly.
And then when tank level gets above 75, we say, OK, we have high level.
When it gets below 10, we say we have low level.
So that's the that's the simulation logic I've built so that we can test this sequencer.
All right. So let's actually walk through what this looks like.
And then we'll just close the tutorial real quick.
So we're sitting here in idle.
System is just chilling.
Everything seems OK.
Nothing's faulted. A person comes along and hits a start button.
Now I go to step 10.
Now I move into step 11.
I'm in running.
And if I go in and monitor my running logic, you can see we're sitting here with the fill
valve open, waiting for high level to be reached.
So if I go over to my sim logic, you can see the tank level is rapidly counting up.
It's counting up twice per second.
So we'll hit 75 in no time.
But if I had an HMI set up for this and, you know, we'll do some HMI tutorials on the
channel soon, it would say, you know, filling wait for high level.
You'd have a graphic of the tank that is showing the volume counting up.
And then it just hit 75.
So now we're in mixing and you can see our mix timer is counting up.
The fill valve closed.
The level is holding at 75, which is what it filled to there.
And in 45 seconds, we're going to finish this mix timer.
And here you could is a good spot to say, like, you have the main logic of what you
want to be happening.
What you would also add to this code is like, what's a common issue you could have at
this step? It would be like the mixer is being commanded to be on, but I don't have
mixer feedback for five seconds or one second.
Like I gave the contactor a chance to pull in and relay that back to the PLC.
And so you could have a, oh, I commanded the mixer to start and I didn't get the
feedback. In that case, I want to go to my alarm state where I ask an operator to come
and address why the mixer didn't start.
But when everything's good and we get the feedback, we count up to 60 seconds, we get
to our drain state.
Now the mixer's off, the fill valve's off and all the system is trying to do is drain
until we get low level.
We can see the level hurtling down now, kind of opposite of the filling step that we
were just in. And in a couple of seconds, we'll be at 10, which will trigger our low
level indication and we should go back to idle.
Right. So this very simple logic actually would let us completely automate a filling
and draining sequence.
And it all goes back to there.
There we go. We hit 10.
OK, so we should be back in idle.
All right. We hit low level.
It moved to step zero and it reset active.
And now everything is just chilling, waiting for commands and transitions.
It all goes back to being able to define the state transition diagram for your system.
If you can create this diagram, writing the PLC code to actually do the thing is
relatively trivial.
And there are great ways to go through step by step and check for edge cases.
And of course, it gets more complicated as you get into dozens or thousands of I.O.
points and large interconnected systems with DCSs and SCADAS.
But if you can take every system and subsystem back to a state transition diagram and
say what sort of things should transition a device from open to close should move me
from filling to mixing to draining, then you can create well-structured, easy to debug,
easy to program PLC code.
And a big thing in our industry in controls and automation is writing code for the next
person who has to work on that system.
OK, over decades, it's very unlikely that one person will be responsible for all of the
control code for a facility, a state, a state machine and well-documented state transition
diagrams are a great way to show future programmers and people working on a system what
you were thinking when you created that system.
OK, so like I said early on in the video, in a smaller system like this one I've
demonstrated here, creating a sequencer where you just say like I'm in step 10, 11, 12,
13 is not super different from a state machine.
When you get to very large systems, you often have like zones of control that are doing
master things like, oh, OK, now it's time to fill.
And you'll have a mode that says I've gone from idle to fill, but you'll have a new
like sub step or sub mode that's like command all the valves to do a thing.
And they'll all be sub functions inside of the fill sort of master mode control.
So state machines can actually have state machines nested inside of them.
It gets very complicated at very large systems.
But if you're able to break it down to fundamentals, then you're able to figure out what
any system is doing.
So I hope you found this useful.
As always, if you have questions about what you saw in this video, feel free to ask them
in the comments below.
Please consider leaving a like on the video and subscribing to the channel and let me
know what you want to see more of on the channel next time.
Thanks for watching and see you next time.
Thanks. Bye.