LadderLogicOnline

Learn › Instructions

CTU and CTD: Count Up and Count Down

Counters count events: boxes past a photo eye, cars over a sensor, presses of a button. CTU adds one each time its rung goes true, CTD subtracts one, and the done bit tells you when the count has reached the preset.

The parts of a counter

Both instructions write to a tag of type COUNTER. Like a timer, it's a small structure:

MemberMeaning
PRE (preset)The target count. A new counter starts at 10.
ACC (accumulator)The current count.
CU (count up enable)Follows the rung of the CTU. Used to spot the false-to-true change.
CD (count down enable)Follows the rung of the CTD.
DN (done)On whenever ACC is greater than or equal to PRE.

Read them with dotted names: XIC Boxes.DN, or use Boxes.ACC in a compare instruction.

Counting edges, not time

The most important thing to understand about counters: they count transitions, not scans. A CTU adds 1 only on the scan where its rung goes from false to true. If the rung stays true for a hundred scans, that's still one count. The rung has to go false and then true again to count again.

That's what the CU bit is for. The CTU remembers last scan's rung state in CU, and counts only when the rung is true now and CU was off. CTD does the same with CD. So holding a button down, or a box sitting in front of a photo eye, counts once, which is exactly what you want.

A timeline

A CTU named Boxes with a preset of 3, driven by a photo eye. Each box blocks the eye for about a second.

EventRung (PE)ACCCUDN
Startfalse000
Box 1 arrivestrue110
Box 1 still blockingtrue110
Box 1 leavesfalse100
Box 2 arrivestrue210
Box 3 arrivestrue311
Box 4 arrivestrue411

Notice that the counter keeps counting past the preset. Reaching PRE turns DN on, but it doesn't stop the counter or reset it. To start a new batch, use a RES.

Counting a batch

Count boxes, stop the conveyor when the batch is full, and restart with a button:

 |   PE_Count                                        |
 |----] [------------------------------[CTU  Boxes  PRE 5]--|
 |                                                  |
 |   Run          Boxes.DN                Conveyor   |
 |----] [---------]/[------------------------( )----|
 |                                                  |
 |   NewBatch                                        |
 |----] [------------------------------------(RES Boxes)--|
The conveyor runs until 5 boxes have passed the eye. NewBatch clears the count.

The box counter tutorial builds this program step by step on the simulated conveyor.

Counting up and down on one tag

Put a CTU and a CTD on the same counter tag and you can track how many things are in an area: count up when something enters, count down when it leaves.

 |   PE_In                                           |
 |----] [------------------------------[CTU  OnBelt  PRE 4]--|
 |                                                  |
 |   PE_Out                                          |
 |----] [------------------------------[CTD  OnBelt  PRE 4]--|
 |                                                  |
 |   OnBelt.DN                               Full    |
 |----] [------------------------------------( )----|
OnBelt.ACC is the number of boxes between the two eyes. Full is on while there are 4 or more.

Each instruction has its own enable bit (CU for the CTU, CD for the CTD), so the two never interfere with each other's edge detection. DN is worked out the same way by both: on whenever ACC is at least PRE. If the CTD counts below zero, ACC simply goes negative; this simulator doesn't have the overflow and underflow bits (OV and UN) that some PLCs add.

Try it in the Playground

  1. Open the Playground and add two push buttons: Up and Down.
  2. Build rung 0: XIC Up then CTU C1, and set its preset to 3. Build rung 1: XIC Down then CTD C1.
  3. Build rung 2: XIC C1.DN then OTE Lamp.
  4. Press Run. Hold Up down and notice it counts once, not continuously. Tap it three times to light the lamp, then tap Down to bring the count back under 3.

Common mistakes