In this tutorial, I break down User-Defined Types (UDTs) — one of the most powerful tools for writing clean, scalable PLC code. If you've ever found yourself copying the same data structure across dozens of blocks, this one's for you.
We'll cover: What User-Defined Types (UDTs) actually are and why they exist How UDTs let you create custom data structures for your projects Using UDTs inside Data Blocks, Function Blocks, and arrays How UDTs keep your code consistent and dramatically easier to maintain Updating a UDT once and having changes propagate across your entire project Live demo in Siemens TIA Portal (concepts apply to any platform) Whether you're managing a handful of motors or scaling up to hundreds of identical stations, UDTs are how you stop repeating yourself and start programming like a professional.
Hello, and welcome back to the channel.
And today's video will be continuing our PLC tutorial series.
And, uh, today we'll be looking at user defined data types, UDTs, which are a
super important concept in PLC programming.
Okay.
So today's tutorial is going to be a little bit different because I've been
doing a lot of code along tutorials in this series, but today's video is going
to need to feature a fair bit of structured text, which I haven't gone
super deep into on the channel.
Um, and so I think what would be more valuable than sharing, uh, the writing
of this code line by line is talk about the concept and show it in a practical
use environment, and then I'll explain to you the code I generated for this.
Um, so to start very high level with the, the concept, a user defined data
type is essentially a grouping of tags that you want associated
with a shared type of device.
So a very common example is in a big chemical plant.
You'd make a user defined data type for every valve, uh, every pump and motor.
And sometimes you go even more generic and you say every two state device is
going to have auto manual mode.
It'll have an interlock bit.
It'll have auto manual commands.
It'll have alarms, overrides, and a whole bunch more features.
The every single device of this type is going to have these things.
So you bunch a bunch of features together for shared objects in a data type.
The example that I'm going to show with you guys today is going to be alarms.
This is very common, regardless of the size of your system to want a bunch
of shared alarms, uh, in your, uh, factory.
So if you're building a little standalone system, there still could be 15, 20
things that go wrong with it.
And if you're controlling an entire factory from this PLC, there could be
hundreds or thousands of things that could go wrong with it.
And so you want to start by defining some things that every tag, uh,
every alarm might have in common.
So in TIA portal, there's a folder called, uh, PLC data types over
on the left in the system tree.
Um, in Rockwell, I think there's a section called UDTs user defined data
types and most other PLC platforms.
It'll be pretty clear.
It'll be something that says data types.
Uh, and when you make a new one, you go to add new data type.
I've already generated the base code for alarm.
Um, and here's where I think it's really clear what's going on.
You're saying, what does every alarm have?
It's going to have some event that's going to trigger it initially.
And then it's going to need to give me the option to acknowledge that
alarm and say, yep, I've seen it.
Because when you trigger an alarm, the most normal response case is that a
light would turn on to say, Hey, I'm in alarm state, you would shut down the
until someone had come over and said, I see the alarm.
It's okay to resume.
So then there'll be this bit tracking.
If that alarm is active, if it has not yet been acknowledged, and then when
someone does try to acknowledge it, we need to retain that bit because there's
a chance someone hits acknowledge, but the trigger condition is still active.
In that case, we need to see that it's been acknowledged, but not yet cleared.
And then just practically speaking, we'll want to keep a track of the last time.
Uh, the, the timestamp that the alarm triggered, this is really useful.
If you were to tie this to an HMI or some sort of SCADA software, so
you can track alarms over time.
Uh, and then we'll just give a couple identifying things.
So ID and message could just be like enumerated one through 20 and message
could be like, here's the thing that just happened.
So you put that information again on an HMI.
And we'll say every alarm in our system, we want to share these and
you can see, we have Booleans, we have long date time, we have integer
and string tags, all grouped into this user defined data type, because
we say, no matter how many alarms we make, we want to keep
track of all of these alarms.
And then to show where this gets really useful, we make a data block.
Um, we make a global database, apologies.
Uh, and we say, Hey, we're going to store all the information about all
of these alarms inside of this database.
Uh, so I go and make an array of 20 alarms and right off the bat, I get 20
individual objects that all have the ability to be set up here.
So, um, initially I have one of these tied to a physical push button.
I have another one tied to a PLC bit.
I could come right in here and I could say, this is actually my third alarm.
It doesn't matter.
Um, and then we'll say, and this ID is two and you can see this is the
default timestamp it loads in at.
So this has never been triggered.
Okay.
But I get all of this information for 20 objects, 21, really, just by
making an array from zero to 20 of alarm.
So I created that data type and now I can make infinite numbers of that data type.
And I get all of those tags populated every time.
And then I made one individual outside of the array to say current alarm, because
a really common thing in big systems in particular is alarm fatigue.
And so many alarms go off when something shuts down that you lose sight of what
has happened most recently or what the most critical one is.
So I made a separate one.
And for the sake of this demonstration today, this will be really valuable
because we'll be able to see whatever triggered last.
So you can see the last alarm that I, I triggered was the, uh, button trigger.
Um, the one that's tied to the button.
So I'm going to drag that off to the side.
We'll pull that back up later.
Um, but now we've created a bunch of these objects, right?
They live in this DB alarms, but we're not doing anything with them yet.
And this is where the structured text is going to come in.
So buckle up.
Um, an important thing to understand is for loops are not super easy to do in ladder.
You can do something kind of brute force with like counter variables and move blocks,
but almost the whole reason structured text became a thing.
And in Siemens it's called SCL structured command language.
It is Siemens version of structured text.
Uh, all the syntax people can calm down.
Um, the reason structured texts kind of emerged as a viable PLC programming
language is because loops and other sort of more advanced programming concepts
are really tricky to do in ladder.
I think Rockwell does have a four, uh, block, but honestly, if you can pick
up a little bit of structured text, you're going to be a better PLC
programmer anyways, and I'll talk you through what's happening here.
And let's talk through how you would want alarms in your factory to work.
Any alarm goes off in my system and I want it to activate the alarm light,
the, Hey, you have an alarm and it hasn't been acknowledged light.
So that's my light zero right here.
And then what I also want to do is I want to move whatever the last alarm that
occurred is into my current alarm field.
Okay.
This is partly for me for this tutorial.
So I can keep it up over here on the right and show you, uh, how this code
works, but this is also a very real world scenario where you'd want to keep track
of whatever the last alarm that went off was and show that on the HMI, or you
might want to do it, uh, where the first, right, the oldest unacknowledged alarm
was that might be more useful if you're doing like root cause analysis.
Um, anyways, you can do that pretty easily in structured text.
And, uh, what we're going to do here is we're going to talk through the code
that I have set up in my alarm processing routine.
And then if you're doing this and kind of a follow along fashion,
feel free to pause and copy it.
Um, but anyways, the first thing we're going to do is we're
just going to check the local time.
This is useful when we're doing time comparisons later, and then we're
going to reset the light to zero.
So the way functions work, and this is in a function, not a function block.
The way functions work is they don't have memory.
Uh, so we're going to start every scan of the alarm logic as if there is no alarm
active, and so we're going to turn the light off unless we find that one of the
alarms is active as we iterate through on our for loop and that's when we'll turn
the light back on as we'll see in a bit.
Okay.
And then latest time and latest index are just going to be, uh, they're going
to be checkers for us to see if we've iterated through the whole loop.
And if we found alarms that have gone off more recently, um, then the kind of initial
time, right?
The default time when you first set up a new tag.
Um, okay.
So hopefully that made sense.
These are all just instantiating values.
If you've seen any like C plus plus programming or Python programming,
Arduino, hopefully this looks pretty familiar to you at the beginning.
Now, the meat of what the alarm handler is doing for us is it's iterating from zero
to 20, right?
Because we made an array of zero to 20 alarms.
So important to know.
If you made a new array with 50 new alarms and you called them something
different, you do need to make a new version of this, or at least update this
counter with your new array size and you need to point it at the right input tags.
Okay.
Um, but anyways, for zero to 20, which is going to iterate through all of our alarms
in our database of alarms, first, we're going to check if the trigger is active.
Uh, so if it's actively, um, having the alarm condition trigger it, and
it's hasn't already been active.
So if the triggers on, but we've already seen that it's active on past cycles,
doesn't make sense to do all this initial stuff again.
But what we'll do then is we'll say, okay, active is true.
We'll reset acknowledged because even if for some reason it had been previously
acknowledged, the alarm is still occurring.
So we'll make that false.
And then we'll set the timestamp that this alarm has last gone off at as temp time.
So this will give us a snapshot of the time that the alarm triggered.
Then we'll also say if acknowledges true, or this is something I forgot
to mention, acknowledge all.
So what I have physically is two push buttons.
One, I'm going to use to trigger one of the alarms.
The other one I want to use as an acknowledge all button.
Okay.
If you've been in an industrial environment, it's very normal for a cabinet to have
like an alarm acknowledge push button on the outside.
So I have an acknowledge all push button here.
So if I acknowledge just that individual alarm or I press acknowledge all, then I
want acknowledged, acknowledged past tense to go equal to true.
And then I can reset acknowledge equal to false again.
Acknowledged is that intermediate value.
That's checking to see if acknowledged has been true.
Then I should reset the thing.
If trigger is not currently active and acknowledged is true, then we want
to set active back equal to false.
And we don't really need an, and if active check here, because there's no
harm in setting a value that's already false equal to false again.
So just keep it clear and simple.
Then again, if we get any alarm that is currently active, let's have
our active alarm light just be on.
And then this little bit here is just for us to be able to track the most recently
triggered and active alarm and move it into our global alarm.
So that's this line down here, whatever our latest time and latest alarm indexes.
Let's go ahead and copy that whole thing into the current alarm.
And then the last thing we'll do is we'll assume at all is equal to false, right?
So it's a push button.
So if we detect that it's true, we'll go ahead and reset it at the end.
This we could actually delete most likely now that I'm reading it to you, but we're
going to leave it as is because I know this is working.
So I put all that logic in a function I called alarm handler.
And then in main, I call two things.
I call the alarm handler and then I call another function called alarm setup.
And this one I made purely for the sake of demonstrating how this works.
Okay.
So, like I said, I have two push buttons in the field.
One, I'm the first one tied to zero on my PLC.
I'm going to use it to simulate the trigger for alarm zero.
And then I made a memory tag called trigger one.
I'll use that to simulate triggering alarm one.
And then my other push button tied to input one.
I'll use that as the acknowledge all signal.
Okay.
Again, I hope this is making sense in terms of why to use it.
I set up alarm zero and alarm one to be a button trigger and a PLC bit trigger.
So that when I trigger them here on the left, you'll see them populate this
current alarm on the right, and you'll get a good sense of the benefit of a UDT.
So if I push the start push button, you can see on the left side
there that activates alarm zero.
In the field, my output, my light is on now.
And if I go ahead and press stop all that'll trigger acknowledge all the light
is off and what you can see in current alarm here in the bottom right is we
got an updated timestamp and it says, Hey, the button trigger was the last
alarm to go off.
Now, if I modify to one, what's cool is the light just turned on in the field.
So the same output triggered, that's my, Hey, an alarm is active
and hasn't been acknowledged yet.
And here you can see PLC bit trigger, uh, is the last alarm that got
triggered and the ID is one and I got an updated timestamp.
So again, I'll press acknowledge, but it's not going to actually clear it
because the trigger condition is still true.
If I'm modified to zero, the trigger condition goes away.
Now I can clear the thing.
So I'm able to generate a lot of alarms with shared logic very quickly, and they
all have a trigger and acknowledge and active acknowledge timestamp.
They have all these shared conditions and I know they're going to work the same
way, thanks to the UDT that I generate, but I can still reference the individual
tags under each of those alarms and use them to program unique functionality.
So if only one or two of these alarms need to open a vent valve or stop a
certain pump, I could still do that with these UDTs, but hopefully that's
a useful practical tutorial for you.
Apologies.
This is a little bit of a chaotic tutorial, but this is honestly a pretty
tricky one to teach because I didn't want to get super in the weeds on
structured text with you and the, the lines and the use cases between UDTs
and function blocks and instance data blocks do get a little bit blurry.
Some of it is just getting familiar with both.
And then the use cases sort of arise as you're building systems, but
essentially speed run a user defined data type is just you grouping tags
that you want every device of a certain type to have access to, to one master
data type tag, like an alarm that needs a trigger acknowledge active and timestamp.
It's also totally common practice to create UDTs data types for devices like
alarms, pumps, motors, valves, analog sensors, and combine that with function
blocks, so it's not either or user defined data types or function blocks
and databases, which we talked about in our last video, they can be combined.
And that's how you can make some really powerful, clean, well-structured code.
Okay.
So I still hope this practical tutorial was useful for you as always.
If you have questions or there are things I forgot to cover in this, feel
free to ask them in the comments below.
I'll get back to you as soon as I can.
Let me know what you want to see next on the channel in the comments.
And please don't forget to leave a like on the video, subscribe to the channel.
If you're finding this content useful, thanks for watching.
Good luck with your projects and see you next time.
Bye.