Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 37 ยท Ladder Logic

Edge Detection and One-Shot Pulses

Learn rising and falling edge detection in PLCs, R_TRIG and F_TRIG blocks, one-shot (OSR) rungs, toggle buttons and the one-scan-pulse rule, with runnable C simulations.

A contact tells you whether a bit is on. Often you need to know when it just became on. A counter should add one per button press, not one per scan while the button is held. A toggle switch should flip once per press. For these you need edge detection.

This lesson explains rising and falling edges, how to build a one-shot with a "previous value" bit, the IEC R_TRIG and F_TRIG blocks, and classic uses such as counting and toggling.

The problem: levels vs edges

A person holds a button for maybe 200 ms; a scan takes 5 ms. So the button bit is 1 for about 40 scans. A rung that adds 1 every scan the bit is on would add 40. You want one action at the moment the bit goes from 0 to 1: a rising edge. A falling edge is the 1 to 0 transition.

flowchart LR
    A["Button bit: 0 0 1 1 1 1 0 0"] --> B["Rising edge: 0 0 1 0 0 0 0 0"]
    A --> C["Falling edge: 0 0 0 0 0 0 1 0"]

Figure: a rising edge is true for exactly one scan.

Building an edge by hand

Remember the previous scan's value in a memory bit, and compare:

rising = current AND NOT previous then previous = current (at the end).

In ladder the pattern is a rung with Button and /Button_Prev that drives the pulse, followed by a rung that copies Button into Button_Prev. Order matters: the copy must come after the logic that uses the old value (see the scan cycle).

C Stand-in: rising and falling edge detection (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

IEC edge blocks

The standard supplies two function blocks. Each instance keeps its own "previous" bit.

TEXT
VAR
    StartEdge : R_TRIG;       (* rising edge *)
    StopEdge  : F_TRIG;       (* falling edge *)
END_VAR

StartEdge(CLK := Start_Button);
IF StartEdge.Q THEN
    PartCount := PartCount + 1;
END_IF;

Q is true for one scan after the edge. Siemens ladder calls these contacts P and N; Rockwell uses ONS (one-shot) and OSR/OSF. Different names, same idea.

Edge instructions

Rising 0 to 1: R_TRIG
Falling 1 to 0: F_TRIG
One scan Output lasts one scan

Each use needs its own memory bit

1 / 2

Use 1: count presses

C Stand-in: count presses, not scans (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Only three products passed, so the edge count is the correct one (3).

Use 2: toggle with one button

A single push button can turn a lamp on and off alternately: on each rising edge, flip the state.

C Stand-in: one-button toggle (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Use 3: a start pulse for a sequence

Edges are also the right trigger for "start the cycle" commands. If a sequence were started by the level of Start, holding the button would restart the sequence every time it ended. Starting on the rising edge means one press = one cycle.

Common mistakes

  • Sharing one edge bit across several uses. Each edge needs its own memory bit, or one use will clobber the other.
  • Putting an edge inside code that does not run every scan. If an IF branch skips the edge block for a few scans, it misses transitions. Call edge blocks every scan.
  • Updating the previous bit too early, which makes the edge always false.
  • Edge on a bouncing input. A noisy contact produces several rising edges. Debounce first (digital and analog I/O).
  • Too-short pulses. If a signal lasts shorter than one scan, no edge can be seen at all.
C Stand-in: bounce makes extra edges (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A rising-edge output is true for how long?

    The pulse appears only on the scan where the input changed from 0 to 1.
  2. 2. How is a rising edge computed by hand?

    It is true when the bit is on now but was off last scan.
  3. 3. Why must the 'previous' bit be updated after the edge logic?

    The edge compares with the previous scan's value, which would be lost if updated first.
  4. 4. What problem do edge detection and counting solve together?

    Counting on the edge adds one per press, not one per scan.

Summary

  • An edge is a change of state; a one-shot is true for one scan.
  • Compute it with a previous-value bit, or use R_TRIG / F_TRIG.
  • Edges are used for counting, toggling and starting sequences.
  • Give each edge its own memory, call it every scan and debounce noisy inputs.

Next: Interlocks and Motor Control