PLC Programming: Functions, Function Blocks & Organization Blocks Explained
In this tutorial, I break down the three core building blocks of any PLC program — Functions (FCs), Function Blocks (FBs), and Organization Blocks (OBs). If you've ever been confused about when to use which, or why your program structure matters, this one's for you.
We'll cover:
What Functions, Function Blocks, and Organization Blocks actually are The key differences between FCs and FBs (and why it matters) How OBs control your program execution Instance data blocks and why Function Blocks need them Best practices for organizing your PLC projects Live demo in Siemens TIA Portal (concepts apply to any platform)
Whether you're just getting started with PLC programming or cleaning up years of spaghetti code, understanding these block types is essential to writing clean, reusable, maintainable programs.
Don't forget to leave a like on the video and subscribe to the channel for more automation content!
Hello and welcome back to the channel. In today's video we'll be continuing our PLC tutorial series
by looking at functions, function blocks and data blocks, and organization blocks. The basic
structure of how you create a large PLC program. So of all the videos that we've done so far,
this is the first one where much of what I'm saying will be Siemens specific and there are
a few other equipment manufacturers that use a similar structure to Siemens. And there are some
key differences in the Allen-Bradley Rockwell platform. I'm familiar with both so I will try
to call out what those differences are. Broadly speaking, the topics will still apply to both
and more universally any PLC that you're writing code for. But some of the specific syntax will be
Siemens specific in this tutorial. Let's start from organization block. The basic thing when you
create a new PLC program, whether you're in Allen-Bradley Rockwell or Siemens, you're going
to be given main. And it's going to be this special kind of ladder, this special kind of
routine on both platforms. Main is the PLC's main object loop. It is the start here and go as fast
as you can by default thing for both PLCs. So if you remember or if you've seen the how PLCs work
video that I published on my channel, you'll know that we can go into a PLC and we can see how fast
it's doing cycles. That's this cycle time or scan time, depending on what platform you're working
on. And it's telling us that it's doing a scan of the PLC logic every millisecond. So that's
1000 times per second. The longest one it's had is two milliseconds, and that was probably on boot.
So by default, any code that we drop in main is going to be getting processed as fast as the PLC
can. So if we do just the very simplest piece of code we could do to reorient everyone, I have two
input push buttons and one output light hooked up to my PLC, just so I have some tags I can write
code with. If I just basically said, hey, when I push this button, I want this light to turn on.
I can put that in main. Main is a spot where you can technically write code. But as we're going to
talk about next, organizationally, you don't really want to put any functional code in main.
All you want to do is call functions and function blocks from main for almost any system that's going
to be more than a few lines of code long. So I put that code in and now I can push the start push
button and I can see the light turn on in the field. So this is functional code. But when you
get to dozens or hundreds or thousands of IO points in a system, you don't want to put that stuff in
main. Instead, you want to create a new block. And here's where you need to decide between
functions and function blocks. And the number one thing I'm going to say, the difference between
functions and function blocks is, is memory. Functions process things every scan, and then
they make decisions about outputs and you can use them to calculate math and scale variables.
But they're not retaining anything in memory. They don't have anywhere to store calculations
and math that they did in the intermediate. So functions are instantaneous. So we'll just make
an example function here. And we're going to replace this line of code in main, which is an
organization block, we're going to get rid of this. And instead, we're going to call the example
function. So this is the main loop saying, Alright, go do this function. And then in this function,
we're going to evaluate here. And we're actually going to say, Okay, when I press the start button,
let's get rid of this momentary coil and say when I press the start button, set the light on. And
let's add a second rung of code. That's when I press the stop push button, reset the light or
turn it off. Okay, so we've got this little function that says when the first button is
pressed, turn that light on. When I've got the second button, turn that light back off. And then
from main, which is my organization block, I'm saying, Okay, call that function every scan of
the cycle time. Here, you can see, if I had hundreds of functions, this main organization
block can still stay fairly organized, because all it's doing is calling specific functions in
specific orders. And if I had a function for every valve, or every group of valves, or every pump,
or every motor, it's still going to be pretty neat from the main organization block to go and
just see what gets called and in what order. And you can dig into each function. And you can say,
Oh, I see, that's what this function is doing. That's what this function is doing. And they're
very focused on a small subset of groups and functions. So we'll go ahead and we'll download
this real quick. Okay, and now when I'm looking at main, I just see that the example function is
lit up green, meaning it's getting called. And if I go into example function, and I start monitoring
it, I can see that the energy right is highlighted left here is green, and it's making it to these
buttons. So if I hold down the start button, and then release it, I've got the light turning on.
And then if I hold down the second button, stop, it resets it and it turns the light off. So my
function is working great. And main my organization block is calling that function. Now there's two
other types of organization block, we're going to touch on super quickly. And then there's actually
a lot more types of organization block, but they're a little more niche and nuanced. So it's
not really worth a high level overview covering them. But if I go to organization block,
I'm going to make one called startup. And there's actually a specific type of organization block in
Siemens called startup. Alan Bradley, like I said, they don't have a special organization block for
startup, they have a bit that's like s colon f s. And it's a little different logics 500 versus 5000.
But they have a way to say do this only on first scan is what they call it. But the idea is the
same, run this cycle of code exactly once when it starts up. And the reason you might do this
is if you have a bunch of valves, pumps, motors and devices that you want to put in a certain state
when they start up. So like if I put the light here, and I said, Okay, the first time this code
runs, I actually want that light to turn on. Because by default, I'd like the light to be on
until I come in and press the button to turn it off. This is a spot where I could say right when
the PLC boots up, turn that light on, but then it's going to stop getting called after that
because it only runs on startup. Similarly, one other type of organization block I want to call
out here before we move on. And you can actually see here a list of all the organization blocks
that sort of come with to portal. But the other one I want to talk about is cyclic interrupt.
So you can have stuff that does not check that absolute as fast as the PLC can process,
which is like every millisecond. Here, I can say actually, I only want it to check
10 times a second. So every 100 milliseconds, or every half second, so 500 milliseconds,
I can define how frequently I want it to scan, because you may have issues with blipping sensors
or things that happen way too frequently. And it's just creating noise for your program,
or it's a little bit more processor intensive. And so it's actually harder for the PLC to process
it super frequently. Things like PID loops have to go in cyclic interrupt blocks, because you
can't update a PID 1000 times per second and expect the devices in the field to actually
respond to that. So a cyclic interrupt is where you could say like, okay, now I'm going to maybe
get rid of the example function code where the stop push button is turning the light off. And
instead, I'm going to put this in cyclic interrupt, which means it's possible I'll have to
hold that stop push button down for between, you know, one and 500 milliseconds, depending on where
in the scan cycle I am. So I could drop this in the cyclic interrupt and delete it from example
function. And now I'm going to go ahead and download this, what you'll notice is those other
organization blocks, I did not have to add to main because organization blocks have rules about
when they get called by default, but you don't have to do anything special to make sure they
get called. Half second we made to get called every 500 milliseconds and startup is going to
get called when we boot the PLC up. So I'm going to download this and a recap of everything we
should have happen, we're going to go ahead and continue, we're going to have to restart the PLC
and we should have the light turn on. And then we should be able to hold down the stop push button
and that'll make it turn off. But if I just click the stop push button, we're not necessarily
guaranteed to have the light turn off. Okay, so I can sit here and I can click the stop push button
momentarily. And it's not turning the light off by default, because sometimes I'm clicking it
way too fast. But if I hold it down, I make sure that it gets scanned in that half millisecond
time, and that actually gets it to turn off. Okay, so that's kind of where we're going to call it on
an introduction to organization blocks, right, we have a startup one that puts that light or
pumps valves, motors, devices, in startup state, whatever you want them to default to when you
first boot a system up. Main organization block is where we call all the functions and function
blocks that we want to have scan every loop of the PLC. And you can use cyclic interrupts for
things like motor control, PID loop control or anything where you need a slightly slower scan
time. Now we've started covering functions a little bit, let's go ahead and do a little bit
more, right, we can do something beyond just check if button pressed and set light on something like
a very common thing might be scaling a analog value. So if we had if we had a value that was
coming in raw from a PLC zero to 27,648, you know, basically an unscaled electrical
signal on an IO card, we could drop a calculate block in there and say let's take our raw input
and we'll we'll make one up for this. It's not a wiring lesson. Let's go ahead and define a global
memory. Word, let's say word, and it's going to be an integer. And let's just say that we have this
raw input coming in. And we want to divide it by 27,678. Like I said, 27,648. Sorry. And then we
want to multiply it by 100. So this is basically like a very simple scaling function. Alan Bradley,
they will call these compute blocks instead of calculate blocks. But the idea is the same,
you can take this input one, you can divide it by what's your max span of the unprocessed signal.
And then you can multiply it by 100. So you're basically getting a zero to 100 value, not by 100
by input three. There we go. Okay, and we'll put this in another tag that we'll call scaled input.
Okay, and we'll define that as a global memory as well. And that'll be the next integer. Okay.
Alright, so this is a good example of something you might use a function block for there's no
need to store this in memory, because it's something that's getting calculated every
scan of the PLC. So if you had a temperature here, that's constantly fluctuating, you want
to recalculate what the scaled input is every scan of the PLC. One other little syntactic thing,
it's important to call out is because this is calculating an integer, right, we made that a
word. So it's not a real, it's not capable of holding decimal points, we actually want the
internal math to be done as reels. And Alan Siemens does this really nice thing with the
indicated with these little squares that it's doing implicit type changing. So it's taking an
integer value, it's converting it to a real, it'll do real math, and then I'll convert it to an
integer on the outset. Not every PLC platform does that. So you know, if this was just an integer,
it would be doing this in one divided by into and then rounding to the nearest integer. But the
whole point of this is it's taking something that could be up to 27,000, scaling it back,
and then multiplying by 100 to basically get a zero to 100% percentage. So we want to calculate
a real, just a little bit of PLC programming fun there for everybody. And let's go ahead and send
that live. Basically, now if we put in what should be about 50%, 13,500, you can see the
scaled input is 59. But if this was some device in the field, and oh, it's a pressure spike or a
temperature spike, and goes up to 1800, it gets recalculated as a 65. So scaling of analog values
is a really great example of something where a function does everything you'd need it to do.
There's no need for function blocks here, because that's an instantaneous calculation that you want
to have done every scan of the PLC. So that's a decent explanation, I think of functions.
Now let's talk about when you do want function blocks. But the most important use case is to
keep in mind, do I want memory? Do I want something that gets stored and retained between cycles?
Pretty simple analogy that I've seen before and had explained to me before is a function
is like a vending machine, you walk up, you put in money, you get whatever you ordered out.
And a function block is kind of like a bartender where they remember your favorite order, and you
can walk in, and they remember what you got last time. And they probably just start making you
your regular. So if you're an analogies person, maybe that helps. But let's use an example of
I had not just one light hooked up, but let's go ahead into PLC tags. And say I had two lights
hooked up. Or, you know, really, you can put yourself in the shoes of like, hey, maybe I have
hundreds or 1000s of devices in my system. So I have so many pumps or motors or valves, and I want
them all to share sort of base code, I want them to function in the same way. So now I have light
zero and light one. And I want to write just a light function that is going to handle some code
for all of them. So I'll just call this light and I'm going to make it a function block. Okay. And
the thing I want to retain the memory that I want out of this function block is how long each light
has been active. Okay, so I want to keep track of my input, which I'm going to come up here in the
top section, which is where I can define inputs, outputs, things that need to both get passed in
and go out. And then purely internal things, things that are going to get used inside of the
routine only, as well as temporary and constant values. Now for the input, I'm going to say,
well, that's actually just going to be the light input. Okay, so I'm going to tie whatever light
this is keeping track of to this light input parameter when I call the function. And then
the main thing I'm going to output is going to be runtime minutes. And I'll make it a real so we can
keep track of fractional minutes. And then in here, I'm going to make a very simple little bit
of code, I'm going to check when my light input is active, I need the pound sign. But so we're
going to have this light input, and we're going to have it activate a retentive timer. Okay,
and if you're following along with the series, you're probably familiar with the to n r, which
is a timer that keeps counting up, and will retain how long a timer has been active. And we'll call
this runtime. And we're going to make it a parameter instance, because it's happening inside
of a data block. So we want it to get generated for every data block like this. Again, single
instance, multi instance, parameter instance, these are all just options of creating timers,
the alternative would be you have to generate a new one of these outside of your function every
time you made a new one of these. So we're going to make it a parameter instance, and it's going
to automatically get dropped in out, we don't actually need it getting passed in or out. So
we're going to move it to static for this video. And then I'm going to make this elapsed timer,
I'm going to make it 10 minutes. So that's 600,000 milliseconds. And then we're going to come down
here and we're going to move that time value of elapsed time, we're going to go ahead and move it
into an intermediate value. So we'll take runtime, hashtag runtime dot elapsed time,
and we're going to move it into an intermediate value that we'll just call runtime milliseconds.
So this way, we can have a tag, this will also be local static, we can have a tag that is in data
type int, that'll be fine, didn't just to make sure it's big enough. And now we have a millisecond
value, we can do math on. So then the last thing we'll do is we'll make a calculate block. And
we'll take runtime milliseconds, and we'll divide it by 60,000. Okay, and this will tell us our
runtime minutes, whoops, runtime, underscore minutes. Alrighty, and then the function for
this is just going to be in one divided by in two, like that. And I think maybe the only thing
we have to do here is say local output, and real. And I'm a little surprised I'm getting this at
all. Oh, yes, of course. So the reason I'm getting the red squiggly lines on this first one, and down
there is I accidentally put the hash signs in these tags when I define them up here, which is
not correct, you need the hash signs when you reference them down here, those pound signs, but
you don't put them on the tag names up top. So sorry for slipping these tutorials are a lot of
content very fast. So everyone makes mistakes. Okay, so to recap what we're doing here, we're
going to pass in a value of a light, we're going to count up how long that timer has been counting,
convert it to a millisecond value, divide it by 60,000, and report back the runtime minutes.
And we have two lights now. So we'll go into example function. And we'll say,
all right, start push button is going to be tied to light one, or light zero, and the stop push
button, let's say will be tied to light one, I'll make these both momentary coils. And I'll just go
ahead and delete half second and startup, we don't need those anymore. So basically, I'll even delete
this calculate block, we really don't need that either. So basically, the total code in the system
now is when I'm holding start push button down, the light zero should turn on when I'm holding
the second button down, like one should turn on. And I have this function called light that should
give me a count up counter for both those lights. But I have to do this last important step,
similar to the original function, I have to call them both. So here, I'm going to say light zero.
And you can see when I dragged that function block that I just defined into main to get called,
it automatically said, Hey, you need to make a data block to store the memory associated with
this instance of your function block. So I'll call this light zero data block. And then I'm
gonna make another light. And this is where you see the power of function blocks. I'm gonna make
another light and call it light one data block. And I'm going to tie this light input to light
zero. And I'm going to take this light input into light one, okay. And now I'm going to download
this PLC function. And if we did it, right, that's a big if I should have two different timers here,
that when I hold down the two different buttons, they're both going to activate
different retentive timers. And I get the this functionality of seeing how long they've been
running for free twice, even though I only wrote the code once. So I'll hold down one button here,
and the timer does not count up, I'll hold down the other that timer does not count up. Epic.
Okay, so most likely, we got the wrong type here. So yeah, so we're calculating a dent,
which means an integer value, which means it's going to be rounding to the nearest whole minute,
which is why nothing happened on the seconds order. Let's go ahead and make that a real
and download. Alright, but let's go back to main now that we're calculating reels. And actually,
you can see immediately we have these fractional values already, which is cool. So let's try this
demo. Let's hold down light one. Okay, and now let's hold down light zero. All right. And
hopefully, you can see this is a very simple example, because I'm trying to go fast for this
YouTube tutorial. But we got retentive timers to tell us how long these lights were running
for free by writing general code generic code in a function block that can be extrapolated away to
many instances. So if I made 1000 light objects, I could call all of them from main. And I would
get this counter of how long they've each been running just by tying the correct input to each
of those data block instances. So that's kind of the main reason to use function blocks and data
blocks instead of just plain old functions. That's how organization blocks work. And again, PLC to
PLC, these individual things can get called something different, but the main way they work
and the main thing they do are pretty universal across every platform. So I hope you found this
tutorial useful. I hope you've been enjoying the controls and automation series. As always, let me
know in the comments if you have any questions or what you'd like to see more of on the channel.
Please consider leaving a like on the video and subscribing to the channel and we'll see you next
time. Thanks. Bye. Transcribed by https://otter.ai