Timers in PLC Programming
Some of the most essential functions in PLC programming are the timers. Time in PLC programs is used almost everywhere from delaying the start of a motor to prolonging a signal. PLC timers are used exactly for that. Although they at first can seem confusing it doesn’t take a lot to understand how they work and how to use them.
Depending on which platform you are using you will find quite a lot of different types of timer functions. Many of them are platform specific which means that you can only use them on a certain platform. Siemens for example has many of their own PLC timer functions. These are unique for the Siemens PLC platform.
Timers can be used not only in ladder logic but also as functions blocks in function block diagram or functions in structured text. They can even be used to check how long an actuator has been running and then for alarms in a SCADA system.
PLC Timers from the standard
Because this can be very confusing with all these different timer function, I will give you an introduction to the 3 standard timer functions.
The reason for this is that they work the same way no matter which platform you are using.
You will find them defined in the official standard for PLC programming languages – IEC61131-3 by PLCOpen. Since this is the standard for PLC programming languages most platforms have these three types of timer functions available
In this tutorial, I will not only show you how those standard timer functions work but also stimulate them in the open-source CodeSys environment. That way, you can see how you can use the PLC timer functions in your own PLC programs.
On Delay Timer (TON)
First one of the standard timers is the on delay timer also known as just TON. This is by far the most used timer in PLC programming. You will find this in any platform and it is in fact so useful that you can build the other timer functions with the on delay timer.
The functionality of the on delay timer (TON) can be described like this:
Output is turned ON after a delay
For that reason the timer also has its name.
Below here you can see the timing diagram of the on delay timer. You can see that the output is turned on after a delay.
This delay is called the preset time (PT). The delay said in another way, is how long you want the timer to be turned on. When you turn on the input (IN) the timer will start timing (turning on the timer). Elapsed time (ET) is the current time of the timer. Here you can always see how long the timer has been turned on.
As soon as you activate the timer by turning on the input the timer will start counting. After a certain delay the output will be turned on.
Off Delay Timer (TOF)
The second standard PLC timer is the off delay timer or just TOF. My best way to remember how it works is again by its name.
It is called an off delay timer because it works like this:
Output is turned OFF after a delay
One of the biggest differences between this and the on delay timer is how you activate it.
As soon as you turn on the input of this timer, the output is also turned on. This is because in order for the output to be turned OFF (after a delay) it needs to be turned on in the beginning.
Check the timer diagram below to see that.
The timer will not be activated before you turn the input off again. When you do that the timer will start counting and after the delay, the output will be turned off.
Pulse Timer (TP)
The final one of the 3 standard timers is called the Pulse Timer or PT.
Although this timer is not so commonly used it is still a very useful timer function.
This timer is a little different than the two others, since this one is used to generate pulses. Yes, that is also how we can describe its functionality:
Generates a pulse of a specific length
You can activate the timer by turning on the input. When that happens the timer will start counting time. As a parameter for the pulse timer the time for the pulse is defined.
After the output has been on for that amount of time it will be turned off again.
Again, you can see above in the timing diagram how it works
The difference from the two other timers here is that this pulse will happen no matter what the state of the input will be in the mean time. You will always get a pulse of that certain length after the timer has been activated.
If you look closer at the timing diagram you can also see that Q always has the same length
I find the pulse timer to be very useful exactly because is doesn’t depend on the input. All it needs is a short or long or just any signal at the input to give you that pulse.
Timer Accuracy and Timer Errors
Due to the scan cycle and the internal workings of a PLC the timers are not always accurate.
This is important to know about if you are a professional PLC programmer, because in the end it can affect how your programs will work. If you are timing with seconds, minutes or more this has no effect and you should not be worried.
But if you are dealing with milliseconds in your timers, the accuracy of the timers can suddenly have a huge impact and your timing can be wrong.
When the timer does not count the time we expected it is called a timer error. And basically there are two types of timer errors – software and hardware errors.
Both of these can generate errors and inaccuracy both on the input side and on the output side
You might wonder how the software in a PLC can generate timer errors. But here I’m not talking about the software you are programming with ladder logic or structured text. I’m talking about the internal software also called firmware.
The firmware is what makes the PLC work. It is this piece of software that does the whole scan cycle of the PLC – puts the state of the inputs into memory registers, runs your program and sets the outputs via the output memory registers.
Because of exactly that scan cycle the software can lead to some timer inaccuracy and errors.
Software errors on the input side happens when the input is detected too late.
The scan cycle takes some time (often around 20 ms) and it is only in the first step of the scan cycle that the inputs are detected. If you turn on the input right after the PLC has scanned the inputs it can take a whole scan cycle before the input is detected as on
This might not seem like a lot, but if you’re dealing with large PLC programs or you have to time very short amounts of time (milliseconds) this is a significant error.
Output errors also happens because of the scan cycle. If the output is set right after the scan cycle where the PLC writes the output register, it can take a whole scan cycle before the output is actually set in the register.
Total Software Error
When you sum up the input and output errors you will get what is called the total software error. This error can be up to two scan cycles long which is quite a lot of time if you’re working in ms.
The accuracy of a PLC timer can also depend on the PLC hardware. You will often see even experienced PLC programmers overlook this source of errors.
It is so important for PLC programmers not only to know about PLC programming, but also to know about electronics and hardware. Because of how the hardware works it can generate errors both at the inputs and at the outputs of a PLC.
Most modern PLC’s has a build-in filter on the input side to avoid noise and bouncing. This filter is actually a small delay, that makes sure the short spikes will not get detected at real inputs.
When you press a button and turn on e.g. 24VDC at a PLC input what will happen is a lot of bouncing, just as the button or sensor closes and electrical connection is made.
The signal will jump up and down and without the filter the PLC might detect that as not just one press but multiple presses on the button. If you connect an oscilloscope at the input you can see that bouncing.
Filtering out the bouncing and noise will also give a delay from the timer input is turned on to when the PLC actually detects that it is turned on.
On the output side you can also have errors. They happen because of the output electronics. If you have a relay output it can actually take up to 10 milliseconds for the relay to close.
Transistors (such as NPN transistors) are a little better but could still take up to 1 ms to fully close.
All these hardware and software errors might not seem significant, but if you sum them up you can easy get a total error or inaccuracy of 100 milliseconds.
That is a tenth of a second.
This is why it is so important to know about these timer errors and the timer accuracy. If you are working with small amounts of time that requires precision you should take your measures.
You can for example do it with periodic task execution that doesn’t depend on the scan cycle and use transistor outputs.