PLC Programming · Lab Manual

Siemens PLC Counters CTU, CTD and CTUD: Production Counting Lab Manual

A production counter must count each part once – not once per scan, not twice when the sensor bounces, and not zero after a power cut. This lab manual builds it through all six stages and lets you compare level, edge and debounced counting against the real number of parts.

2,500+ engineers trained 4.9/5 Google rating 21+ years, Chinchwad, Pune Next batch: contact for dates
Quick answer

Count each confirmed part once: debounce the sensor in the input mapping, feed the clean signal to CTU (which counts rising edges), keep the batch counter separate from a lifetime total, make both retentive where the business needs it, and reset only the batch, under control.

  • Count edges, never levels.
  • Debounce a bouncing sensor before counting.
  • Batch and lifetime are two counters with two reset rules.

Why PLC Production Counters Overcount

Three causes explain almost every wrong count: incrementing on the level instead of the edge (one part = many scans), a bouncing sensor that produces several edges per part, and a counter that is reset or lost when it should not be. Each has a simple fix, shown stage by stage below.

Lab Flow: From Part Sensor to Batch Complete

Every lesson in the Softwell PLC lab manual series follows the same six stages and the same address rule, so the code you write here plugs straight into the HMI lessons.

Lab manual flow: field input to tested HMI screenSix stages: field device and PLC tag, input mapping to DB_IO, PLC logic, HMI tags, graphics linking, and perform and test.1Field + tags%I7.1 B60_Partpart sensor2Input mappingdebounce 5 ms→ DB_IO3LogicCTU + DIntbatch, lifetime4HMI tagsDB_Count_*CV, PV, Q5GraphicsCounter screenreset with rights6Perform + testSimulatorbounce, power cutAddress rule for every lesson: %I only in FC_InputMapping, %Q only in FC_OutputMapping, symbolic DB members everywhere else – including the HMI.
Figure 1 – Six-stage lab flow for this lesson.

Stage 1Field Signal and PLC Tags

PLC tagAddressTypeDevice / comment
B60_Part%I7.1BoolThrough-beam sensor at the outfeed
H60_BatchDone%Q3.6BoolBatch complete lamp

Stage 2Input Mapping with Debounce

