Lessons in this course (10)

4. Timers, counters and sequences

Intermediate★ Must knowLesson 4 of 10
pascal
// Structured Text (IEC 61131-3)
VAR
  tStartDelay : TON;     // on-delay timer
  ctParts     : CTU;     // up counter
  rtSensor    : R_TRIG;  // rising edge detector
END_VAR

tStartDelay(IN := StartRequest, PT := T#5S);
ConveyorRun := tStartDelay.Q;               // runs 5 s after the request

rtSensor(CLK := PartSensor);
ctParts(CU := rtSensor.Q, RESET := ResetCount, PV := 100);
BoxFull := ctParts.Q;                        // true after 100 parts
BlockBehaviour
TONOutput turns ON after input has been ON for preset time
TOFOutput stays ON for preset time after input turns OFF
TPFixed-length pulse
CTU / CTD / CTUDCount up / down / both to a preset

Sequences — use a state machine (SFC, or a CASE on a step number in ST): Idle → Fill → Heat → Mix → Drain → Idle, each step with entry conditions, timeouts and fault handling. Avoid scattering sequence logic across many unrelated rungs.