04 · Edges and part counting

Practice module 4 of 5 · Ladder + Siemens SCL

One part can occupy many scans.

Count arriving parts, not scans. Reason about rising edges, reset and preset behavior.

20–25 minutesWritten predictions + self-checkNo account required

04 / Count events, not assumptions

What you will be able to prove

  • Distinguish a sampled level, a rising event and a physical part.
  • Prove reset priority and repeat-batch behavior.
  • Recognize bounce and pulses that cyclic logic cannot observe.

Before this module: Module 03 plus the rising-edge worked example below.
Core: 20–25 min · Applied lab: 25–35 min. These are planning estimates; work at your pace.

Your deliverable: An event trace, a batch-count test sheet and a sampling-limit explanation.

Predict → trace → challenge → explain. The written labs are self-assessed. Use paper or your own compatible simulation. The browser workshop supports a limited training subset; these SCL excerpts are not import-ready or TIA-compiled blocks.

Your next step

Start small. Build from there.

New to this topic? Follow one part arriving and leaving. Compare a change from FALSE to TRUE with a signal that stays TRUE.

Optional hint: the idea in plain words

A rising edge is a change from FALSE to TRUE between calls. A sensor staying TRUE over several scans is still one arrival, not several parts.

Work in one language first, then compare the other. After the prediction, open step 3 for guidance or step 4 for an independent challenge.

Before you reveal: write your answer and one sentence explaining it. Use a notebook or your own editor. These pages do not execute code, auto-grade answers, or save progress.

01 · WORKED EXAMPLE

Count a transition, not a TRUE level.

Input PartPresent may remain TRUE for many scans as a box passes a sensor. A rising edge is FALSE→TRUE. Use a dedicated Static PartEdge : R_TRIG instance, called once every scan, with BOOL output Pulse. For the trace below, edge history is initially FALSE.

Ladder

Call R_TRIG PartEdge every scan:
  CLK <- PartPresent
  Q   -> Pulse
LAD network notation; contacts test Boolean values.

SCL

#PartEdge(CLK := #PartPresent,
          Q => #Pulse);
SCL function-block body excerpt.

With CLK values 0, 1, 1, 1, 0, 1, Pulse is 0, 1, 0, 0, 0, 1. The instance remembers the previous query. Reusing it for another input or skipping calls changes that history. A TRUE input on the first call may count as an edge; define startup behavior.

02 · PREDICT

An IEC counter already detects edges.

Use a dedicated Static Parts : CTU_INT instance, INT output Count, BOOL output BatchReady, and BOOL input Reset. CTU increments on rising CU, so an extra R_TRIG is unnecessary when the only job is counting. PV=3; Reset=TRUE resets Count and takes priority over counting. Call it every scan.

Ladder

Call CTU Parts every scan:
  CU <- PartPresent
  R  <- Reset
  PV <- 3
  Q  -> BatchReady
  CV -> Count
LAD network notation; contacts test Boolean values.

SCL

#Parts(CU := #PartPresent,
       R := #Reset,
       PV := 3,
       Q => #BatchReady,
       CV => #Count);
SCL function-block body excerpt.

Q means Count ≥ PV. The counter does not stop at PV: it continues toward its integer type limit. Use a positive preset for this task; do not generalize reset always makes Q FALSE when PV could be zero or negative.

For the paper trace, start with Count=0 and previous CU=0. In a simulation, initialize the counter instance by holding PartPresent FALSE, executing one call with Reset TRUE, then one with Reset FALSE. Do not initialize it by writing only the connected Count output. Reset is FALSE during the trace until the final row. Fill Count and BatchReady.

CTU trace with PV = 3
ScanPartPresentResetCount?BatchReady?
100??
210??
310??
400??
510??
600??
710??
800??
910??
1001??
Reveal the answer and why

Count: 0, 1, 1, 1, 2, 2, 3, 3, 4, 0. BatchReady: 0, 0, 0, 0, 0, 0, 1, 1, 1, 0. Scan 3 does not count again while the signal stays TRUE. Scan 9 goes beyond the preset. Reset clears the count in scan 10.

03 · FINISH THE SOLUTIONReject the per-scan counting bug.
IF #PartPresent THEN
  #Count := #Count + 1;
END_IF;
Faulty manual count for a box that occupies the sensor for several scans.

