The count jumped by 40
An operator presses a button on the HMI to add one reject to the shift count. The count goes up by 40. Nothing is wrong with the button. The PLC scanned that rung 40 times while the button was held, and the ADD ran every time.
Most PLC instructions do their job on every scan their rung is true. That's right for "keep the motor running while the start is sealed in", and wrong for "do this once when something happens". One-shots turn "the button is held" into "the button was pressed just now".
ONS, OSR and OSF in plain words
- ONS (One Shot): sits in the middle of a rung like a contact. On the one scan the logic to its left goes false to true, it lets the rest of the rung be true. Every other scan, the rest of the rung is false.1
- OSR (One Shot Rising): sits at the end of a rung. On the false-to-true scan it sets an output bit for one scan. You can use that bit in any other rung.2
- OSF (One Shot Falling): the same as OSR, but it fires on the true-to-false scan: the moment a pump stops, a door closes, or a button is released.2
Each one needs a storage bit. That's the instruction's memory of the last scan. Give every one-shot its own storage bit and never write to it anywhere else. Two one-shots sharing a storage bit is one of the most common "it works sometimes" bugs we find.
Which one do I use?
Need the edge on just this rung? ONS. Need a pulse bit that several rungs can read? OSR. Need to act when something turns off? OSF. Counters (CTU/CTD) already count once per false-to-true transition, so they don't need a one-shot in front of them.
The example in Studio 5000
One routine covers both cases: a manual "add reject" button that must count once per press, and a pump where we want to count starts and stops and capture the flow reading at the moment it stops. These are made-up tags; rename them to your standard.
Ladder
Five rungs in one ladder routine, top to bottom. Open "as text" under any rung to copy it into the Studio 5000 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(PB_AddReject)ONS(AddReject_ONS)ADD(RejectCount,1,RejectCount);Rung 1 as text
XIC(Pump_RunFbk)OSR(Pump_Start_SB,Pump_StartPulse);Rung 2 as text
XIC(Pump_RunFbk)OSF(Pump_Stop_SB,Pump_StopPulse);Rung 3 as text
XIC(Pump_StartPulse)ADD(PumpStarts,1,PumpStarts);Rung 4 as text
XIC(Pump_StopPulse)[ADD(PumpStops,1,PumpStops),MOV(Pump_Flow,FlowAtLastStop)];All 5 rungs as text
XIC(PB_AddReject)ONS(AddReject_ONS)ADD(RejectCount,1,RejectCount);
XIC(Pump_RunFbk)OSR(Pump_Start_SB,Pump_StartPulse);
XIC(Pump_RunFbk)OSF(Pump_Stop_SB,Pump_StopPulse);
XIC(Pump_StartPulse)ADD(PumpStarts,1,PumpStarts);
XIC(Pump_StopPulse)[ADD(PumpStops,1,PumpStops),MOV(Pump_Flow,FlowAtLastStop)];Structured Text
Same logic in one structured text routine. Logix structured text doesn't have ONS, OSR or OSF; its one-shots are OSRI (rising) and OSFI (falling), which use an FBD_ONESHOT tag.3 Set .InputBit, call the instruction, then read .OutputBit, in that order.
// Each edge gets its own FBD_ONESHOT tag:
// AddReject_OS, PumpStart_OS, PumpStop_OS : FBD_ONESHOT
// OSRI / OSFI are the structured-text one-shots in Logix.
// ONS, OSR and OSF are ladder-only.
AddReject_OS.InputBit := PB_AddReject;
OSRI(AddReject_OS);
IF AddReject_OS.OutputBit THEN
RejectCount := RejectCount + 1;
END_IF;
PumpStart_OS.InputBit := Pump_RunFbk;
OSRI(PumpStart_OS);
Pump_StartPulse := PumpStart_OS.OutputBit;
PumpStop_OS.InputBit := Pump_RunFbk;
OSFI(PumpStop_OS);
Pump_StopPulse := PumpStop_OS.OutputBit;
IF Pump_StartPulse THEN
PumpStarts := PumpStarts + 1;
END_IF;
IF Pump_StopPulse THEN
PumpStops := PumpStops + 1;
FlowAtLastStop := Pump_Flow;
END_IF;
If you'd rather not use the function-block one-shots, the classic "last value" compare does the same job in any PLC that has structured text:
// Same rising edge without OSRI: compare with last scan's value.
// AddReject_Last : BOOL (one per signal, used for nothing else)
IF PB_AddReject AND NOT AddReject_Last THEN
RejectCount := RejectCount + 1;
END_IF;
AddReject_Last := PB_AddReject; // update AFTER the test, every scan
On Siemens and other IEC 61131-3 platforms you'll see R_TRIG and F_TRIG for the same thing. Those are IEC function blocks; they aren't Rockwell instructions, so don't go looking for them in Studio 5000.
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
- HMI buttons. A momentary HMI button sets a bit and clears it a moment later. If the HMI and PLC update times line up badly, the PLC can miss the press or see it twice. For anything that changes a count or a recipe, have the HMI set a request bit and let the PLC clear it after acting on it.
- Pulses and tasks. An OSR/OSF output bit is true for one scan of the task it lives in. Logic in a different task (say a slower periodic task) can miss it completely. Use the pulse in the same task, or latch it and unlatch it where it's used.
- Read order in structured text. If you read
.OutputBitabove theOSRIcall, you're reading last scan's result, one scan late. - Code that isn't scanned. A one-shot inside a subroutine that isn't called, or inside an
IFthat's false, doesn't see the input change. When it's scanned again, it may fire late or not at all. Keep one-shots where they run every scan. - Start-up. In prescan, Rockwell sets the
ONSandOSRstorage bits true so they don't fire falsely on the first scan; an input that's already on at start-up is not treated as a new press.12OSRI/OSFIlikewise clear their history so a real transition is needed.3 If you need something to happen at power-up, use the first-scan bitS:FS, not a one-shot. - Not for safety. A one-shot is standard control logic. Stop functions and E-stops belong in the safety system, not in a pulse bit.
Sources
- Rockwell Automation, Studio 5000 Logix Designer online help, "One Shot (ONS)": rockwellautomation.com.
- Rockwell Automation, "One Shot Rising (OSR)" and "One Shot Falling (OSF)": OSR, OSF.
- Rockwell Automation, "One Shot Rising with Input (OSRI)" and "One Shot Falling with Input (OSFI)", FBD_ONESHOT structure: OSRI, OSFI.
- Rockwell Automation, Logix 5000 Controllers General Instructions Reference Manual, publication 1756-RM003: PDF.
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.