Counters (output instruction)
CTUD: Count Up and Down
Counts up on its own rung and down on a second bit, for a running total that can go both ways.
Also called: up/down counter, bidirectional counter.
CTUD(counter, preset, downTag)Arguments
| Name | What it is | Accepts | Default |
|---|---|---|---|
| destination | What the instruction acts on | a counter | required |
| preset | Preset | a whole number | 4 |
| downTag | Count down on | a tag | none |
Arguments are positional and this table is generated from the simulator's own instruction registry, so it always matches what the engine accepts.
What it does
CTUD keeps a total that can rise and fall. It is the instruction for anything that is a level rather than a tally: parts sitting in a buffer, cars in a car park, bottles between the infeed and the outfeed sensor. A CTU alone can only ever go up, so it can count how many arrived but never how many are there now.
It counts up when its own rung goes from false to true, and down on the rising edge of the bit named as its down input. Both are edges, which is the whole point: a sensor that stays blocked for two seconds counts one part, not one part per scan.
The down input is a bit rather than a second rung because a rung here drives one output per branch row. If the down count needs conditions of its own, give it a rung of its own that drives a bit, and hand that bit to CTUD. The example does exactly that, and it is the normal shape.
Read the result with .ACC for the running total and .DN for whether it has reached the preset. RES sets the accumulator back to zero.
A rung you can run
How many parts are in the buffer right now
ONS(Infeed_Sensor)OTE(Part_In)
ONS(Outfeed_Sensor)OTE(Part_Out)
XIC(Part_In)CTUD(Buffer_Count,25,Part_Out)
XIC(Buffer_Count.DN)OTE(Buffer_Full)
LES(Buffer_Count.ACC,1)OTE(Buffer_Empty)Each sensor gets a rung and a one-shot, so one part makes one pulse however long it sits in front of the eye. The counter adds a part when one arrives and takes one away when one leaves, and .ACC is the number in the buffer at this instant. Toggle Infeed_Sensor a few times, then Outfeed_Sensor, and watch the total climb and fall.
Run this rung in the simulatorWhat it is called on other controllers
| Platform | Equivalent |
|---|---|
| Rockwell (Studio 5000) | No single instruction: a CTU and a CTD sharing one counter tag |
| Siemens (TIA Portal) | CTUD |
| Mitsubishi (GX Works) | No single up/down contact: a counter driven by separate up and down instructions |
| CODESYS and IEC 61131-3 | CTUD |
Common mistakes
- Expecting the down input to be a rung with contacts on it. It is one bit. Drive that bit from its own rung, with its own one-shot, and the counter stays correct.
- Assuming the total cannot go negative. Nothing stops it. If the outfeed sensor sees a part the infeed sensor missed, an occupancy count walks down past zero and stays wrong all shift. Clamp it with a compare, or re-zero it with RES when the machine is known to be empty.
Practise it
Interactive exercises that use CTUD. Each one checks your ladder against a real scan.