A box holds the sensor TRUE for 5 scans. How many increments occur? Replace this code with the CTU call. If you wanted a reusable one-scan event too, which block would you call?

Reveal the answer and why

The faulty code increments 5 times. CTU counts one FALSE→TRUE transition. Use the CTU call above with PartPresent on CU. R_TRIG supplies a shared Pulse if other logic also needs the event. Neither approach fixes sensor bounce or a pulse too short to be sampled; those require suitable signal conditioning or hardware handling.

04 · INDEPENDENT VARIANTCount trays in a batch of five.

Change the counter preset to 5. BOOL TraySensor is TRUE while a tray is present. BOOL ClearBatch resets the count; INT TrayCount and BOOL BatchComplete are outputs. Create your own persistent counter instance, draw LAD block pin connections and write SCL. Test a held sensor, five distinct rising edges, a sixth tray, and ClearBatch=TRUE during an arriving edge.

Compare your independent solution

Ladder

Call CTU Trays every scan:
  CU <- TraySensor
  R  <- ClearBatch
  PV <- 5
  Q  -> BatchComplete
  CV -> TrayCount
LAD network notation; contacts test Boolean values.

SCL

#Trays(CU := #TraySensor,
       R := #ClearBatch,
       PV := 5,
       Q => #BatchComplete,
       CV => #TrayCount);
SCL function-block body excerpt.

Declare Trays as Static CTU_INT and TrayCount as INT. A held sensor adds only one. The fifth distinct edge reaches Count=5 and Q=TRUE. The sixth reaches 6 with Q still TRUE. With ClearBatch=TRUE, Count remains 0 and Q=FALSE for this positive preset, even if an arriving edge occurs.

05 · EXPLAIN YOUR TESTWhen is a new edge actually new?

Write a three-scan input pattern that produces two rising edges, given previous CU=0. Then explain why a continuously TRUE sensor cannot do it.

Reveal the answer and why

Use 1, 0, 1. The FALSE sample rearms rising-edge detection. A continuous TRUE level contains no new FALSE→TRUE transition. If a pulse rises and falls entirely between samples, ordinary cyclic logic may miss it; do not assume the counter measures every real-world part at any speed.

APPLIED LAB / BUILD • INJECT • PROVE

Applied lab: make a defensible batch counter

A virtual packaging lane needs a batch of five parts. Use the dedicated Trays counter from the independent variant. Initialize its instance with the sensor FALSE and reset TRUE, then release reset. Write the expected count before running each pattern.

Sensor patterns are successive sampled values, not physical time
Pattern or conditionQuestion to prove
0,1,1,1,0Why is this one event, not three?
0,1,0,1,0How many rises does the PLC actually see?
Five separate rises, then a sixthDoes the count stop at the preset? Does BatchComplete clear?
ClearBatch TRUE on the same call as a rising sensorWhich action has priority?
ClearBatch, sensor FALSE, then a new batchCan a second complete batch work without reinitializing the program?
Check the expected observations

The first pattern adds one. The second adds two, even if you describe the physical cause as “one bouncing part.” Five rises reach 5 and completion becomes TRUE; a sixth reaches 6 with completion still TRUE. For this positive preset, reset keeps the count at zero and completion FALSE. After a reset and a FALSE sensor observation, five new rising events must again reach 5.

Fault ticket: the display says 8, but only 5 parts passed

  1. Compare the physical event description with the sampled sensor trace.
  2. Look for increments every scan, bouncing transitions, multiple calls or another writer.
  3. Separate a software counting fault from an input-signal problem.
  4. Retest one held part, separated parts, reset plus arrival and a full second batch.

The missed-pulse test

Suppose a simplified cyclic input is sampled only at 0 ms and 20 ms. A pulse rises at 5 ms and falls at 10 ms. Both samples are FALSE: a cyclic R_TRIG cannot reconstruct the missing pulse. Faster hardware capture, input filtering and high-speed counting are separate engineering choices to investigate for the actual sensor/CPU, not features this lesson promises.

Evidence to keep: a sampled-input trace with edge marks, count after each call, and one sentence explaining what your test does not prove about real production speed.

Move on when: you can solve the independent variant in both representations, explain the edge cases, and fix any error without copying. A correct guess is a review item.