PLC Programming Tutorial - Ladder Logic #2: Timers & Counters | Siemens TIA Portal Master PLC timer and counter instructions in this hands-on ladder logic tutorial using Siemens TIA Portal. This is Part 2 of my PLC programming series, building on the binary and Boolean fundamentals from Part 1. What you'll learn:
Timer On Delay (TON) - how to create time-based delays Timer Off Delay (TOF) - controlling output hold times Retentive Timer (TONR/RTO) - timers that remember elapsed time Counter Up (CTU) - counting events and triggering outputs
These instructions are essential building blocks in industrial automation—you'll use them in nearly every PLC program you write. Perfect for beginners learning PLC programming, electricians moving into controls, or experienced techs brushing up on Siemens ladder logic fundamentals. Watch Part 1: Binary & Boolean Operations ▸ [link] Resources PLC used in this tutorial: https://amzn.to/4p90T8d Support the channel: https://www.patreon.com/LeMasterTech Timestamps 00:00 Introduction 02:02 Timer On Delay (TON) 09:55 Timer Off Delay (TOF) 13:57 Retentive Timer (TONR/RTO) 18:35 Counter Up (CTU) 23:36 Outro
What's up, automation nation? Welcome
back to LMaster Tech. And in today's
video, we're going to continue our
tutorial series on ladder diagram, basic
ladder programming for automation and
controls engineering. Today, we're going
to be looking at the most common types
of timers and counters used in PLC
programming. We're going to look at TO's
which are timer on delay to F which is
timer off delay. T NR which is often
also abbreviated RTO or retentive timer
in other platforms. And then we're also
going to look at counters. Count up
countdown. They're pretty much the same.
So we're going to cover all of these
today. We're going to be using ladder
diagram. Of course the syntax is quite
different but the concept is exactly the
same if you're using these in structured
text or another PLC programming
language. This is meant as a standalone
tutorial. So the concepts in here don't
require prior knowledge of any of the
past tutorials, but we are also
compounding automation and controls and
ladder programming knowledge as we go
through this series. And as we dive into
these tutorials, if you're really
enjoying the content, be sure to leave a
like on the video, subscribe to the
channel, and if you want to help me grow
faster, do more hardware related
projects, feel free to check out my
Patreon down below as well. So to start,
as a reminder, my physical setup, uh,
which we kind of covered in the last
video, I have a 24volt power supply that
is powering a Seaman Sematic S71200 PLC.
It's running TIA portal version 17.
We're covering ladder diagram, not
Seaman's TIA portal. So the instructions
that we're covering in this video will
work for most platforms, but that's what
I'm using. And then for demonstration
purposes, I have a control box with uh
two hand switches and four output lights
that are going to be used for the
examples in this video. So if you want
to pause on this, my face is probably in
the way. If you want to pause on this
wiring schematic and build some similar
uh test bench, go ahead. I'll link the
PLC that I'm using in the description
below the video. Okay, so let's dive
right into it. Uh I'm going to go ahead
and delete out everything I have in this
code. Okay, so I've got all functional
code out of this program. The only thing
we're calling in main is the ladder
tutorials binary function, which is what
we're programming here, and it has no
code in it. And then if I show all the
PLC tags, all I have are my four output
tags for my lights and my two input tags
for my hand switches. I added those in
the last video if you're curious. Um,
but there's nothing on this PLC. So, now
let's dive into the timer. And we're
going to drop the uh empty box function
in here. And we're just going to start
with a to. The n is timer on delay. It
is the most common uh timer. And
honestly, you can do almost everything
you need to do in code with this. So,
we'll call it example to n. Um, let's do
ex all caps just cuz I'm crazy like
that. We're going to do an example timer
on delay here. Okay. And it's kind of
nice. It populates with the four
parameters you need to know about for
timers. There's on what condition do we
want this timer to start. That's this
in. Some platforms will call that en for
enable. Some platforms will have in for
input. But they're sort of universally
on what conditions do you want to start
the timer. Now PT process timer, preset
timer, uh it doesn't really matter.
That's your set point. Okay? It's called
PT in almost every platform, but that's
your set amount of time you want it to
count up to, right? So, a uh a timer is
going to be something that starts on
these input conditions, counts up to a
value, that's your PT, and then it when
the timer finishes, that will actuate
your output bit, which is just true,
false, has the timer finished? And
that's my Q. If you remember most
European and international standards to
avoid O's that look like zeros call
outputs Q's but uh like Rockwell Allen
Bradley uses an O there. They just say
like hey you'll figure it out um for
output but the Q is going to be what
happens when the timer is done. And then
the only other thing we haven't talked
about here is this ET which is our
elapsed time. So as the timer is
counting up to that preset time value,
it's continuously keeping track of how
much time has passed in the elapse time
field. So that's everything that makes
up a timer object. And you might have
seen when I made this to block, it
created a database of this example
timer. And that's because you have to
create a little pocket of memory to
store these variables. There has to be
the input bit, the Q bit, the output
bit, the preset timer, and the elapse
timer stored somewhere. So that's living
in a timer database that's called
example timer. So let's say what do we
want uh to have actuate this timer to
have it start. Simplest thing we can do
is just say when I turn hand switch one
on. So while hand switch one is on. And
if you missed the previous video on
binary conditions, um this means that
the whole time hand switch one is on, I
want the timer counting up. So not just
when I turn it on, which would be like
this P, which would be the positive
transition of it turning on, but the
whole time hand switch one is on, I want
this timer counting up. Now, we're not
going to put anything in elapsed time
because it's a purely internal thing
that's keeping track of how long the
timer has been active. But we'll put two
two seconds in preset time. So we'll say
2,00 because the default unit is going
to be milliseconds. But what you see
when I hit enter is that it says t pound
sign2s.
Okay. So the seaman's PLC is taking that
value inferring that I meant
milliseconds and it's making it t pound
2S which is the seaman's PLC standard
for a time parameter. in like Alan
Bradley, it's just going to leave the
millisecond value you punch in there. If
I were to say 2 and a half seconds, if I
were to type in 2500 here, it's going to
give me this t pound sign 2 seconds
underscore 500 milliseconds. It's
honestly kind of annoying. Um, but you
can even delete the s and the underscore
and just say t pound sign 2500
milliseconds. That part doesn't matter
too much because if you want, you can
always just throw a millisecond value in
there and TIA portal generally is going
to translate it for you. Most PLC timers
are expecting you to punch in a
millisecond value there. So, let's go
ahead and change it back to 2 seconds,
2,000 milliseconds. We're not going to
put anything in the elapse time. And
then we're just going to add when the
output finishes, when the timer
finishes, let's go ahead and turn on
output light one. Okay, so I've got my
box here. I think it looked pretty good
last video just keeping it on screen.
Let's download this to the PLC. We're
going to hit these little glasses and
play button which is live monitoring.
And uh every PLC platform generally has
some form of live monitoring the logic.
So just make sure to hit the button for
yours. And now we're going to look when
I turn on hand switch one, we should see
a timer start counting and after 2
seconds the light should turn on.
Okay. And it's beautiful when it works.
When I turn the hand switch off, it's
going to turn off right away. And what
you could see when I turn this on is the
timer starts counting. Maybe you could
see the elapsed time value starts
counting up there. So elapsed time is
the number on top that's counting up 0,
250 milliseconds, 1 second, 2 seconds
until the light turns on. So that is the
basic functionality of a timer on delay
is conditions trigger it to start
counting up to some preset time and when
the preset time finishes you can use the
output bit of that timer to turn on the
light. Now an interesting derivation off
of this but super useful if you want
your timers and your commands for your
timers in different spots you can
reference this example timer.done done
bit, you can reference that bit on a
separate line of code. This will work
exactly the same way. So in a really
large ladder program, you might want to
create a timer based on a certain input
in one spot, but then use the output of
that timer in another spot. You can do
that with this uh sort of referencing
the timer output elsewhere. So if I turn
the hand switch on, you'll see the timer
still counts up, the timer done bit will
become true, and then the output light
will still turn on. So you can reference
that output light elsewhere. One
additional thing to mention while we're
here, you can look for a uh let's say
greater than block uh greater than and
we'll say get rid of the x timer.done
bit here. We'll say now if x timer it
uhelapsed time is greater than 1 second.
Uh and now let's also say just to get
tricky with it, let's say and the timer
is not done. Okay. Then we want the
light to turn on. This is us looking at
the elapsed time and saying, "Okay, it
the timer's been active for more than 1
second, but hasn't yet finished. That's
when we want to turn on the light." So,
we're creating a program that if it's
working correctly, should turn on when
the timer gets to 1 second. So, halfway
finished, and then turns off after 1
second. So, let's take a look.
Okay, that's kind of a cool example of
using elapse time and the done bit and
you kind of get a uh multi-state output
out of that. So that's sort of every
component of the basic to n timer on
delay block. Now let's look at to this
is going to go really fast because it's
very similar. Okay, so let's go ahead
and put this back just as the very
simplest version which was just when the
timer finishes, we'll turn output light
one on. And now we'll start making
something that instead is going to look
at hand switch 2.
And here's where it's kind of nice to
have the box. We can leave all our
previous timers in place. And now we're
going to type in to okay timer off
delay. And we'll do example to. Okay.
Tof though timer off delay. And uh
similarly, but to show that it's
different, we'll make this one 3000.
Okay. So we'll make this 3 seconds. It
has the exact same in uh preset time,
elapse time, and Q. It It is still a
timer block. It has all of the same uh
values, inputs, and outputs and uh
variables as the TO. It's a timer block.
Main difference between the TO and the
TOF is that this output will become true
as soon as the input conditions are true
regardless of how long the preset time
is. But they will stay active for a
period of time after the input
conditions are active and then go away
after that timer. So timer off delay
means outputs energize immediately when
the input conditions are true and then
the output is retained for a period of
time afterwards. So timer off delay.
Let's go ahead and send this one that's
going to turn on output light two just
so you guys can see exactly what that
looks like. Okay, so I'm going to go
ahead and trigger output uh hand switch
two. And you can see the light turns on
immediately. And now I'm going to turn
hand switch 2 off. And now you can see
the elapse time starts counting up. And
3 seconds later, output light 2 turns
off. So if we were going to do something
like we did a little bit earlier, which
was use the elapse time. So if we do a
greater than and we use the elapse time
on our example to example to f.elapse
time and we say that when it's greater
than500
then we want to turn let's use output
light three so we can leave output light
2 exactly how it is. When the timer the
elapse timer is greater than 1500 we
want uh output light three to turn on.
So let's go ahead and send that. Right.
So with that active, you can see the
hand switch is uh not active right now,
but output light three is on because the
elapse time has hit 3 seconds. So the
timer finished and elapse time has hit 3
seconds. Let's go ahead and cycle hand
switch 2 real quick and talk about
what's happening. You can see elapse
time goes to zero and then when I turn
the hand switch off, it starts counting
up. It gets above 1500 and then it stays
on. So, what you would actually want to
do in this case is rather than looking
for a lapse time uh greater than 1500
seconds, we'd want to look at it elapse
time less than 1500 seconds. This is
what'll mean when the timer starts
counting, which means at the point the
input conditions have fallen false after
half of that countdown, that's when the
light will turn on. So, let's go ahead
and trigger hand switch two again. And
here you can see both lights turn on at
the same time. And they're going to stay
on because elapse time stays zero the
whole time the input conditions are
true. And then when I trigger it off,
you can see the timer starts counting
up. When I hit 15 seconds, the red light
goes away. Or maybe you can't cuz I
moved the box out of the way. But you
get the idea. The interesting thing here
is elapsed time is zero the whole time
that timer is energized because it's not
counting. It's just saying the input
conditions are currently true.
Therefore, my output is currently true
and my elapse time is zero. And then
once the sort of falling edge of the
input conditions are detected, the timer
starts counting up. And that's when
elapse time starts trending towards
preset time. And once elapse time hits
preset time, it stays at or just above
preset time and output stays false. So
to kind of count for a certain period of
time after a condition has become false.
TO is sort of wait for a trigger and
then count up a period of time. All
right, so let's move on from the TOF and
look at what could really be considered
kind of the third type of timer. The
only other kind of timer really, which
is a retentive timer. All right, let's
go ahead and this in Seammens is a TR.
In Alen Bradley, it's RTO or retentive
timer. So, uh, we'll call it toner
example. And there's a new bit on this
one. There's reset. And that's because
retentive timer is counting up while
conditions are true. Um, but it's not
going to forget that value. So if I have
hand switch one true, this to elapse
time will start counting up. But if I
turn hand switch one back off before it
reaches the preset time, elapse time
goes back to zero. It resets by default
every time input is no longer true. The
same thing but in reverse for this tof.
If I turn hand switch two off, this
timer is going to start counting up and
elapse time is going to start counting
up. But if I turn hand switch two back
on, it's going to forget how long its
elapse time was and just go back to
zero. Sometimes like if you might want
over the span of a day you want to
trigger some sort of clean in place or
reset cycle every time uh something has
happened a valve has been opened for a
full hour or something like that you
might want to retain time over time. So
you might want to use a trigger that's
like let's say um both hand switches
being on no let's say both hand switches
being off are going to be what's
actually going to reset the timer. Um,
but both hand switches being on will be
what triggers the timer and then the
timer being done. Okay, so let me write
this and then re-explain it a little bit
better. It's hard to write code and
explain it super well um at the same
time. But let's look at how a retentive
timer works by using when both hand
switches are true, this will count up.
Let's give it a higher uh target value
like 10,000 milliseconds or 10 seconds.
And then we're going to do nothing with
elapse time. We're just going to let it
count up. And we're going to have this
be what triggers output light number
four. Okay. Uh and maybe yeah, we're
going to have some of the other lights
uh triggering during this time because
I'm not going to delete those timers.
But let's talk through what this is
saying again real quick. This is saying
when hand switch one and hand switch two
are both active, count up. And we'll see
elapse time counting up while both of
them are active. But if I turn either of
those two hand switches off, this is
going to remember, it's going to retain
the elapse time so far, and it's going
to store that in elapse time until
someone comes along and resets it. And
the reset conditions I have are when
both hand switches are off and this
timer has already finished. So,
basically, no matter what, this timer is
going to hold on to how long both hand
switches have been active until it hits
10 seconds, and then I can turn both
hand switches off to get it to reset.
Let's go ahead and download this and see
it in practice. Okay, so output light
four is this one that my index finger's
on right here. I'm just going to keep
the index finger on so you guys don't
lose it amidst the other timers we
already made. And I turn one hand switch
on, the timer is not counting up yet. I
turn the second hand switch on. It
starts counting up. It gets to three
seconds. And I turn one of the hand
switches off. And you can see just above
the timer block, it's holding on to 3
seconds and 441 milliseconds. Now I'm
going to turn uh both of them back on.
And I'm going to turn the other hand
switch off this time. And you can see I
got to 6.7 seconds, but it remembers how
long it was active. Now I turn them both
on. And I'm going to let it get up to 10
seconds. And now you can see it's done.
And output light four turned on. And I'm
going to turn one hand switch off. That
didn't reset it. I'm going to turn the
other hand switch off. That didn't reset
it because my conditions to reset are
both hand switches being off and the
timer done. So now if I turn both hand
switches off, you see output light 4
turns off. Okay. So, a retentive timer
is sort of uh like I was describing,
useful for the total amount of time
something was active being true. If you
have a heating jacket or a cooling
jacket and the total heating time is the
thing that matters, but it could start
and stop and start and stop numerous
times during a batch, but you need to
make sure that something is mixing for a
minimum of 1 hour or something like
that. A retentive timer can be a really
useful tool. Now, I'm going to go ahead
and delete all three of these networks
and showcase one more thing that is
closely related to these. So, I think
it's super useful to cover here. Um, and
I'm going to delete all these networks
and we're going to do a counter. Okay,
so a counter is not necessarily uh a
timer because you're counting up just
total conditions and the number of times
a condition has been met, but it's not
time bound. So, for example, let's go
ahead and say I want to count by using
the positive rising edge of hand switch
one. I want to count the number of times
I've cycled hand switch one. And I don't
care how long it was on or off in
between cycles, just total number of
times that hand switch one has been
turned on. I need to make a memory bit
so that I can remember the last state of
hand switch one. We covered this in the
last video. If you haven't seen a rising
edge like oneshot trigger before, uh not
a bad idea to go check out the boolean
logic video. But basically, if I want to
say how do I keep track of the total
number of times that hand switch one has
turned on, I want this instruction
called CTU, count up. Other types of
counters are count down and then a CT,
which is count up and down. Um, but
we're just going to cover count up
because similar to TOF and RTO or TOR R,
they're all iterations on a theme. So,
if we cover count up, you should have a
pretty good idea what you're doing here.
Um, and so we'll say let's make a
counter example data block. And you're
going to see count up, which is actually
the uh trigger to count something. And
then we see the reset bit, just like the
retentive timer. We see PV, which is the
preset value. And in this case, uh let's
say we want this counter to be done when
we get to 10 uh cycles. Okay, so 10
times oscillating that hand switch on.
Uh and then when this is finished, let's
just go ahead and turn all of the lights
on. Okay, so uh let's just use all the
lights for a cool light show. And we'll
say output light two, three, and four
are all going to turn on when this
counter finishes. All right. And now
let's add to the reset condition uh what
we want to have turn it off. And we'll
say how about the rising edge of hand
switch two is what's going to uh reset
this. Um and let's go ahead and make a
hand switch to memory bit. Uh just like
we did for hand switch one.
Boolean global memory. Global memory is
easier so I don't have to stick it in a
database.
And let's go ahead and say that I only
want the rising edge of hand switch two
to reset my counter if the counter has
also finished. Okay, so um this one
totally optional. You don't uh you don't
have to add this bit for this to work.
It would work just by looking at hand
switch 2, but I'm going to say I don't
want to reset this counter unless it's
finished. No matter what, I want to keep
track of how many times this has
occurred. Uh so let's go ahead and
download this and view it in practice.
Okay, so let's get our box up here.
Let's look at hand switch one right
here. And uh let's start cycling it. All
right, I turn it on. There's the
positive edge. And you can see the value
of my counter turned to one. I turn it
back off. Doing hand switch two does
nothing because the counter hasn't
finished. But now if I get to two,
three, four, five, six, seven, eight,
nine. Okay, the next high transition of
this hand switch is going to complete
the counter because you can see it got
to its target value. Now it's at 10 and
all four of the lights turned on. What's
cool about the count up block is it's
actually going to keep counting even
though it's already finished. So, you
can use counters to just track total
number of times something has happened
and not worry too much about that PV
value where you're like, I don't
actually have a target finish in mind.
I'm just generally curious how often
this valve is cycling or things like
that. You can use a counter just to
track number of strokes. Okay. But now
let's say even with hand switch one uh
in the true position which is obviously
the the signal to count up. If I make
hand switch two now, oh, it's not going
to reset it because I used uh I used the
wrong bit. Rather than using done, I
used count up, which is a trigger uh
that is not going to be helpful. So,
we're going to use QU, which means
output count up has finished. Uh, I used
count up, which is only true for one
cycle when the rising edge of hand
switch one is uh true. PLC's are fast
enough that there's no way I was going
to get that to work. Um, so this is the
output bit saying count up is uh true.
And you can see it's already highlighted
green because we're well above the set
point of 10. So, I'm going to go ahead
and trigger hand switch to on again.
Where's the box? There it is.
And you can see all my lights turn off.
Okay, so count up is very similar to a
timer. Um, but it's not time bound. It's
sort of conditional bound, but it's
super useful to include timers in
counters. Okay, so I know these are
action-packed tutorials. It's impossible
to cover any of these instructions in
every possible use case, but I think
we're building a really excellent skill
set and fundamental understanding of
ladder as we go through these. I hope
you found this useful. Drop uh in the
comments any questions that you might
have and what you'd like to see next on
the channel. I hope you've been finding
them useful. If you really love the
channel, consider leaving a like on the
video, subscribing to the channel. My
Patreon is down below. And as always,
thank you for watching and see you next
time.