// FC_InputMapping (or an FB with a static timer)
"T_B60_Deb"(IN := "B60_Part" XOR "DB_IO".B60_Part, PT := T#5MS);   // must stay different for 5 ms
IF "T_B60_Deb".Q THEN
    "DB_IO".B60_Part := "B60_Part";                                // accept the new, stable state
END_IF;

For pulses shorter than the scan, use a high-speed counter input instead – see the scan cycle lesson.

Stage 3Logic: CTU for the Batch, DInt for the Lifetime

// FB_Production – STAT: C_Batch : CTU_INT; PartOld : Bool;
#C_Batch(CU := "DB_IO".B60_Part, R := "DB_Count".Cmd_BatchReset, PV := "DB_Count".BatchPreset);
"DB_Count".BatchCV   := #C_Batch.CV;
"DB_Count".BatchDone := #C_Batch.Q;

IF "DB_IO".B60_Part AND NOT #PartOld THEN                       // lifetime: never reset
    "DB_Count".Lifetime := "DB_Count".Lifetime + 1;             // DInt, Retain
END_IF;
#PartOld := "DB_IO".B60_Part;
"DB_Count".Cmd_BatchReset := FALSE;                             // one-shot from the HMI

// FC_OutputMapping
"H60_BatchDone" := "DB_Count".BatchDone;

Shift totals, snapshots and CSV export build on this in the shift and production report.

Stage 4Link PLC Tags to the HMI

HMI tagPLC memberUsed by
DB_Count_BatchCV / _BatchPreset"DB_Count".BatchCV / BatchPresetCV/PV fields
DB_Count_Lifetime"DB_Count".LifetimeLifetime field
DB_Count_Cmd_BatchReset"DB_Count".Cmd_BatchResetReset button

Stage 5Link the Graphics

#ObjectPropertiesTag / event
①Output field CVFormat 99999DB_Count_BatchCV
②Input field PVLimits 1 – 9999, authorisation SupervisorDB_Count_BatchPreset
③Button BATCH RESETConfirmation; authorisation SupervisorSetBit DB_Count_Cmd_BatchReset
④Lamp batch completeAppearance on BatchDoneDB_Count_BatchDone

Who may reset counters is set in HMI user administration.

Stage 6Perform and Test: Three Ways to Count One Part

The same sensor feeds three counters: counting the level (wrong), counting the raw rising edge (wrong when the sensor bounces) and counting the debounced edge into CTU (right). Reset the batch, change the preset and power-cycle the CPU.

Conveyor and PLC
HMI · Counter screen
Line 6 · Counting
Counting the level (wrong)
Raw rising edge
Debounced edge → CTU
Batch CV / PV
Lifetime total
Batch preset PV

Troubleshooting

SymptomLikely causeFirst check
Count jumps by hundredsCounting the levelUse CTU or an edge
Two counts per partSensor bounceDebounce in the mapping
Count lost after power cutNot retentiveRetain in DB_Count
Lifetime reset with batchSame counter used for bothSeparate counters

Hands-On Lab: Build and Prove a Production Counter

Hands-on
Before you start
  • TIA Portal with an S7-1200 or PLCSIM
  • A sensor or simulated input
  • Estimated time: 60 minutes
1

Wire and declare the sensor

Declare B60_Part and the batch-complete lamp.

Sensor and lamp are declared.
2

Debounce in the input mapping

Accept a new sensor state only after it is stable for 3 scans or 5 ms.

DB_IO.B60_Part changes only after a stable 5 ms.
3

Count with CTU and a lifetime total

Use C_Batch (CTU, PV from the HMI) and a retentive DInt lifetime total.

CV counts one per part; Q at PV; lifetime keeps counting.
4

Link HMI tags

Publish CV, PV, Q and the lifetime total.

HMI shows CV, PV and lifetime.
5

Build the counter screen

Show CV/PV, a preset input and a reset button with rights.

Only a Supervisor can reset the batch.
6

Prove it

Compare with real parts, inject bounce, reset the batch and power-cycle the CPU.

Counts match real parts with bounce and survive a power cycle.
Checkpoint—how to know you did it right

Each part is counted exactly once, the batch resets under control, the lifetime total never resets, and counts survive a power cut.

Frequently asked questions

Why does my PLC counter increase every scan?

The program increments while the signal is true instead of only when it changes from false to true. Use a rising-edge pulse or an IEC counter, which counts edges at CU.

When should I use a high-speed counter?

When the input pulse can be shorter than the input filter or the scan cycle, such as encoders and fast packaging lines.

Should production counters be retentive?

It depends on their meaning. Batch and shift totals usually need retention through brief outages; temporary diagnostic counts may not. Document the restart and reset philosophy.

How do I stop a bouncing sensor from double counting?

Accept a new state only after it has been stable for a short time (a few ms or a few scans) and count the edge of that debounced signal.

Why keep a batch counter and a lifetime counter?

They answer different questions. The batch resets for every order; the lifetime total never resets and drives maintenance and machine history.

What do CU, R, PV, CV and Q mean on CTU?

CU counts rising edges, R resets CV to zero, PV is the preset, CV the current value, and Q becomes true when CV reaches PV.

Reviewed by Bhawesh Kumar SinghIndustrial Automation Trainer and Industry 4.0 Consultant · Softwell Automation · 21+ years industry experience

Get the full syllabus + free demo class

Share your details—a Softwell training advisor will contact you with batch dates, fees and hardware-practice options.

No spam. Used only to share course details for this enquiry.

Learn with practical industrial examples

Join live online, Pune classroom or corporate in-plant automation training.

Request Course Details
Verified learning pathway

Discuss PLC Counters and Production Counting

Explore practical curriculum, software, hardware and batch options for this technology.

Content reviewed: 16 October 2026

Siemens PLC & TIA Portal Learning Path

Continue with the related Siemens PLC tutorials in this practical learning series.

  1. SCL vs Ladder Logic
  2. Upload PLC Program
  3. TIA Selection Tool
  4. Analog Input Scaling
  5. PLC Counters
  6. PLC Timers
  7. Addressing & Data Types
  8. Hardware & PLC Tags
  9. OB, FB, FC & DB
☎ Call WhatsApp ✉ Email Enquire Now