Count events, not scans
I can distinguish one bottle from many scans and reset a count deliberately.
Practise it now: Count the arrivals Free, no sign-up. New to this? Read the lesson first.
1. Why it matters
One bottle can sit under a for hundreds of . Count every scan and you could report a full batch with only one bottle there.
2. The idea in plain words
A is the moment a signal changes from to . Staying TRUE on the next scan is not another arrival.
Example: a bottle arrives and the sensor reads FALSE, TRUE, TRUE, TRUE. That is one rising edge, so one bottle.
Edge instructions remember the from the last scan. That lets them give a pulse that lasts one scan only.
Siemens uses R_TRIG and Logix uses OSRI. They are built differently and behave differently at startup.
A adds one for each event. In the mission it is already declared as Count. Clear it on purpose, with its reset input.
Siemens CTU: CU is the signal to count. R clears the total. PV is the target. Count.CV is the current total, and Count.Q turns TRUE at the target.
Logix ST CTUD: CUEnable counts up and CDEnable counts down. PRE is the target and Reset clears it. Leave CDEnable FALSE when you only count arrivals.
Count.ACC is the Logix total, and Count.DN shows the target was reached.
Start with the sensor FALSE, then make it TRUE. Logix runs startup checks first, and this gives it a fresh arrival after them.
In , an edge needs its own memory bit. In Siemens LAD, a P contact stores the ’s previous state in a separate memory bit.
In Logix Ladder, ONS stores the incoming power. OSR has a separate storage bit and bit.
Use the pulse to mark one event, then count it. Keep the edge memory separate from the batch-counter reset.
The exercise starts with the sensor clear, because each vendor handles startup (prescan) differently.
A rising edge (FALSE to TRUE) marks a bottle arriving. A (TRUE to FALSE) marks it leaving. A bottle sitting at the sensor is neither.
Siemens uses F_TRIG for a falling edge. LAD uses an N contact with its own memory bit. Rockwell Ladder uses OSF, with storage and output bits.
In you can do it yourself. Look for a FALSE reading now and a TRUE reading last scan. Then save the current reading for the next scan.
Keep saving that reading during a counter reset too.
3. See it happen
// Count is an IEC_COUNTER instance.
#Count.CTU(CU := #BottlePresent, R := #Reset, PV := 3);
#Done := #Count.Q;Will holding the sensor TRUE for one second count fifty bottles?
Decide your answer first. Then step through the scans to check it.
| Input or event | Result |
|---|---|
| Sensor FALSE → TRUE | ? |
| Sensor remains TRUE | ? |
| FALSE, then TRUE again | ? |
A mistake to look for
Adding one on every scan while the sensor is on. When a count looks wrong, look at the sensor’s real on/off pattern, not only the final total.
4. Vendor notes
Siemens notes
CTU counts each rising edge on CU. CV is the current count, and Q turns TRUE when it reaches PV. R resets it.
In LAD, P and N edge contacts each need their own BOOL memory bit. R_TRIG and F_TRIG use their own instances.
A MOVE box copies a value; it does not count. Wire Count.CV to IN and Pieces to OUT. When CV = 2, Pieces = 2. The next event makes CV = 3, and the next copy makes Pieces = 3.
In SCL, #Pieces := #Count.CV; is the same copy. Put the counter before the copy, so the copy gets the latest count.
ADD with IN1 State, IN2 1 and OUT State adds one on every scan its rung is powered. So only let an edge trigger it. A plain sensor contact held on would count scans, not bottles.
5. On site
On a drinks line, one bottle can sit in front of the counting sensor for a while. It still counts as one bottle.
Counting every scan can turn one bottle into hundreds. The line could report a full batch while most bottles are missing.
At a soft-drinks factory, empty bottles arrive on a moving belt. A sensor tells the controller when a bottle is under the filling nozzle. The valve opens, fills the bottle, then closes before the belt moves again. Caps and labels go on before the bottles are packed.
- Empty bottlesThe belt brings them to the nozzle
- Fill each bottleStop it, fill it, then let it moveYou are here
- Add caps and labelsSeal the drink and name the product
- Pack the bottlesPut finished bottles into boxes
On a real site: places, parts and safety
- Soft-drinks factory — filling bottles without spills
- Dairy — putting milk into bottles
- Cleaning-products factory — measuring liquid into containers
- Food packaging line — counting bottles before packing
Recognise it on site
- On the bottle conveyor, a motor drives the belt. Rails guide the bottles and guards cover the moving parts.
- The bottle sensor uses a light beam to spot a bottle under the filling nozzle.
- At the filling station, a valve above the bottle opens to fill it, then closes to stop.
- The tracked bottle holds 1 L. Only this bottle drives the readings. The others are there to show the line.
- The small control station has buttons, status lights and a PLC that runs the programme.
If the filling valve opens without a bottle under it, liquid spills onto the belt. If the line jams, the valve must not keep filling.
Real conveyors need guards. The power must be safely locked off before anyone clears a blockage. The Stop input here is not an emergency stop.
The sizes, timings and cutaways are teaching choices, not a drawing of any one real installation.
6. Try it on the rig
Hold BottlePresent TRUE for many scans and watch the count. Then give it three separate pulses and compare.
Open the mission, make a prediction, run the equipment, then test your programme. Passing the tests completes practice; reading this page alone does not.
10. Count the arrivals11. Count with a one-shot12. Count bottles as they leave13. It never reaches three14. Review the AI: One bottle, one count7. Check your understanding
Predict a new case. A wrong answer gives you an explanation and another try.
1. Over five scans, the bottle sensor reads FALSE, TRUE, TRUE, FALSE, TRUE. How many bottles does the counter add?
#Count.CTU(CU := #BottlePresent, R := #Reset, PV := 3);