Two numbers the operator needs
A mix tank runs batches: a set number of ingredient doses, then a mix step. The operator wants two things on the HMI: doses left in this batch, and whether the tank has had its full 20 minutes of mixing, even if the agitator was stopped for a few minutes in the middle.
Those need a counter you can trust and a timer that keeps its time when it stops. (For lifetime motor hours that run for years, see our run-hours tutorial; timers alone aren't the right tool for that. For parts per hour, see the rolling-hour tutorial.)
Counting: why this example counts up
A Logix CTU adds 1 to .ACC each time its rung goes from false to true, so it already counts once per dose; no one-shot needed. Its .DN bit is on when .ACC is greater than or equal to .PRE, which is exactly "batch complete". "Doses left" is just .PRE minus .ACC.1
You might reach for CTD (count down) to show "doses left". It works, but read its help first:4
- It decrements on each false-to-true transition, and in prescan it sets its
.CDbit so it doesn't count on the first scan. - The done bit still means
.ACC≥.PRE. Counting down toward a preset,.DNgoes off as you get there, which reads backwards for "batch done". - It's typically paired with a
CTUon the same counter tag, for example parts into and out of a buffer. - Rockwell warns that unexpected operation may occur if members of a structure operand are overwritten, so think twice before writing
.ACCdirectly to "load" a countdown. Rung 0 writes.PRE(the CTU Preset operand writes the same member) to set the batch size and usesRESto zero.ACC. That is the documented way to give a counter a variable preset.
Counting up and subtracting avoids all of that.
RTO: time that survives a pause
A TON clears its time the moment its rung goes false, so a short agitator stop restarts the mix from zero. An RTO holds its accumulated time while the rung is false and only clears on a RES.1 Time it from proven running (the motor's run feedback), not from the start command, or you'll count mixing that didn't happen.
A Logix timer's preset and accumulated values are milliseconds in a DINT, so one timer tops out around 24.8 days. That's plenty for a mix step, and the reason we don't use a timer as a long-term hour meter.
The example in Studio 5000
Ladder
Six rungs in one ladder routine. On the CTU box, Preset and Accum are pseudo-operands that show .PRE and .ACC; rung 0 loads .PRE from BatchDoses. Rung 0 starts a batch from an HMI request bit that the PLC clears after acting on it. Open "as text" under any rung to copy it into the rung editor.
Drawn the way the Studio 5000 ladder editor lays it out: inputs on the left, outputs pushed to the right rail. The small numbers with arrows are where the editor shows live values; they read 0 here because nothing is online.
On a phone, swipe sideways to see the whole rung.
Rung 0 as text
XIC(Batch_StartReq)ONS(Batch_Start_ONS)[RES(DoseCtr),MOV(BatchDoses,DoseCtr.PRE),RES(MixTmr),OTL(Dosing_Active),OTU(Mixing),OTU(Batch_Done),OTU(Batch_StartReq)];Rung 1 as text
XIC(Dosing_Active)XIC(Dose_Confirmed)CTU(DoseCtr,0,0);Rung 2 as text
SUB(DoseCtr.PRE,DoseCtr.ACC,DosesLeft);Rung 3 as text
XIC(Dosing_Active)XIC(DoseCtr.DN)[OTU(Dosing_Active),OTL(Mixing)];Rung 4 as text
XIC(Mixing)XIC(Agitator_RunFbk)RTO(MixTmr,1200000,0);Rung 5 as text
XIC(Mixing)XIC(MixTmr.DN)[OTU(Mixing),OTL(Batch_Done)];All 6 rungs as text
XIC(Batch_StartReq)ONS(Batch_Start_ONS)[RES(DoseCtr),MOV(BatchDoses,DoseCtr.PRE),RES(MixTmr),OTL(Dosing_Active),OTU(Mixing),OTU(Batch_Done),OTU(Batch_StartReq)];
XIC(Dosing_Active)XIC(Dose_Confirmed)CTU(DoseCtr,0,0);
SUB(DoseCtr.PRE,DoseCtr.ACC,DosesLeft);
XIC(Dosing_Active)XIC(DoseCtr.DN)[OTU(Dosing_Active),OTL(Mixing)];
XIC(Mixing)XIC(Agitator_RunFbk)RTO(MixTmr,1200000,0);
XIC(Mixing)XIC(MixTmr.DN)[OTU(Mixing),OTL(Batch_Done)];Structured Text
Logix structured text doesn't have CTU, CTD or RTO; those are ladder instructions.1 Its timers and counters are the function-block versions (RTOR, TONR, CTUD).2 For a simple count, a plain DINT and a one-shot (OSRI)3 is easier to read than a counter block, so that's what this version uses. It runs in the same order as the rungs.
// Tags: DoseCount, DosesLeft, BatchDoses DINT; MixTmr FBD_TIMER;
// Dose_OS, BatchStart_OS FBD_ONESHOT
// Structured text has no CTU or RTO. This counts doses in a plain
// DINT with a one-shot, and uses RTOR (retentive, FBD_TIMER).
BatchStart_OS.InputBit := Batch_StartReq;
OSRI(BatchStart_OS);
MixTmr.Reset := BatchStart_OS.OutputBit;
IF BatchStart_OS.OutputBit THEN
DoseCount := 0;
Dosing_Active := 1;
Mixing := 0;
Batch_Done := 0;
Batch_StartReq := 0;
END_IF;
Dose_OS.InputBit := Dose_Confirmed;
OSRI(Dose_OS);
IF Dosing_Active AND Dose_OS.OutputBit THEN
DoseCount := DoseCount + 1;
END_IF;
DosesLeft := BatchDoses - DoseCount;
IF Dosing_Active AND DoseCount >= BatchDoses THEN
Dosing_Active := 0;
Mixing := 1;
END_IF;
MixTmr.PRE := 1200000; // 20 min in ms
MixTmr.TimerEnable := Mixing AND Agitator_RunFbk;
RTOR(MixTmr);
IF Mixing AND MixTmr.DN THEN
Mixing := 0;
Batch_Done := 1;
END_IF;
With RTOR, .Reset clears the time and the timer starts again once .Reset goes false. .ACC is kept while .TimerEnable is false, which is what makes it retentive. PRE is in milliseconds.2
Educational example. This code has not been compiled, downloaded, or tested on a controller. It is not production-ready or drop-in code. Treat it as a starting point to review, adapt, and test on your own system. It is standard control logic, not safety logic.
Gotchas
- Count the right thing. Count from a signal that proves the dose happened (a flow meter total, a weigh scale, or a valve-closed confirmation), not from the command bit. Debounce a chattering input with a short
TONbefore the counter. - Downloads and power cycles. Counter and timer values live in the controller. A program download puts back whatever was in the offline file. If a batch can be in progress during a download, decide what should happen and document it.
- Who can reset. Make batch start a request from the HMI that the PLC handles, and limit it to the right users in the HMI. Don't let a stray reset wipe a half-finished batch.
- Scan the timer. Rockwell's help notes a running timer must be scanned at least every 69 minutes to keep
.ACCcorrect, so don't put it in code that's skipped for long stretches.2
Sources
- Rockwell Automation, Logix 5000 Controllers General Instructions Reference Manual, publication 1756-RM003, "Timer and Counter Instructions" (CTU, RES, RTO, TON); also the online help index: rockwellautomation.com.
- Rockwell Automation, Studio 5000 Logix Designer online help, "Retentive Timer On with Reset (RTOR)": rockwellautomation.com.
- Rockwell Automation, "One Shot Rising with Input (OSRI)": rockwellautomation.com.
- Rockwell Automation, Studio 5000 Logix Designer online help, "Count Down (CTD)": rockwellautomation.com.
Instruction details were checked against the vendor documentation linked above. Behavior can change between software and firmware versions, so check the help for the version you run.