Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 37 ยท Timers, Counters and Sequences

PLC Edge Detection and One-Shots

Learn how PLCs detect rising and falling edges with R_TRIG, F_TRIG and one-shot contacts, why they matter for buttons and counters, with runnable C models.

A button is pressed. For the human it is one event. For the PLC, which scans the program perhaps 500 times per second, the button is "on" for dozens of consecutive scans. If your logic says "when the button is on, add one to the count," you will add dozens. What you want is to react once, at the moment the signal changes. That moment is an edge, and detecting it is one of the first idioms you learn in PLC programming.

PLC code is shown read-only in this lesson; the runnable programs are C stand-ins that model the ideas.

Level versus edge

A level is the current state of a bit: it is true or false. An edge is a change between two scans:

  • Rising edge (positive transition): false in the previous scan, true in this one.
  • Falling edge (negative transition): true in the previous scan, false in this one.
TEXT
scan:     1  2  3  4  5  6  7  8
input:    0  0  1  1  1  0  0  1
rising:   0  0  1  0  0  0  0  1
falling:  0  0  0  0  0  1  0  0

The edge bit is true for exactly one scan. That is why it is also called a one-shot: it fires once. To detect an edge, you need memory: the PLC must remember what the input was last scan.

R_TRIG and F_TRIG

The IEC standard provides two function blocks for this:

TEXT
risingDetector(CLK := startButton);
IF risingDetector.Q THEN
    cycleCount := cycleCount + 1;
END_IF;

fallingDetector(CLK := doorSensor);
IF fallingDetector.Q THEN
    doorClosedEvent := TRUE;
END_IF;

R_TRIG outputs Q true for one scan on a rising edge of CLK; F_TRIG does the same for a falling edge. Each instance stores its own previous value, so you need one instance per signal. In ladder logic you will meet the same idea as positive-transition contacts (P contacts in IEC, ONS one-shot in Allen-Bradley, P_TRIG in Siemens). They are one-shots built into the instruction set, and each requires a memory bit to store the previous state.

Building a rising edge yourself

The internal logic is two lines. This C program models it and shows the output for a scripted input:

C C stand-in: rising and falling edge detectors

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.

Each time the input goes up, rise flashes for one scan; each time it goes down, fall does. Note that prev is updated after the comparison: if you update it first, the edge is never seen.

Toggle: one button, two states

A classic use is a push-on, push-off button. Each press toggles a motor between running and stopped. Without an edge, the output would flicker every scan while the button is held.

C C stand-in: toggle on each button press

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.

Despite the button being held for four scans, the motor changes once per press. If you removed the edge check, the motor would change state on every scan the button was held.

Where edges belong

  • Before counters and toggles. A counter's count input already includes an edge detector, but a counter built from + 1 arithmetic does not. See counters.
  • Command triggers. Starting a recipe, sending a message, or logging an event should happen once per request, so trigger them with a rising edge.
  • Fault logging. A falling edge of "healthy" or a rising edge of "fault" tells you the instant a problem began, which you can timestamp.
  • Handshakes. Many communication protocols use a request bit that you pulse and wait for an acknowledge.

Pitfalls

  1. Reusing one detector for two signals. Each signal needs its own memory bit or instance.
  2. Calling the detector conditionally. If it is skipped for a few scans, prev becomes stale and you may see a phantom edge on return. Call edge detectors every scan.
  3. Edge on power-up. On the first scan prev is false, so an input that is already true produces a rising edge. Decide whether that is acceptable or whether you need an "initialized" flag.
  4. Duplicate use of an ONS memory bit. Two instructions sharing one storage bit block each other.
  5. Missed edges. A pulse shorter than the scan time may never be seen. Use hardware interrupts or a faster input for very short pulses.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. For how many scans is the output of an R_TRIG true after a rising edge?

    The edge output is true only in the scan where the input changed from false to true. In the next scan the stored previous value matches, so it is false again.
  2. 2. What must an edge detector remember between scans?

    An edge is a difference between this scan and the last one, so the previous input value must be stored.
  3. 3. In the C model, what goes wrong if prev is set to the new input BEFORE computing the edge?

    After prev = in, the check in && !prev is always false, so the detector never fires.
  4. 4. A button toggles a motor. Holding the button for 20 scans without an edge detector will cause what?

    Without an edge, the toggle logic runs on every scan where the level is true, so the output flickers.

Summary

  • A level is a state; an edge is a change between scans, true for exactly one scan.
  • R_TRIG detects rising edges and F_TRIG falling edges; ladder has equivalent one-shot contacts.
  • Edge detection needs one stored bit per signal, updated after comparing.
  • Use edges for toggles, counters, command triggers, fault logging and handshakes.
  • Call detectors every scan and watch out for the first scan after power-up.

Next: Sequencers and State Machines