Counters in PLC Programming
A PLC counter is a function block that counts up or down until it reaches a limit. When the limit is reached the output is set.
Counting plays a crucial role in PLC programming. It’s all about tracking numbers—like how many times a process completes or the quantity of products produced. Think of it as a digital tally counter that helps manage industrial tasks efficiently.
PLC counters are also used to assist logging to SCADA systems by counting the amount of times these events has happened or setting alarms when an event has happened a certain amount of times.
With all that said timers are very useful and it is crucial for every PLC programmer to know the basics of counting in a PLC program. In this article I will explain how counters work, and how you can use them in your PLC programs.
You can either watch my video tutorial about PLC counters or read it here just below the video:
Content:
Basics of Counting in a PLC
Before you start counting in any PLC program there are some basics you should know first. These are basic information about the counter instructions and the PLC itself.
Understanding a PLC’s counting limits is vital. It’s like knowing the maximum number your digital counter can reach before it resets or overflows.
Counter Limits
Counters use variables of certain data types to store numbers in the PLC. All counters need to store at least two numbers:
- Counter Limit
- Current Counter Value
Since these two numbers are saved in a certain data type they also have their limits. Many PLC’s save these two numbers as WORD or Integer data types and if you remember the basics of PLC data types, you will know that a WORD takes up 16 bits.
A signed integer also takes up 16 bits, but the first one is used for signing, so you will only have 15 bits for the actual number
With a little bit of calculation we’ll quickly find out that the maximum value of a WORD is 16.535. The maximum value of the signed integer is 32.767.
Although it is rare that you will need to count to such high numbers, it is still important know to avoid overflow errors.
High-Speed Counters
When dealing with very fast pulses, like from encoders, standard counters might not keep up. Here, you’ll need a high-speed counter module designed to handle these rapid signals.
These input modules are better known as high-speed counters and they are built to capture inputs of various high frequencies.
Counter Function Blocks
All counter operations or counter function blocks has some inputs and some outputs. In fact, I talked about two of them before (counter limit and current counter value).
What is important to know here is which data type the inputs and outputs take. Normally for counters this is boolean and WORD data types, but it can depend on the PLC platform you’re using. So, don’t forget to check the documentation for the counter on the PLC platform you’re using.
Up Counter (CTU)
The first counter instruction I will introduce you to is the up counter, also known as just CTU. As the name implies, this PLC counter is used for counting up.
You can see the up counter function block illustrated below:
An Up Counter sets an output after counting to a specified number. It’s straightforward: each incoming pulse adds one to the count until the set limit is reached.
How it works
Each pulse on the count input (CU) will increase the current counter value (CV) by 1. When CV is greater than or equal to the counter limit (PV) the output (Q) is set. A pulse on the reset input (R) will reset the value of CV to 0.
Example:
Up counters are usually used to keep track of how many times an event has happened. Let’s say you want a process to complete 10 times before cleaning needs to happen.
For this you have to set the counter limit (PV) to 10. Each time the process has completed you will give a pulse on the count input (CU). When the process has completed 10 times, the output (Q) will be set. Now you can use that output to for example set an alarm that the system needs cleaning.
When cleaning is done you can give a pulse on the reset input (R) and you can now start over again.
Down Counter (CTD)
Counting down is another operation that is widely used in PLC programming. In some cases you want to know how many counts are remaining before the limit is reached. With the up counter you can use some math to do it. But you can do it easily with a down counter.
The Down Counter works by counting backwards from a set number down to zero, offering a clear countdown for tracking remaining processes.
As you can see the down counter has a pin called LD instead of the reset. It is called load and is used for loading a value into the current counter value. Because when you count down to 0 you will need some initial value of the counter.
How it works
Each pulse on the count input (CD) will decrement current counter value (CV) by 1. When CV is less than or equal to 0 the output (Q) is set. A pulse on the load input (LD) will assign the value of counter limit (PV) to CV.
Example:
Imagine you have a semi-automatic process where the operator needs to do a manual task to start a process. The process need to be completed 10 times but when there is 2 times left the operator needs to inspect. It is important that the operator can see how many processes are left, since the total amount of processes can vary.
This is where the down counter comes in handy. What you need to do here is to assign the total amount of process times to the counter limit (PV) and then give at pulse at the load input (LD). Now you have to total amount as the current counter value (CV).
Every time the process has completed you give a pulse on the count input (CD). You can now use CV to show the operator how many processes are left. When he can see that there are 2 left, he needs to inspect. Additionally you can even add an alarm by comparing CV to 2 with an equality operator.
Up Down Counter (CTUD)
At last you have the up down counter which can count both ways. Sometimes the combination of the up and the down counter can be useful. You can count the same number both up and down and set both an upper and a lower limit.
As you can see below it has a bit more pins than the two other counters:
The whole idea about this counter is that it can count both up and down the same current counter value (CV). At the top of the function block you can see that there are 2 count inputs. One for counting up and one for counting down.
How it works
Each pulse on the count input (CU) will increase the current counter value (CV) by 1. And each pulse on the count input (CD) will decrement current counter value (CV) by 1.
When CV is greater than or equal to the counter limit (PV) the output (QU) is set. When CV is less than or equal to 0 the output (QD) is set.
A pulse on the reset input (R) will reset the value of CV to 0. A pulse on the load input (LD) will assign the value of counter limit (PV) to CV.
Example:
One way of using the up down counter is to count finished products. Say you want to produce a certain amount of products in a batch control system. But after every product is finished a manual inspection is needed. If a product is discarded you will have to subtract it from the number of finished products.
For this you can use the up down counter. Every time a product is finished you can give a pulse at the count up input (CU). But if at inspection the product fails the quality test you can give a pulse at the count down input (CD) to decrement the amount of products produced.