Learn › Tutorials
Tutorial: count boxes on a conveyor with a photo eye
Packing lines run in batches: fill a case with five boxes, stop, swap the case, go again. In this tutorial you'll count boxes with a photo eye and a CTU counter, stop the conveyor when the batch is complete, and fix a subtle double-counting bug along the way.
You should know the start/stop seal-in circuit before starting. If you don't, do the seal-in tutorial first. It also helps to have skimmed CTU and CTD.
Tags and parts
Open the Playground in the conveyor scene. The starter program already has the buttons, the conveyor, a box spawner and a photo eye, so you only need to add a few things.
| Tag | Type | Used for |
|---|---|---|
Start | BOOL | Green push button, normally open |
Stop | BOOL | Red push button, normally open |
Motor | BOOL | Conveyor Forward bit |
PE1 | BOOL | Photo eye near the end of the belt |
Reset | BOOL | A new blue push button, normally open |
Batch | COUNTER | Counts boxes; set its preset to 5 |
Add the Batch tag in the Tags panel with type COUNTER. You'll set its preset to 5 when you add the CTU in rung 1: select the instruction and type the preset in its properties. Place a third push button, colour it blue and give it the tag Reset. Check that the photo eye sits across the conveyor with the tag PE1, and that the box spawner has an auto-drop interval (3 seconds works well) so the boxes arrive with gaps between them. The gaps matter: the eye has to see the beam clear between boxes, or two boxes touching look like one long box.
Rung 0: run the conveyor until the batch is done
| Start Stop Batch.DN Motor |
|----] [----+----]/[--------]/[-----------------------( )----|
| Motor | |
|----] [----+ |
This is the familiar seal-in, plus an XIO Batch.DN in series with Stop. The counter's done bit, DN, turns on when its accumulator reaches the preset. While it's off, XIO Batch.DN passes power and the rung behaves exactly like before. When the fifth box is counted, DN turns on, the XIO goes false, and it breaks the seal just as if someone had pressed Stop.
Because Batch.DN breaks the seal rather than just blocking the output, the conveyor stays stopped after the batch, even once the counter is reset. An operator has to press Start to run the next batch, which is how you'd want a real case packer to behave.
Rung 1: count the boxes
| PE1 |
|----] [------------------------------[CTU Batch]-----|
CTU adds 1 to Batch.ACC each time its rung goes from false to true. It doesn't keep counting while the rung stays true: it remembers the previous state in its CU bit. So a box sitting in the beam for a second still counts once, and you don't need a one-shot here.
Rung 2: reset for the next batch
| Reset |
|----] [------------------------------[RES Batch]-----|
RES sets Batch.ACC back to 0 and turns off DN, CU and CD. With the counter cleared, Start works again.
Test it
- Press Run, then click Start. Watch
Batchin the Tags panel go 1, 2, 3 as boxes pass the eye. - When it reaches 5/5 the conveyor stops. Clicking Start does nothing until you reset.
- Click Reset, then Start. The next batch begins.
Now look closely at the counter right after you press Reset. Depending on where the last box stopped, you may see the count jump straight to 1 before the belt has even moved. That's a real bug, and it would happen on a real PLC too.
The double-count bug
Here's what happens. The fifth box breaks the beam, the counter reaches 5 and the conveyor stops, with that box still sitting in front of the eye. PE1 stays on. When you press Reset, RES clears the accumulator, but it also clears the CU bit, which is the counter's memory that "the rung was already true". On the very next scan, rung 1 sees PE1 on and CU off, decides that's a new false-to-true transition, and counts the same box again. The new batch starts at 1 and will only take four new boxes.
The fix is to count boxes as they leave the beam instead of as they arrive. A box that's left the beam can't be counted twice. The falling-edge one-shot, OSF, does exactly that (see ONS, OSR and OSF). Add two BOOL tags, PE1_Mem and Box_Out, and replace rung 1 with these two rungs:
| PE1 |
|----] [---------------------[OSF Stor PE1_Mem Out Box_Out]--|
| |
| Box_Out |
|----] [------------------------------------[CTU Batch]-----|
Box_Out is on for a single scan as the back of each box clears the beam. The counter sees that one-scan pulse as a false-to-true transition and adds 1. Now the conveyor stops just after the fifth box has passed the eye, nothing is left blocking the beam, and a reset can't produce a phantom count.
Whenever a counter is reset while its input might still be on, ask yourself whether that input will be counted again. Counting on the falling edge, or resetting only when the input is off, are the two usual cures.
Try it in the Playground
Build both versions in the Playground and compare them. Run the first version, let a batch complete, and press Reset while a box is sitting in front of the eye: you'll see the count jump to 1. Switch to the OSF version and repeat. The count stays at 0 until a new box has passed.
Extensions to try
- Batch size from a tag. Add a DINT
BatchSizeand a rung withMOV BatchSize Batch.PRE, then change the batch size from the Tags panel while it runs (see math and move instructions). - A running total. Add a DINT
Totaland putADD Total 1 Totalon theBox_Outrung as a parallel output. BecauseBox_Outis a one-scan pulse, it adds exactly one per box, and it isn't cleared by Reset. - Automatic restart. Use a
TONonBatch.DNto wait 5 seconds (time to "swap the case"), then reset the counter and restart the conveyor automatically. You'll need to think about how the timer's done bit gets into the seal-in rung. - Sorting. Add a pop-up transfer before the eye and raise it for every third box, using a second counter, to divert boxes onto a side conveyor.