Module 09 · Ladder logic basics

Counters & compare

Count parts into a batch, track stock, fill a car park. Counters count edges, compare instructions turn numbers back into on/off decisions. Together with the last four modules they complete the ladder toolkit for most simple machines.

≈ 45 min6 lessons1 hands-on labQuiz · pass 80 %Free

By the end of this module you can

  • Use count-up (CTU) and count-down (CTD) counters with preset, reset and load
  • Read a counter's current value and done bit
  • Use compare instructions (=, <>, <, >, <=, >=) in rungs
  • Count up and down with one value
  • Set counters up in Siemens, Rockwell, Mitsubishi and Logivyx, including Rockwell's different CTD behaviour

A CTU adds 1 to its current value CV on each rising edge of its count input (it detects the edge itself, so no one-shot is needed). When CV reaches the preset PV, output Q turns on. The reset input R sets CV back to 0.

Note step 3: a CTU keeps counting past the preset. If extra parts must be rejected or the conveyor stopped, the program has to do that using Q.

A CTD subtracts 1 on each rising edge of its count input. The load input LD copies the preset PV into CV. In IEC, Q turns on when CV reaches 0 or below. Here: a magazine holds 6 caps; each cap used counts down; at 0 the "refill" lamp lights.

A compare element tests two values and passes power if the test is true. One side is usually a tag, the other a tag or a constant. Use them for limits, step numbers, counts and analog thresholds.

TestIEC / LogivyxSiemens LADRockwellMitsubishi
equal=CMP ==EQULD=
not equal<>CMP <>NEQLD<>
greater>CMP >GRTLD>
greater or equal>=CMP >=GEQLD>=
less<CMP <LESLD<
less or equal<=CMP <=LEQLD<=
in range(a >= lo) AND (a <= hi)IN_RANGELIMtwo compares
TipPrefer >= over = when a value can jump past the target, e.g. a fast count or an analog value. "Level = 80.0 %" may never be exactly true; "Level >= 80.0 %" will be.

A car park counts cars in and out. IEC has a CTUD block for this; many PLCs too. The Logivyx Ladder Designer offers CTU and CTD, so here we use one DINT tag with edge contacts and Raw ST actions, which works on any PLC.

C_Batch(CU := Part_PE, R := Reset_PB, PV := 5);
Batch_Full  := C_Batch.Q;
Batch_Count := C_Batch.CV;
// CTUD: CU, CD, R, LD, PV → QU, QD, CV

IEC: CTU (CU, R, PV → Q, CV), CTD (CD, LD, PV → Q when CV ≤ 0), CTUD. Logivyx Ladder Designer: Counter (CTU/CTD) with Instance name, Type, Preset value, Reset tag (R for CTU, LD for CTD) and ACC Tag, a real tag that receives CV every scan so screens and compares can use it. Compare: Compare element with Value A, Operator, Value B (tag or literal).

        "C_Batch_DB"
           +-------+
"Part_PE"  |  CTU  |
--| |------|CU    Q|------( "Batch_Full" )
"Reset_PB"-|R      |
        5 -|PV   CV|- "Batch_Count"
           +-------+

S7-1200/1500 IEC counters CTU, CTD (CD, LD, PV), CTUD, each with an instance DB; choose the data type (Int, DInt…). Compare with CMP ==, <>, >, >=, <, <= contact boxes, plus IN_RANGE / OUT_RANGE. S7-300/400 also have S5 counters (S_CU, S_CD, S_CUD, max 999).

XIC Part_PE CTU C_Batch 5 0
XIC Reset_PB RES C_Batch
XIC C_Batch.DN OTE Batch_Full
GEQ C_Batch.ACC 3 XIO C_Batch.DN OTE Nearly_Full

Studio 5000 CTU and CTD share a COUNTER tag: .PRE, .ACC, .CU, .CD, .DN, .OV, .UN. Reset with a separate RES rung. Watch out: .DN is set when .ACC ≥ .PRE for both CTU and CTD, unlike the IEC CTD whose Q means "reached zero". A CTD on the same tag as a CTU gives up/down counting. Compares: EQU, NEQ, GRT, GEQ, LES, LEQ, LIM, CMP.

LD   X0
OUT  C0  K5        ; count rising edges of X0, set value 5
LD   X1
RST  C0            ; reset
LD   C0
OUT  Y0            ; batch full
LD>= C0  K3        ; compare contact
ANI  C0
OUT  Y1            ; nearly full

Counter devices C count up on each rising edge of the coil; the C0 contact closes when the count reaches the set value; reset with RST C0. Long counters LC hold 32-bit values. Up/down counting uses UDCNT instructions or the IEC CTUD FB. Compare contacts: LD=, AND>, OR<=… and D prefixes for 32-bit (LDD>=).

Use PLC_Trainer. PB1 = part sensor, PB2 = reset, SW1 = conveyor run switch, Q1 = conveyor, Q2 = nearly full, Q3 = batch full, N1 = count.

Lab

L09_Batch

  1. New ladder routine L09_Batch. Rung 1: XIC PB1 → Counter, Instance name C_Batch, Type CTU, Preset value 6, Reset tag PB2, ACC Tag N1.
  2. Rung 2: Function Block Bit C_Batch.Q → OTE Q3.
  3. Rung 3: Compare N1 >= 4, XIO Q3 → OTE Q2.
  4. Rung 4: XIC SW1, XIO Q3 → OTE Q1. The conveyor stops when the batch is full.
  5. Call L09_Batch from Main, save, F5. Count to 6 and check Q2, then Q3 and the conveyor stop. Reset with PB2.
  6. Extension: change the counter to CTD with Preset 6 and the same Reset tag. What does PB2 do now, and when does Q turn on? Adjust rungs 3 and 4 so the lab still works.
Level 2 completeContacts, coils, latches, edges, timers, counters and compares: with these you can program most simple machines. Level 3 shows the same logic in Structured Text.