
Lesson 4
PLC Counters Explained: CTU, CTD and CTUD
Counting up and down, the preset and the load, the edge inside every counter, and the sensor that counts one part twice.
From Usta's field manual in Meridian Works, a story-driven PLC course: chapter 4, “Counting Events”. A timer asks how long. A counter asks how many.
How many, not how long
The packaging line taught you to ask when. The warehouse asks how many.
A timer is a stopwatch. You start it, time runs, and at some number of milliseconds it says "done". Take its input away and most timers forget everything. A counter is a tally chalked on a wall. It does not care how long anything took. It sees that something happened, and it adds one.
That difference is what the warehouse needs. Twelve jars make a box. Twenty-four cases make a pallet. Five trucks fill an aisle. None of these is a question about time. You cannot ask a timer how many jars went past.
Three things set a counter apart from everything you have built so far.
It holds a number, not a bit. A coil only remembers on or off. A counter remembers seventeen.
It remembers on its own. It needs no seal-in for this. A counter's value does not depend on power flow. Remove the sensor, delete the rung. The number stays where it is until somebody resets it.
Resetting it is your job. A TON that loses its input resets itself. A counter never does. Every counter you put in a program brings a question: who resets this one? If you have no answer, the rung is not finished.
The parts of a counter
A counter has four parts. The two platforms give them slightly different names.
| Part | Siemens | Rockwell |
|---|---|---|
| Count input | CU (up), CD (down) | the rung itself |
| Preset | PV | .PRE |
| Current value | CV | .ACC |
| Done bit | QU (up), QD (down) | .DN |
Count input. The rung that feeds the box. The power arriving here is what gets counted. On a CTU the number goes up, on a CTD it goes down.
Preset. The number you count to: twelve jars, twenty-four cases. It is written into the box as a fixed number. If the operator must be able to change it, it comes from a tag.
Current value. The tally itself.
Done bit. It goes TRUE when the count reaches its target. On a CTU the condition is the same on both platforms: the current value is equal to or greater than the preset. On a CTD the platforms part. You will see this on the page "Counting down, and the load".
In Siemens the counter's memory is an instance data block (of type IEC_COUNTER). In Rockwell it is a tag of type COUNTER. On both platforms every counter needs its own name.
In this editor the box sits in the middle of the rung. The power arriving on its left is the count input. The power leaving on its right is the done bit. Put a coil straight after the box and the done bit drives it.
You can read the inside of a counter by name. Siemens spelling: C_Jars.CV, C_Jars.PV and C_Jars.QU. Rockwell spelling: C_Jars.ACC, C_Jars.PRE and C_Jars.DN. This editor understands both. Keep the current value on screen while you test. A machine that acts at the wrong moment nearly always has a number in it that you did not expect.
The edge inside every counter
You met edges in the power room. The button is pressed and the button has just been pressed are different things. One is a level, the other is an event. Keep this in mind, because a counter counts events.
A counter counts transitions. Every time its count input goes from FALSE to TRUE, the number changes by one. However long the signal stays TRUE, nothing else happens.
This is not a small detail. It is what makes a counter usable in a machine that scans. The program runs a hundred times a second. If a counter counted levels, a jar standing in front of the photo-eye for half a second would add fifty jars to the box.
Look at the wide pulse. The belt has jammed and a jar has stopped in the beam. The beam stayed blocked for four hundred milliseconds. The tally went up only once, on the edge where the jar arrived. It will not go up again until the beam clears and a new jar blocks it.
So you do not need an edge instruction in front of a counter. The edge detection is inside the instruction. That is true on both platforms. In Siemens the edge memory lives in the counter's instance data block. In Rockwell it lives in the .CU and .CD bits of the COUNTER tag. An apprentice who puts a P contact or an ONS in front of a counter does not make the rung safer. He wastes an edge bit. If he uses that bit on another rung as well, he brings back the fault you learned in chapter 2: pulses that come and go.
There is one fair exception, and it comes from the sensor. We will reach it at the end of the chapter.
Counting up, and past the preset
A CTU counts up from zero. It has the same name on both platforms.
Picture a box of twelve jars, preset 12. From the first jar to the eleventh, only the current value rises. The twelfth jar takes the count to twelve. The done bit goes TRUE. Whatever follows the box on that rung comes to life.
Now the mistake apprentices make on their first counter. The done bit does not stop the counting. The thirteenth jar makes the count thirteen, the twentieth makes it twenty. The bit stays TRUE all that time, because thirteen is still more than twelve. The number does have an upper limit, but that limit is not the preset. In this simulator the count stops at 32767. In Rockwell .ACC is a DINT and its limit is much higher.
This behaviour is both useful and a trap. It is useful because an alarm set at a high mark stays on while things get worse. It is a trap because the reset can be late. Say the reset arrives after two more jars have gone past. Those two jars are counted into a box that is already closed and on its way.
The reset. Here the two platforms part.
- In Siemens the CTU box has an R input. While R is TRUE, the count is held at zero.
- In Rockwell the box has no reset pin. The reset is a separate rung: a condition and a RES instruction naming the same COUNTER tag.
In this editor the CTU box has a reset (Reset) field. You can use it in both views.
Learn two properties by heart.
The reset is a level, not an edge. Hold R TRUE for half a second and the counter stays at zero for half a second. It counts nothing that passes in that time. The same is true of RES. The whole difficulty of the first work order in this zone lies in this rule.
The reset must win. In Siemens, if a reset and a count arrive in the same scan, the reset wins. Nothing is counted. In Rockwell RES is a separate rung, so rung order decides who wins. The Set and Reset rule from chapter 2 applies here too: put the RES rung below the counter.
Counting down, and the load
A CTD counts down. But it is not a CTU with a minus sign in front. On top of that, the two platforms define it differently. First learn the rule this simulator uses. It is the rule of the IEC standard and of Siemens.
The IEC rule (Siemens and this simulator). You start a CTD at a stock figure. Every event takes one off. The done bit (QD) goes TRUE when the count reaches zero. So the bit means finished, empty, the shelf is bare. That is exactly the number a storeman carries in his head.
Two things about this rule must be said out loud.
A CTD does not start full. In a processor that has just been switched on, every counter is at zero. A CTD reads zero as "done". You switch on, and the reorder lamp lights at once. That is not a fault. The PLC has never seen your shelf. Until somebody tells it, the honest answer is zero.
The way to tell it is the load. On a Siemens CTD box this input is called LD. LD does not clear the count. It loads the preset into the count. In this editor the CTD box's reset field does the same job: it writes the preset into the count. The field that clears a CTU fills a CTD. Same field, opposite meaning. The only difference is which instruction it is on. Wire the same key into both instructions of an up/down pair and you will find this out one evening, with the line stopped.
The Rockwell rule. The CTD in Studio 5000 works differently. Every event takes one off .ACC. The .DN bit is TRUE for as long as the count is equal to or greater than the preset. So it goes FALSE when the count drops below the preset, not when it reaches zero. There is no input that loads a value into the count. RES always clears it. If you want to write a stock figure, you write it into the .ACC member with a MOV. Do not mix these two rules up. When you see a CTD in a Rockwell program, check again what its .DN bit is asking.
There is no floor. Take twenty-five cases out of a shelf of twenty-four and the count is minus one. The instruction does not argue. In this simulator the count goes down as far as −32768. If negative stock makes no sense in your plant (it usually does not), say so in the rung. Put the empty bit's NC contact in front of the count input. The counter stops itself at zero.
Two instructions, one counter
This zone is really built on this pattern.
An aisle has a photo-eye at each end. One sees trucks going in, the other sees trucks coming out. What you want is the difference: how many trucks are in there right now?
Do not use two separate counters. Subtracting one number from the other needs arithmetic you have not learned yet. A second number is a second thing that has to be kept in step. Use one counter tag and two instructions instead:
The entry eye feeds a CTU. The exit eye feeds a CTD. Both boxes name the same counter. So both work on the same current value. A truck going in raises the number, a truck coming out lowers it. The number in the middle gives the aisle's occupancy without anybody doing any arithmetic. This is exactly how occupancy is counted in Rockwell: a CTU and a CTD share one COUNTER tag.
The detail that makes this safe is: the value belongs to the counter, the input edge belongs to the instruction. Each instruction keeps its own record of what its rung did in the previous scan. In Rockwell the CTU's is in the .CU bit and the CTD's in the .CD bit. If two trucks break both beams in the same scan, the number goes up one and down one. No count is lost and the aisle total stays right.
In Siemens one box does this job: CTUD. The count-up input is CU and the count-down input is CD. QU goes TRUE at the preset, QD at zero. The occupancy is declared once instead of twice. Two edges in the same scan still cancel. Studio 5000 has no CTUD. That is why you learned the pair first. This editor's Siemens view also has the CTUD box.
Two warnings.
Each direction has its own done bit. In Siemens, C_Aisle.QU is the up-counter's answer: the aisle is full. C_Aisle.QD is the down-counter's answer: the aisle is empty. One bit cannot answer both questions. A counter at zero is both "not yet at five" and "all the way down".
A Rockwell COUNTER tag has a single .DN. There is no separate down-done bit. In a real Rockwell program the question "is the aisle empty?" is usually asked with an EQU that compares .ACC with zero. You will learn the compare instructions later. In this editor, the power leaving each box is that instruction's own answer. Take each lamp from its own instruction's rung.
Rung order still matters. If the exit rung is below the entry rung, a truck leaving shows up on the entry rung's lamp one scan later. That is ten milliseconds. Nobody in a warehouse will notice. Know it anyway.
Count, act, reset — and the sensor that lies
A count that reaches its preset nearly always makes something happen: close the box, push the diverter, turn the table or tell somebody. Then a new box starts. The count must be back at zero before the next product arrives.
Count, act, reset, and start again. There are three steps. The trap lies between the second and the third.
The first idea is to reset the counter with the thing it triggered. You wire the diverter's own output into the reset. It even looks tidy on the screen. And it works until two products come close together. Because the diverter is not an event, it is half a second of solenoid output. The reset is a level and it wins. For that half second the counter stays at zero. The next jar passes the beam and goes into the box, but it is not counted.
So reset the counter with a signal that lasts one scan. Taking the done bit into a bit gives you exactly that. In the scan the box fills, the bit goes TRUE. In the next scan the counter is reset and the bit drops with it. Give the diverter its own pulse and let it run out by itself. In Siemens that is a TP. Rockwell has no TP. There the pulse is built from a set/reset bit and a TON (chapter 3). The count is back at zero long before the next product arrives.
Where the reset lives depends on the platform. That also changes what you look for. In Siemens the reset is an input on the box: R on a CTU, and on a CTD an LD that loads the preset. In Rockwell the box has no such input. Resetting a counter or a timer is a separate rung that ends in a RES naming it. RES always clears.
Two things follow. First: RES is a level too. Everything said above about resetting with a level applies to RES as well. Second: because RES is a separate rung, it can be anywhere in the program. It can even be three hundred rungs away from the box it resets. That is why in Rockwell the answer to what resets this one? is in the cross reference. In Siemens a look at the box is enough.
Now the sensor. The sensor is the other half of every counting job. A photo-eye is a piece of glass in a dusty building. A label edge, a clear bottle or a shiny surface can break and re-make the beam twice for one product. Your counter is not wrong. It counted two events, because two events happened in the plant.
There are real cures for this. Mount the eye where the product is solid. Use a sensor with a reflector or with background suppression. If the flicker cannot be fixed at the sensor, filter it in the program. Put in a short TON. Let a count through only if the beam stays blocked without a break for a while. What you never do is divide the total by two and call the job done.
Count what the plant did, not what you hoped it did. When the number is wrong, it is telling you something about the plant.