Learn › Instructions
ONS, OSR and OSF: One-shot instructions
A one-shot turns a signal that stays on for a while (a held button, a box sitting in front of a sensor) into a pulse that lasts exactly one scan. It's how you make something happen once per press instead of on every scan.
Why you need a one-shot
A PLC solves your whole program over and over, many times a second (see How a PLC scan cycle works). In the Playground the program scans once per screen refresh, usually around 60 times a second. If you hold a push button for half a second, every rung that reads it sees it as true for roughly 30 scans in a row.
For coils that's exactly what you want: the output is on while the button is held. For instructions that do something each scan they're true, it's a problem. An ADD that adds 1 to a total would add about 30 for one press. A MOV that copies a reading would keep copying it. A one-shot fixes this by passing power for the single scan when its rung goes from false to true, and then going false again even though the button is still held.
Counters (CTU and CTD) already have this built in: they only count on the false-to-true transition, using their CU and CD bits to remember the last state. You don't need a one-shot in front of a counter. You do need one in front of math, moves and anything you want to trigger once.
ONS: One Shot
ONS sits in the middle of a rung, like a contact. It takes one tag, a BOOL storage bit, which it uses to remember whether its rung-in was true on the previous scan. Each scan:
- If the power coming into it is true and the storage bit is off, the rung just went true.
ONSpasses power on for this scan. - In every other case (still true from last scan, or false) it passes no power.
- Either way, it then copies its rung-in into the storage bit, ready for the next scan.
| AddBtn Add_ONS |
|----] [------[ONS]------[ADD A Total B 1 Dest Total]--|
Everything to the right of the ONS sees one true scan per press. Everything to its left behaves normally.
Give every one-shot its own storage bit, and don't use that bit anywhere else in the program. If two one-shots share a storage bit, or another rung writes to it, they'll fight over it and miss or double up pulses.
OSR: One Shot Rising
OSR is an output instruction, so it goes in the last column of a rung. It has two operands, both BOOL tags: a Storage Bit and an Output Bit. When its rung goes from false to true, it turns the Output Bit on for one scan. On every other scan the Output Bit is off.
| PE1 |
|----] [---------------------[OSR Stor PE1_Mem Out PE1_Edge]--|
| |
| PE1_Edge |
|----] [---------------------[ADD A Boxes B 1 Dest Boxes]----|
The difference from ONS is where the pulse ends up. ONS passes power along its own rung; OSR puts the pulse in a bit that any rung can read. That's handy when several rungs need the same edge, or when you want to name it (Start_Pressed reads better than another copy of the button logic).
Remember rung order: the Output Bit is set when the OSR rung is solved, so rungs below it see the pulse in the same scan. Rungs above it see it on the next scan, by which time it's already off. Put rungs that read the pulse after the OSR.
OSF: One Shot Falling
OSF works the same way as OSR, with a Storage Bit and an Output Bit, but it fires on the opposite edge: the Output Bit is on for one scan when the rung goes from true to false.
Falling edges are useful when something should happen when a signal ends. A box has fully passed a photo eye when the eye's tag turns off. A push button has been released. A machine cycle has finished when its running bit drops.
| PE1 |
|----] [---------------------[OSF Stor PE1_FMem Out Box_Cleared]--|
Which one to use
| Instruction | Where it goes | Operands | Fires when |
|---|---|---|---|
ONS | Middle of the rung | Storage bit | Rung-in goes false → true; passes power for one scan |
OSR | Last column | Storage Bit, Output Bit | Rung goes false → true; Output Bit on for one scan |
OSF | Last column | Storage Bit, Output Bit | Rung goes true → false; Output Bit on for one scan |
Use ONS when the pulse only matters to that one rung. Use OSR or OSF when you want a named pulse bit, or when you need the falling edge.
Try it in the Playground
- Open the Playground. In the Tags panel add a DINT tag called
Totaland a BOOL calledAdd_ONS. - Add a rung:
XIC Start, then anADDin the last column with Source ATotal, Source B1and DestTotal. - Press Run, then press and hold the green Start button for a second. Watch
Totalin the Tags panel shoot up by dozens. - Stop, select the space between the
XICand theADD, and add anONSwith the tagAdd_ONS. Run again: now each press adds exactly 1.
Common mistakes
- Reusing a storage bit. Each one-shot needs its own.
- Putting a one-shot in front of a counter. It isn't wrong, but it's unnecessary:
CTUandCTDalready count edges. - Reading an OSR pulse above the OSR rung. The pulse is gone before that rung is solved again.
- Expecting to see the pulse. One scan is a fraction of a second. If you need a visible result, use the pulse to latch a bit or add to a total, then watch that.