Counting boxes on a conveyor with a CTU counter

Counters are the second most used PLC instruction after timers. In this tutorial a conveyor carries boxes past a light barrier. The PLC counts them with an up counter (CTU), stops the belt when a batch of 10 is complete, and lights a lamp until an operator resets the batch. You build and test it in Virtuino Circuits with a Siemens S7-1200.

Open the ready-made circuit in the simulator

How a CTU counter works

  • CU (count up): each rising edge, a change from 0 to 1, adds 1 to the current value CV. Holding the input at 1 does not count again.
  • R (reset): while it is 1, CV is set to 0.
  • PV (preset value): the output Q becomes 1 when CV ≥ PV.

Because CU reacts to edges, you do not need a separate edge instruction for a sensor. Each box that interrupts the beam counts once.

I/O list

AddressDevice
%I0.0S0 Stop (NC)
%I0.1S1 Start (NO)
%I0.2B1 Light barrier (1 = box present)
%I0.3S2 Batch reset (NO)
%Q0.0K1 Conveyor motor contactor
%Q0.1H1 Batch complete lamp
%Q0.2H2 Half batch lamp

Wiring

  1. Place the PLC (S7-1200 CPU 1214C), supply it from L1/N, and feed the inputs from 24 V DC.
  2. Wire S0, S1, S2 to inputs .0, .1, .3. From Sensors place a reflective light barrier B1 (3-wire): BN to L+, BU to M, and its output BK to input .2.
  3. Outputs: .0 → coil K1, which drives the conveyor motor (draw a small power circuit with a motor if you like); .1 → lamp H1; .2 → lamp H2.

Ladder program

 Rung 1 – Conveyor runs until the batch is complete
      %I0.1         %I0.0     C1                        %Q0.0
 |-----| |---+------| |-------|/|------------------------( )---|
      %Q0.0  |
 |-----| |---+

 Rung 2 – Count boxes
      %I0.2                       C1: CTU   PV = 10
 |-----| |-------------------------[CU           Q]-----------|
      %I0.3                        |
 |-----| |-------------------------[R            ]

 Rung 3 – Batch complete
       C1                                               %Q0.1
 |-----| |-----------------------------------------------( )---|

 Rung 4 – Half batch (comparison)
      [ C1 >= 5 ]     C1                                %Q0.2
 |------[       ]-----|/|--------------------------------( )---|
  1. Rung 1 is a start/stop seal-in, with a NC contact C1: when the counter output is 1, the conveyor stops.
  2. Rung 2. From the counters group place a CTU, name it C1 and set PV = 10. Wire the light barrier contact to CU and the reset button contact to R.
  3. Rung 3. A contact C1 lights the "batch complete" lamp.
  4. Rung 4. A comparison contact C1 >= 5 lights the "half batch" lamp. For a counter, the value compared is its current value CV.

Simulate

  1. Press F5 and click S1: the conveyor starts.
  2. Click the light barrier B1 once (a box enters the beam) and once more (the box has passed). Each box is one rising edge, and the counter box in the Ladder window shows CV going up.
  3. At 5, H2 lights. At 10, C1 becomes 1: the conveyor stops and H1 lights.
  4. Press S1 again: nothing happens, because the batch is complete. Press S2: CV returns to 0, H1 goes off, and S1 can start the next batch.
If the counter counts too many boxes on a real machine, the sensor is probably bouncing or the box has holes. Real programs then filter the sensor with a short TON (for example 50 ms) before the counter.

Variations

  • Use CTD (count down) loaded with the batch size to show "boxes remaining".
  • Use CTUD with a second light barrier, to count boxes entering and leaving a buffer zone.
  • Combine it with the tank level tutorial to fill each box automatically.

Open this circuit in the simulator